From 41498c4d88ede886647926dbb87ba66ee0f09299 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 27 Sep 2020 12:40:49 +0100 Subject: [PATCH] Optimised armor checks --- .../enchantments/util/EnchantChecks.java | 16 ++++++++++++++++ .../enchantments/util/WatcherTriggers.java | 19 ++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantChecks.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantChecks.java index 3859f55d..39a8cdbc 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantChecks.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/EnchantChecks.java @@ -183,6 +183,22 @@ public final class EnchantChecks { return armorPoints.get(); } + public static Map getEnchantsOnArmor(LivingEntity entity) { + if(entity.getEquipment() == null) + return new HashMap<>(); + + AtomicInteger armorPoints = new AtomicInteger(0); + List armor = Arrays.asList(entity.getEquipment().getArmorContents()); + + Map ecoEnchants = new HashMap<>(); + + armor.forEach((itemStack -> { + ecoEnchants.putAll(EnchantChecks.getEnchantsOnItem(itemStack)); + })); + + return ecoEnchants; + } + public static boolean helmet(LivingEntity entity, Enchantment enchantment) { return getHelmetLevel(entity, enchantment) != 0; } diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java index fe21d43b..04841cf0 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java @@ -2,7 +2,6 @@ package com.willfp.ecoenchants.enchantments.util; import com.google.common.collect.Sets; import com.willfp.ecoenchants.EcoEnchantsPlugin; -import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.events.armorequip.ArmorEquipEvent; import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager; @@ -102,13 +101,11 @@ public class WatcherTriggers implements Listener { jumpVelocity = Float.parseFloat(df.format(jumpVelocity).replace(',', '.')); if (event.getPlayer().getLocation().getBlock().getType() != Material.LADDER && prevPlayersOnGround.contains(player.getUniqueId())) { if (!player.isOnGround() && Float.compare((float) player.getVelocity().getY(), jumpVelocity) == 0) { - EcoEnchants.getAll().forEach((enchant -> { + EnchantChecks.getEnchantsOnArmor(player).forEach((enchant, level) -> { if(event.isCancelled()) return; if(!enchant.isEnabled()) return; - int level = EnchantChecks.getArmorPoints(player, enchant); - if(level == 0) return; enchant.onJump(player, level, event); - })); + }); } } } @@ -171,13 +168,11 @@ public class WatcherTriggers implements Listener { LivingEntity victim = (LivingEntity) event.getEntity(); - EcoEnchants.getAll().forEach((enchant -> { + EnchantChecks.getEnchantsOnArmor(victim).forEach((enchant, level) -> { if(event.isCancelled()) return; if(!enchant.isEnabled()) return; - int level = EnchantChecks.getArmorPoints(victim, enchant); - if(level == 0) return; enchant.onFallDamage(victim, level, event); - })); + }); } @EventHandler(ignoreCancelled = true) @@ -237,13 +232,11 @@ public class WatcherTriggers implements Listener { LivingEntity victim = (LivingEntity) event.getEntity(); - EcoEnchants.getAll().forEach((enchant -> { + EnchantChecks.getEnchantsOnArmor(victim).forEach((enchant, level) -> { if(event.isCancelled()) return; if(!enchant.isEnabled()) return; - int level = EnchantChecks.getArmorPoints(victim, enchant); - if(level == 0) return; enchant.onDamageWearingArmor(victim, level, event); - })); + }); } @EventHandler(ignoreCancelled = true)