From c1cf2bf406e0cc76140f4a0e2d2c0473cce479a9 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sun, 29 Dec 2019 15:17:31 +0100 Subject: [PATCH] Moved stat handling to MMOLib --- .../java/net/Indyuce/mmocore/MMOCore.java | 11 ++- .../net/Indyuce/mmocore/comp/MMOLibHook.java | 79 ------------------- 2 files changed, 9 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/net/Indyuce/mmocore/comp/MMOLibHook.java diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index 7fa769ec..80e9957c 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -31,7 +31,6 @@ import net.Indyuce.mmocore.command.QuestsCommand; import net.Indyuce.mmocore.command.SkillsCommand; import net.Indyuce.mmocore.command.WaypointsCommand; import net.Indyuce.mmocore.command.WithdrawCommand; -import net.Indyuce.mmocore.comp.MMOLibHook; import net.Indyuce.mmocore.comp.ShopKeepersEntityHandler; import net.Indyuce.mmocore.comp.citizens.CitizenInteractEventListener; import net.Indyuce.mmocore.comp.citizens.CitizensMMOLoader; @@ -90,6 +89,8 @@ import net.Indyuce.mmocore.manager.social.BoosterManager; import net.Indyuce.mmocore.manager.social.GuildManager; import net.Indyuce.mmocore.manager.social.PartyManager; import net.Indyuce.mmocore.manager.social.RequestManager; +import net.mmogroup.mmolib.api.stat.StatMap; +import net.mmogroup.mmolib.api.stat.instance.MMOCoreStatInstance; import net.mmogroup.mmolib.comp.Metrics; public class MMOCore extends JavaPlugin { @@ -156,7 +157,13 @@ public class MMOCore extends JavaPlugin { new Metrics(this); - new MMOLibHook(); + /* + * mmocore stats are functions of the stat base value. the function + * applies all the different stat modifiers saved in the stat map using + * specific stat instances let MMOLib calculate stats with set base + * value + */ + StatMap.setInstanceGenerator((map, stat) -> new MMOCoreStatInstance(map, stat)); if (Bukkit.getPluginManager().getPlugin("Vault") != null) economy = new VaultEconomy(); diff --git a/src/main/java/net/Indyuce/mmocore/comp/MMOLibHook.java b/src/main/java/net/Indyuce/mmocore/comp/MMOLibHook.java deleted file mode 100644 index 86f9620f..00000000 --- a/src/main/java/net/Indyuce/mmocore/comp/MMOLibHook.java +++ /dev/null @@ -1,79 +0,0 @@ -package net.Indyuce.mmocore.comp; - -import java.util.Iterator; -import java.util.function.Consumer; - -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeInstance; -import org.bukkit.attribute.AttributeModifier; -import org.bukkit.inventory.ItemStack; - -import net.Indyuce.mmocore.MMOCore; -import net.Indyuce.mmocore.api.player.stats.StatType; -import net.mmogroup.mmolib.api.player.MMOData; -import net.mmogroup.mmolib.api.stat.SharedStat; -import net.mmogroup.mmolib.api.stat.StatMap; -import net.mmogroup.mmolib.api.stat.instance.MMOCoreStatInstance; - -public class MMOLibHook { - public MMOLibHook() { - StatMap.registerUpdate(SharedStat.ARMOR, new AttributeStatHandler(Attribute.GENERIC_ARMOR, StatType.ARMOR)); - StatMap.registerUpdate(SharedStat.ARMOR_TOUGHNESS, new AttributeStatHandler(Attribute.GENERIC_ARMOR_TOUGHNESS, StatType.ARMOR)); - - StatMap.registerUpdate(SharedStat.ATTACK_DAMAGE, new AttributeStatHandler(Attribute.GENERIC_ATTACK_DAMAGE, StatType.ATTACK_DAMAGE)); - StatMap.registerUpdate(SharedStat.ATTACK_SPEED, new AttributeStatHandler(Attribute.GENERIC_ATTACK_SPEED, StatType.ATTACK_SPEED)); - StatMap.registerUpdate(SharedStat.KNOCKBACK_RESISTANCE, new AttributeStatHandler(Attribute.GENERIC_KNOCKBACK_RESISTANCE, StatType.KNOCKBACK_RESISTANCE)); - StatMap.registerUpdate(SharedStat.MAX_HEALTH, new AttributeStatHandler(Attribute.GENERIC_MAX_HEALTH, StatType.MAX_HEALTH)); - - Consumer moveSpeed = new MovementSpeedStat(); - StatMap.registerUpdate(SharedStat.MOVEMENT_SPEED, moveSpeed); - StatMap.registerUpdate(SharedStat.SPEED_MALUS_REDUCTION, moveSpeed); - - StatMap.setInstanceGenerator((map, stat) -> new MMOCoreStatInstance(map, stat)); - } - - public class AttributeStatHandler implements Consumer { - private final Attribute attribute; - private final StatType stat; - - public AttributeStatHandler(Attribute attribute, StatType stat) { - this.attribute = attribute; - this.stat = stat; - } - - @Override - public void accept(MMOData data) { - AttributeInstance ins = data.getPlayer().getAttribute(attribute); - removeModifiers(ins); - ins.setBaseValue(data.getMMOCore().getStats().getStat(stat)); - } - - private void removeModifiers(AttributeInstance ins) { - for (Iterator iterator = ins.getModifiers().iterator(); iterator.hasNext();) { - AttributeModifier attribute = iterator.next(); - if (attribute.getName().startsWith("mmolib.")) - ins.removeModifier(attribute); - } - } - } - - /* - * both used for the 'movement speed' and for the 'speed malus reduction' - * stats because the movement speed must be refreshed every time one of - * these stats are changed. - */ - public class MovementSpeedStat implements Consumer { - - @Override - public void accept(MMOData data) { - double speedMalus = MMOCore.plugin.configManager.speedMalus * (1 - data.getMMOCore().getStats().getStat(StatType.SPEED_MALUS_REDUCTION) / 100); - double movementSpeed = data.getMMOCore().getStats().getStat(StatType.MOVEMENT_SPEED); - - for (ItemStack item : data.getPlayer().getEquipment().getArmorContents()) - if (item != null) - if (item.getType().name().contains("IRON") || item.getType().name().contains("DIAMOND")) - movementSpeed *= 1 - speedMalus; - data.getPlayer().setWalkSpeed((float) Math.min(1, movementSpeed)); - } - } -}