diff --git a/src/main/java/net/Indyuce/mmocore/api/event/PlayerRegenResourceEvent.java b/src/main/java/net/Indyuce/mmocore/api/event/PlayerRegenResourceEvent.java deleted file mode 100644 index 5ad84c38..00000000 --- a/src/main/java/net/Indyuce/mmocore/api/event/PlayerRegenResourceEvent.java +++ /dev/null @@ -1,62 +0,0 @@ -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; - } -} diff --git a/src/main/java/net/Indyuce/mmocore/api/event/PlayerResourceUpdateEvent.java b/src/main/java/net/Indyuce/mmocore/api/event/PlayerResourceUpdateEvent.java new file mode 100644 index 00000000..61d6b4fb --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/event/PlayerResourceUpdateEvent.java @@ -0,0 +1,121 @@ +package net.Indyuce.mmocore.api.event; + +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +public class PlayerResourceUpdateEvent extends PlayerDataEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + /** + * Type of resource being regenerated, this way + * this event handles all four resources. + */ + private final PlayerResource resource; + private final UpdateReason reason; + + /** + * Amount of resource regenerated. The whole point of the event is + * being able to modify it, for instance to apply the mana regeneration stat. + */ + private double amount; + + private boolean cancelled = false; + + /** + * Called when a player gains some resource back. This can + * be used to handle stats like health or mana regeneration. + *
+ * Example use: {@link net.Indyuce.mmocore.skill.Neptune_Gift} which is a skill
+ * that temporarily increases resource regeneration for a short amount of time.
+ *
+ * @param playerData Player regenerating
+ * @param resource Resource being increased
+ * @param amount Amount being taken away/regenerated
+ * @param reason The reason why this event was called
+ */
+ public PlayerResourceUpdateEvent(PlayerData playerData, PlayerResource resource, double amount, UpdateReason reason) {
+ super(playerData);
+
+ this.resource = resource;
+ this.amount = amount;
+ this.reason = reason;
+ }
+
+ public PlayerResource getResource() {
+ return resource;
+ }
+
+ public double getAmount() {
+ return amount;
+ }
+
+ /**
+ * Changes the amount of resource given/taken away
+ *
+ * @param amount New 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;
+ }
+
+ public enum UpdateReason {
+
+ /**
+ * When resource is being regenerated
+ */
+ REGENERATION,
+
+ /**
+ * When some resource is gained, or consumed by some skills
+ */
+ SKILL_REGENERATION,
+
+ /**
+ * When some resource is gained, or consumed by some skills
+ */
+ SKILL_COST,
+
+ /**
+ * Used by quests triggers
+ * - {@link net.Indyuce.mmocore.api.quest.trigger.ManaTrigger}
+ * - {@link net.Indyuce.mmocore.api.quest.trigger.StaminaTrigger}
+ * - {@link net.Indyuce.mmocore.api.quest.trigger.StelliumTrigger}
+ */
+ TRIGGER,
+
+ /**
+ * When using the resource command {@link net.Indyuce.mmocore.command.rpg.admin.ResourceCommandTreeNode}
+ */
+ COMMAND,
+
+ /**
+ * Anything else
+ */
+ OTHER;
+
+ public boolean isSkill() {
+ return this == SKILL_COST || this == SKILL_REGENERATION;
+ }
+ }
+}
diff --git a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java
index 85892f65..f412fb6b 100644
--- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java
+++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java
@@ -317,19 +317,30 @@ public class PlayerData extends OfflinePlayerData {
lastLootChest = System.currentTimeMillis();
}
+ /**
+ * @deprecated Provide a heal reason with {@link #heal(double, PlayerResourceUpdateEvent.UpdateReason)}
+ */
+ @Deprecated
public void heal(double heal) {
+ this.heal(heal, PlayerResourceUpdateEvent.UpdateReason.OTHER);
+ }
+
+ public void heal(double heal, PlayerResourceUpdateEvent.UpdateReason reason) {
if (!isOnline())
return;
+
+ // Avoid calling an useless event
double newest = Math.max(0, Math.min(getPlayer().getHealth() + heal, getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
if (getPlayer().getHealth() == newest)
return;
- PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.HEALTH, heal);
+ PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.HEALTH, heal, reason);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
- getPlayer().setHealth(newest);
+ // Use updated amount from event
+ getPlayer().setHealth(getPlayer().getHealth() + event.getAmount());
}
public void addFriend(UUID uuid) {
@@ -373,6 +384,12 @@ public class PlayerData extends OfflinePlayerData {
MMOCore.plugin.requestManager.registerRequest(request);
}
+ /**
+ * Teleports the player to a specific waypoint. This applies
+ * the stellium waypoint cost and plays the teleport animation.
+ *
+ * @param waypoint Target waypoint
+ */
public void warp(Waypoint waypoint) {
if (!isOnline())
return;
@@ -384,7 +401,7 @@ public class PlayerData extends OfflinePlayerData {
*/
lastWaypoint = System.currentTimeMillis();
- giveStellium(-waypoint.getStelliumCost());
+ giveStellium(-waypoint.getStelliumCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
new BukkitRunnable() {
final int x = getPlayer().getLocation().getBlockX();
@@ -399,7 +416,7 @@ public class PlayerData extends OfflinePlayerData {
|| getPlayer().getLocation().getBlockZ() != z) {
MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.WARP_CANCELLED);
MMOCore.plugin.configManager.getSimpleMessage("warping-canceled").send(getPlayer());
- giveStellium(waypoint.getStelliumCost());
+ giveStellium(waypoint.getStelliumCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_REGENERATION);
cancel();
return;
}
@@ -501,44 +518,76 @@ public class PlayerData extends OfflinePlayerData {
return profess == null ? MMOCore.plugin.classManager.getDefaultClass() : profess;
}
+ /**
+ * @deprecated Provide reason with {@link #giveMana(double, PlayerResourceUpdateEvent.UpdateReason)}
+ */
+ @Deprecated
public void giveMana(double amount) {
+ giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
+ }
+
+ public void giveMana(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
+
+ // Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_MANA), mana + amount));
if (mana == newest)
return;
- PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.MANA, amount);
+ PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.MANA, amount, reason);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
- mana = newest;
+ // Use updated amount from Bukkit event
+ setMana(mana + event.getAmount());
}
+ /**
+ * @deprecated Provide reason with {@link #giveStamina(double, PlayerResourceUpdateEvent.UpdateReason)}
+ */
+ @Deprecated
public void giveStamina(double amount) {
+ giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
+ }
+
+ public void giveStamina(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
+
+ // Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STAMINA), stamina + amount));
if (stamina == newest)
return;
- PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STAMINA, amount);
+ PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.STAMINA, amount, reason);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
- stamina = newest;
-
+ // Use updated amount from Bukkit event
+ setStamina(stamina + event.getAmount());
}
+ /**
+ * @deprecated Provide reason with {@link #giveStellium(double, PlayerResourceUpdateEvent.UpdateReason)}
+ */
+ @Deprecated
public void giveStellium(double amount) {
+ giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
+ }
+
+ public void giveStellium(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
+
+ // Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STELLIUM), stellium + amount));
if (stellium == newest)
return;
- PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STELLIUM, amount);
+ PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.STELLIUM, amount, reason);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
- stellium = newest;
+ // Use updated amount from Bukkit event
+ setStellium(stellium + event.getAmount());
}
public double getMana() {
@@ -794,8 +843,8 @@ public class PlayerData extends OfflinePlayerData {
flatCooldownReduction *= flatCooldownReduction > 0 ? skill.getModifier("cooldown", getSkillLevel(skill.getSkill())) * 1000 : 0;
skillData.setLastCast(cast.getSkill(), System.currentTimeMillis() - (long) flatCooldownReduction);
- giveMana(-cast.getManaCost());
- giveStamina(-cast.getStaminaCost());
+ giveMana(-cast.getManaCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
+ giveStamina(-cast.getStaminaCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
}
PlayerPostCastSkillEvent postEvent = new PlayerPostCastSkillEvent(this, skill, cast);
diff --git a/src/main/java/net/Indyuce/mmocore/api/player/profess/resource/PlayerResource.java b/src/main/java/net/Indyuce/mmocore/api/player/profess/resource/PlayerResource.java
index b29f4689..090e22d0 100644
--- a/src/main/java/net/Indyuce/mmocore/api/player/profess/resource/PlayerResource.java
+++ b/src/main/java/net/Indyuce/mmocore/api/player/profess/resource/PlayerResource.java
@@ -1,36 +1,72 @@
package net.Indyuce.mmocore.api.player.profess.resource;
+import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
+import net.Indyuce.mmocore.api.player.PlayerData;
+import net.Indyuce.mmocore.api.player.profess.ClassOption;
+import net.Indyuce.mmocore.api.player.stats.StatType;
+import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger;
+import org.bukkit.attribute.Attribute;
+
import java.util.function.BiConsumer;
import java.util.function.Function;
-import org.bukkit.attribute.Attribute;
-
-import net.Indyuce.mmocore.api.player.PlayerData;
-import net.Indyuce.mmocore.api.player.profess.ClassOption;
-import net.Indyuce.mmocore.api.player.stats.StatType;
-
public enum PlayerResource {
- HEALTH(StatType.HEALTH_REGENERATION, ClassOption.OFF_COMBAT_HEALTH_REGEN, (data) -> data.getPlayer().getHealth(),
- (data) -> data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(), PlayerData::heal),
- MANA(StatType.MANA_REGENERATION, ClassOption.OFF_COMBAT_MANA_REGEN, PlayerData::getMana,
- (data) -> data.getStats().getStat(StatType.MAX_MANA), PlayerData::giveMana),
- STAMINA(StatType.STAMINA_REGENERATION, ClassOption.OFF_COMBAT_STAMINA_REGEN, PlayerData::getStamina,
- (data) -> data.getStats().getStat(StatType.MAX_STAMINA), PlayerData::giveStamina),
- STELLIUM(StatType.STELLIUM_REGENERATION, ClassOption.OFF_COMBAT_STELLIUM_REGEN, PlayerData::getStellium,
- (data) -> data.getStats().getStat(StatType.MAX_STELLIUM), PlayerData::giveStellium);
+ HEALTH(StatType.HEALTH_REGENERATION, ClassOption.OFF_COMBAT_HEALTH_REGEN,
+ (data) -> data.getPlayer().getHealth(),
+ data -> data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(),
+ (data, amount) -> data.heal(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
+ (data, amount) -> data.heal(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.heal(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.getPlayer().setHealth(amount)),
+
+ MANA(StatType.MANA_REGENERATION, ClassOption.OFF_COMBAT_MANA_REGEN,
+ PlayerData::getMana,
+ data -> data.getStats().getStat(StatType.MAX_MANA),
+ (data, amount) -> data.giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
+ (data, amount) -> data.giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.giveMana(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.setMana(amount)),
+
+ STAMINA(StatType.STAMINA_REGENERATION, ClassOption.OFF_COMBAT_STAMINA_REGEN,
+ PlayerData::getStamina,
+ data -> data.getStats().getStat(StatType.MAX_STAMINA),
+ (data, amount) -> data.giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
+ (data, amount) -> data.giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.giveStamina(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.setStamina(amount)),
+
+ STELLIUM(StatType.STELLIUM_REGENERATION, ClassOption.OFF_COMBAT_STELLIUM_REGEN,
+ PlayerData::getStellium,
+ data -> data.getStats().getStat(StatType.MAX_STELLIUM),
+ (data, amount) -> data.giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
+ (data, amount) -> data.giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.giveStellium(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
+ (data, amount) -> data.setStellium(amount));
private final StatType regenStat;
private final ClassOption offCombatRegen;
private final Function
+ * This method makes sure that all the plugins which utilize this event
+ * can also communicate with MMOCore
*/
- @Deprecated
@EventHandler(priority = EventPriority.HIGH)
- public void g(PlayerRegenResourceEvent event) {
+ public void g(PlayerResourceUpdateEvent event) {
if (event.getResource() == PlayerResource.HEALTH) {
EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.CUSTOM);
Bukkit.getPluginManager().callEvent(bukkitEvent);
+
+ // Update event values
event.setCancelled(bukkitEvent.isCancelled());
event.setAmount(bukkitEvent.getAmount());
}
diff --git a/src/main/java/net/Indyuce/mmocore/skill/Ambers.java b/src/main/java/net/Indyuce/mmocore/skill/Ambers.java
index a390bac8..a6338a21 100644
--- a/src/main/java/net/Indyuce/mmocore/skill/Ambers.java
+++ b/src/main/java/net/Indyuce/mmocore/skill/Ambers.java
@@ -1,24 +1,19 @@
package net.Indyuce.mmocore.skill;
-import org.bukkit.Bukkit;
-import org.bukkit.Color;
-import org.bukkit.Location;
-import org.bukkit.Material;
-import org.bukkit.Particle;
-import org.bukkit.Sound;
-import org.bukkit.event.EventHandler;
-import org.bukkit.event.Listener;
-import org.bukkit.scheduler.BukkitRunnable;
-
+import io.lumine.mythic.lib.api.DamageType;
+import io.lumine.mythic.lib.api.event.PlayerAttackEvent;
import net.Indyuce.mmocore.MMOCore;
+import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.skill.Skill;
import net.Indyuce.mmocore.api.skill.SkillResult;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.api.util.math.particle.ParabolicProjectile;
-import io.lumine.mythic.lib.api.DamageType;
-import io.lumine.mythic.lib.api.event.PlayerAttackEvent;
+import org.bukkit.*;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.scheduler.BukkitRunnable;
public class Ambers extends Skill implements Listener {
public Ambers() {
@@ -79,7 +74,7 @@ public class Ambers extends Skill implements Listener {
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 1);
// data.getSkillData().ambers++;
- data.giveMana((data.getStats().getStat(StatType.MAX_MANA) - data.getMana()) * percent);
+ data.giveMana((data.getStats().getStat(StatType.MAX_MANA) - data.getMana()) * percent, PlayerResourceUpdateEvent.UpdateReason.SKILL_REGENERATION);
cancel();
return;
diff --git a/src/main/java/net/Indyuce/mmocore/skill/Neptune_Gift.java b/src/main/java/net/Indyuce/mmocore/skill/Neptune_Gift.java
index 9da94530..e995f95f 100644
--- a/src/main/java/net/Indyuce/mmocore/skill/Neptune_Gift.java
+++ b/src/main/java/net/Indyuce/mmocore/skill/Neptune_Gift.java
@@ -8,7 +8,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import net.Indyuce.mmocore.MMOCore;
-import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
+import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.skill.Skill;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
@@ -29,7 +29,7 @@ public class Neptune_Gift extends Skill implements Listener {
}
@EventHandler
- public void a(PlayerRegenResourceEvent event) {
+ public void a(PlayerResourceUpdateEvent event) {
PlayerData data = event.getData();
if (event.getPlayer().getLocation().getBlock().getType() == Material.WATER) {
Optional