Optimised Watcher

This commit is contained in:
Auxilor 2020-09-27 12:32:53 +01:00
parent da012adf65
commit 6d2f6dca84
2 changed files with 90 additions and 32 deletions

View File

@ -1,5 +1,7 @@
package com.willfp.ecoenchants.enchantments.util; package com.willfp.ecoenchants.enchantments.util;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.util.ItemDurability; import com.willfp.ecoenchants.util.ItemDurability;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -10,6 +12,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue; import org.bukkit.metadata.MetadataValue;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -29,6 +32,22 @@ public final class EnchantChecks {
return item.getItemMeta().getEnchantLevel(enchantment); return item.getItemMeta().getEnchantLevel(enchantment);
} }
public static Map<EcoEnchant, Integer> getEnchantsOnItem(ItemStack item) {
if(item == null) return new HashMap<>();
if(item.getType().equals(Material.AIR)) return new HashMap<>();
if(!item.hasItemMeta()) return new HashMap<>();
if(item.getItemMeta() == null) return new HashMap<>();
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
item.getEnchantments().forEach(((enchantment, integer) -> {
if(EcoEnchants.getFromEnchantment(enchantment) != null) {
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer);
}
}));
return ecoEnchants;
}
public static boolean arrow(Arrow arrow, Enchantment enchantment) { public static boolean arrow(Arrow arrow, Enchantment enchantment) {
return getArrowLevel(arrow, enchantment) != 0; return getArrowLevel(arrow, enchantment) != 0;
} }
@ -46,6 +65,23 @@ public final class EnchantChecks {
return enchantments.get(enchantment); return enchantments.get(enchantment);
} }
public static Map<EcoEnchant, Integer> getEnchantsOnArrow(Arrow arrow) {
if (arrow.getMetadata("enchantments").isEmpty()) return new HashMap<>();
MetadataValue enchantmentsMetaValue = arrow.getMetadata("enchantments").get(0);
if (!(enchantmentsMetaValue.value() instanceof Map))
return new HashMap<>();
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
((Map<Enchantment, Integer>) enchantmentsMetaValue.value()).forEach(((enchantment, integer) -> {
if(EcoEnchants.getFromEnchantment(enchantment) != null) {
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer);
}
}));
return ecoEnchants;
}
public static boolean mainhand(LivingEntity entity, Enchantment enchantment) { public static boolean mainhand(LivingEntity entity, Enchantment enchantment) {
return getMainhandLevel(entity, enchantment) != 0; return getMainhandLevel(entity, enchantment) != 0;
} }
@ -58,6 +94,26 @@ public final class EnchantChecks {
return getItemLevel(item, enchantment); return getItemLevel(item, enchantment);
} }
public static Map<EcoEnchant, Integer> getEnchantsOnMainhand(LivingEntity entity) {
if(entity.getEquipment() == null)
return new HashMap<>();
ItemStack item = entity.getEquipment().getItemInMainHand();
if(item == null) return new HashMap<>();
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
item.getEnchantments().forEach(((enchantment, integer) -> {
if(EcoEnchants.getFromEnchantment(enchantment) != null) {
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer);
}
}));
return ecoEnchants;
}
public static boolean offhand(LivingEntity entity, Enchantment enchantment) { public static boolean offhand(LivingEntity entity, Enchantment enchantment) {
return getOffhandLevel(entity, enchantment) != 0; return getOffhandLevel(entity, enchantment) != 0;
} }
@ -71,6 +127,25 @@ public final class EnchantChecks {
return getItemLevel(item, enchantment); return getItemLevel(item, enchantment);
} }
public static Map<EcoEnchant, Integer> getEnchantsOnOffhand(LivingEntity entity) {
if(entity.getEquipment() == null)
return new HashMap<>();
ItemStack item = entity.getEquipment().getItemInOffHand();
if(item == null) return new HashMap<>();
Map<EcoEnchant, Integer> ecoEnchants = new HashMap<>();
item.getEnchantments().forEach(((enchantment, integer) -> {
if(EcoEnchants.getFromEnchantment(enchantment) != null) {
ecoEnchants.put(EcoEnchants.getFromEnchantment(enchantment), integer);
}
}));
return ecoEnchants;
}
public static int getArmorPoints(LivingEntity entity, Enchantment enchantment) { public static int getArmorPoints(LivingEntity entity, Enchantment enchantment) {
return getArmorPoints(entity, enchantment, 0); return getArmorPoints(entity, enchantment, 0);
} }

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.enchantments.util;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.events.armorequip.ArmorEquipEvent; import com.willfp.ecoenchants.events.armorequip.ArmorEquipEvent;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager; import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
@ -46,11 +47,9 @@ public class WatcherTriggers implements Listener {
if(event.isCancelled()) return; if(event.isCancelled()) return;
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnArrow(arrow).forEach(((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.arrow(arrow, enchant)) return;
int level = EnchantChecks.getArrowLevel(arrow, enchant);
enchant.onArrowDamage(attacker, victim, arrow, level, event); enchant.onArrowDamage(attacker, victim, arrow, level, event);
})); }));
} }
@ -81,11 +80,9 @@ public class WatcherTriggers implements Listener {
if (!AntigriefManager.canInjure((Player) attacker, victim)) return; if (!AntigriefManager.canInjure((Player) attacker, victim)) return;
} }
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnItem(item).forEach(((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.item(item, enchant)) return;
int level = EnchantChecks.getItemLevel(item, enchant);
enchant.onTridentDamage(attacker, victim, trident, level, event); enchant.onTridentDamage(attacker, victim, trident, level, event);
})); }));
} }
@ -142,13 +139,11 @@ public class WatcherTriggers implements Listener {
if (!AntigriefManager.canInjure((Player) attacker, victim)) return; if (!AntigriefManager.canInjure((Player) attacker, victim)) return;
} }
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnMainhand(attacker).forEach((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.mainhand(attacker, enchant)) return;
int level = EnchantChecks.getMainhandLevel(attacker, enchant);
enchant.onMeleeAttack(attacker, victim, level, event); enchant.onMeleeAttack(attacker, victim, level, event);
})); });
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
@ -159,13 +154,11 @@ public class WatcherTriggers implements Listener {
LivingEntity shooter = event.getEntity(); LivingEntity shooter = event.getEntity();
Arrow arrow = (Arrow) event.getProjectile(); Arrow arrow = (Arrow) event.getProjectile();
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnMainhand(shooter).forEach((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.mainhand(shooter, enchant)) return;
int level = EnchantChecks.getMainhandLevel(shooter, enchant);
enchant.onBowShoot(shooter, arrow, level, event); enchant.onBowShoot(shooter, arrow, level, event);
})); });
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
@ -197,10 +190,8 @@ public class WatcherTriggers implements Listener {
Arrow arrow = (Arrow) event.getEntity(); Arrow arrow = (Arrow) event.getEntity();
LivingEntity shooter = (LivingEntity) event.getEntity().getShooter(); LivingEntity shooter = (LivingEntity) event.getEntity().getShooter();
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnArrow(arrow).forEach(((enchant, level) -> {
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.arrow(arrow, enchant)) return;
int level = EnchantChecks.getArrowLevel(arrow, enchant);
enchant.onArrowHit(shooter, level, event); enchant.onArrowHit(shooter, level, event);
})); }));
} }
@ -216,10 +207,8 @@ public class WatcherTriggers implements Listener {
ItemStack item = TridentStack.getTridentStack(trident); ItemStack item = TridentStack.getTridentStack(trident);
LivingEntity shooter = (LivingEntity) event.getEntity().getShooter(); LivingEntity shooter = (LivingEntity) event.getEntity().getShooter();
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnItem(item).forEach(((enchant, level) -> {
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.item(item, enchant)) return;
int level = EnchantChecks.getItemLevel(item, enchant);
enchant.onTridentHit(shooter, level, event); enchant.onTridentHit(shooter, level, event);
})); }));
} }
@ -234,13 +223,11 @@ public class WatcherTriggers implements Listener {
if (event.isCancelled()) if (event.isCancelled())
return; return;
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnMainhand(player).forEach((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.mainhand(player, enchant)) return;
int level = EnchantChecks.getMainhandLevel(player, enchant);
enchant.onBlockBreak(player, block, level, event); enchant.onBlockBreak(player, block, level, event);
})); });
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
@ -281,13 +268,11 @@ public class WatcherTriggers implements Listener {
if(event.getBlock().getDrops(player.getInventory().getItemInMainHand()).isEmpty()) if(event.getBlock().getDrops(player.getInventory().getItemInMainHand()).isEmpty())
return; return;
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnMainhand(player).forEach((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.mainhand(player, enchant)) return;
int level = EnchantChecks.getMainhandLevel(player, enchant);
enchant.onDamageBlock(player, block, level, event); enchant.onDamageBlock(player, block, level, event);
})); });
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
@ -302,13 +287,11 @@ public class WatcherTriggers implements Listener {
LivingEntity shooter = (LivingEntity) trident.getShooter(); LivingEntity shooter = (LivingEntity) trident.getShooter();
ItemStack item = TridentStack.getTridentStack(trident); ItemStack item = TridentStack.getTridentStack(trident);
EcoEnchants.getAll().forEach((enchant -> { EnchantChecks.getEnchantsOnItem(item).forEach((enchant, level) -> {
if(event.isCancelled()) return; if(event.isCancelled()) return;
if(!enchant.isEnabled()) return; if(!enchant.isEnabled()) return;
if (!EnchantChecks.item(item, enchant)) return;
int level = EnchantChecks.getItemLevel(item, enchant);
enchant.onTridentLaunch(shooter, trident, level, event); enchant.onTridentLaunch(shooter, trident, level, event);
})); });
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)