From e06f70e15896b9d9be40412d99187cdeb4464317 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Mon, 17 Feb 2020 00:00:42 +0100 Subject: [PATCH] Fixed resource regen event spam, removed useless event calls --- .../java/net/Indyuce/mmocore/MMOCore.java | 2 +- .../mmocore/api/player/PlayerData.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index dbc393eb..ad5c8ef5 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -240,7 +240,7 @@ public class MMOCore extends JavaPlugin { resource.regen(player, d); } } - }.runTaskTimerAsynchronously(MMOCore.plugin, 100, 20); + }.runTaskTimer(MMOCore.plugin, 100, 20); /* * For the sake of the lord, make sure they aren't using MMOItems Mana 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 5e29febe..a6f305f8 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -398,12 +398,16 @@ public class PlayerData extends OfflinePlayerData { } public void heal(double heal) { + double current = player.getHealth(), newest = Math.max(0, Math.min(player.getHealth() + heal, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())); + if (current == newest) + return; + 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()))); + getPlayer().setHealth(newest); } public void addFriend(UUID uuid) { @@ -542,30 +546,42 @@ public class PlayerData extends OfflinePlayerData { } public void giveMana(double amount) { + 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); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return; - mana = Math.max(0, Math.min(getStats().getStat(StatType.MAX_MANA), mana + amount)); + mana = newest; } public void giveStamina(double amount) { + 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); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return; - stamina = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STAMINA), stamina + amount)); + stamina = newest; } public void giveStellium(double amount) { + 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); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return; - stellium = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STELLIUM), stellium + amount)); + stellium = newest; } public double getMana() {