Added API for resource regeneration

This commit is contained in:
Indyuce 2020-02-16 15:24:08 +01:00
parent 693e28fbea
commit fecb8470aa
4 changed files with 123 additions and 4 deletions

View File

@ -0,0 +1,62 @@
package net.Indyuce.mmocore.api.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
public class PlayerRegenResourceEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
/*
* type of resource being regenerated. this way all three re
*/
private final PlayerResource resource;
/*
* amount of resource regenerated. whole point of the event is to be able to
* change it.
*/
private double amount;
private boolean cancelled = false;
public PlayerRegenResourceEvent(PlayerData playerData, PlayerResource resource, double amount) {
super(playerData);
this.resource = resource;
this.amount = amount;
}
public PlayerResource getResource() {
return resource;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -32,11 +32,13 @@ import net.Indyuce.mmocore.api.Waypoint;
import net.Indyuce.mmocore.api.event.PlayerCastSkillEvent;
import net.Indyuce.mmocore.api.event.PlayerExperienceGainEvent;
import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.PlayerClass.Subclass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.api.player.social.FriendRequest;
import net.Indyuce.mmocore.api.player.social.Party;
import net.Indyuce.mmocore.api.player.social.guilds.Guild;
@ -396,6 +398,11 @@ public class PlayerData extends OfflinePlayerData {
}
public void heal(double heal) {
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.HEALTH, heal);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
getPlayer().setHealth(Math.max(0, Math.min(player.getHealth() + heal, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())));
}
@ -535,14 +542,29 @@ public class PlayerData extends OfflinePlayerData {
}
public void giveMana(double amount) {
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.MANA, amount);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
mana = Math.max(0, Math.min(getStats().getStat(StatType.MAX_MANA), mana + amount));
}
public void giveStamina(double amount) {
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STAMINA, amount);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
stamina = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STAMINA), stamina + amount));
}
public void giveStellium(double amount) {
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STELLIUM, amount);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
stellium = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STELLIUM), stellium + amount));
}

View File

@ -11,9 +11,6 @@ import net.Indyuce.mmocore.api.player.stats.StatType;
public enum PlayerResource {
/*
* used to handle resource regeneration.
*/
HEALTH(StatType.HEALTH_REGENERATION, ClassOption.OFF_COMBAT_HEALTH_REGEN, (data) -> data.getPlayer().getHealth(), (data) -> data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(), (data, d) -> data.heal(d)),
MANA(StatType.MANA_REGENERATION, ClassOption.OFF_COMBAT_MANA_REGEN, (data) -> data.getMana(), (data) -> data.getStats().getStat(StatType.MAX_MANA), (data, d) -> data.giveMana(d)),
STAMINA(StatType.STAMINA_REGENERATION, ClassOption.OFF_COMBAT_STAMINA_REGEN, (data) -> data.getStamina(), (data) -> data.getStats().getStat(StatType.MAX_STAMINA), (data, d) -> data.giveStamina(d)),
@ -32,22 +29,38 @@ public enum PlayerResource {
this.regen = regen;
}
/*
* stat which correspondons to resource regeneration
*/
public StatType getRegenStat() {
return regenStat;
}
/*
* class option which determines whether or not resource should be
* regenerated off combat only
*/
public ClassOption getOffCombatRegen() {
return offCombatRegen;
}
/*
* get current resource of player
*/
public double getCurrent(PlayerData player) {
return current.apply(player);
}
/*
* get max resource of player
*/
public double getMax(PlayerData player) {
return max.apply(player);
}
/*
* regenerate resource of player (TRIGGERS A BUKKIT/CUSTOM EVENT)
*/
public void regen(PlayerData player, double amount) {
regen.accept(player, amount);
}

View File

@ -1,19 +1,24 @@
package net.Indyuce.mmocore.listener;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import net.Indyuce.mmocore.api.event.PlayerCombatEvent;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.gui.api.PluginInventory;
public class PlayerListener implements Listener {
@ -57,7 +62,8 @@ public class PlayerListener implements Listener {
PlayerData.get((Player) event.getDamager()).updateCombat();
if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player) {
if(((Player) ((Projectile) event.getDamager()).getShooter()).hasMetadata("NPC")) return;
if (((Player) ((Projectile) event.getDamager()).getShooter()).hasMetadata("NPC"))
return;
PlayerData.get((Player) ((Projectile) event.getDamager()).getShooter()).updateCombat();
}
}
@ -77,4 +83,20 @@ public class PlayerListener implements Listener {
if (!event.entersCombat())
event.getData().getSkillData().resetData();
}
/*
* Warning: this really is not the best way to interface with MMOCore
* generation. Use instead PlayerRegenResourceEvent to be able to access
* directly the PlayerData without an extra map lookup.
*/
@Deprecated
@EventHandler(priority = EventPriority.HIGH)
public void g(PlayerRegenResourceEvent event) {
if (event.getResource() == PlayerResource.HEALTH) {
EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.REGEN);
Bukkit.getPluginManager().callEvent(bukkitEvent);
event.setCancelled(bukkitEvent.isCancelled());
event.setAmount(bukkitEvent.getAmount());
}
}
}