From 37a1afecb5433b912c0280c6e1ec9d5da7a9b4c8 Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 11 Feb 2022 11:57:36 +0200 Subject: [PATCH] Correct way to check for anvil enchanting --- .../jobs/actions/EnchantActionInfo.java | 36 +++++++++---------- .../jobs/listeners/JobsPaymentListener.java | 31 +++++++++++----- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java b/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java index 0a940ceb..107c4196 100644 --- a/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java +++ b/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java @@ -22,26 +22,26 @@ import com.gamingmesh.jobs.container.ActionType; import com.gamingmesh.jobs.container.BaseActionInfo; public class EnchantActionInfo extends BaseActionInfo { - private String name; - private int level; + private String name; + private int level; - public EnchantActionInfo(String name, int level, ActionType type) { - super(type); - this.name = name; - this.level = level; - } + public EnchantActionInfo(String name, int level, ActionType type) { + super(type); + this.name = name; + this.level = level; + } - @Override - public String getName() { - return name; - } + @Override + public String getName() { + return name; + } - @Override - public String getNameWithSub() { - return name + ":" + level; - } + @Override + public String getNameWithSub() { + return name + ":" + level; + } - public int getLevel() { - return level; - } + public int getLevel() { + return level; + } } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index cd7b2e7c..ec83c1ee 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -18,9 +18,11 @@ package com.gamingmesh.jobs.listeners; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -126,6 +128,7 @@ import net.Zrips.CMILib.Container.CMILocation; import net.Zrips.CMILib.Entities.CMIEntityType; import net.Zrips.CMILib.Items.CMIItemStack; import net.Zrips.CMILib.Items.CMIMaterial; +import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Version.Version; public final class JobsPaymentListener implements Listener { @@ -337,9 +340,9 @@ public final class JobsPaymentListener implements Listener { if (Jobs.getGCManager().payForStackedEntities) { if (JobsHook.WildStacker.isEnabled() && !StackSplit.SHEEP_SHEAR.isEnabled()) { - for(int i = 0; i < HookManager.getWildStackerHandler().getEntityAmount(sheep) - 1; i++) { - Jobs.action(jDamager, new CustomKillInfo(sheep.getColor().name(), ActionType.SHEAR)); - } + for (int i = 0; i < HookManager.getWildStackerHandler().getEntityAmount(sheep) - 1; i++) { + Jobs.action(jDamager, new CustomKillInfo(sheep.getColor().name(), ActionType.SHEAR)); + } } else if (JobsHook.StackMob.isEnabled() && HookManager.getStackMobHandler().isStacked(sheep)) { for (uk.antiperson.stackmob.entity.StackEntity stacked : HookManager.getStackMobHandler().getStackEntities()) { if (stacked.getEntity().getType() == sheep.getType()) { @@ -588,7 +591,7 @@ public final class JobsPaymentListener implements Listener { if (Jobs.getGCManager().payForStackedEntities) { if (JobsHook.WildStacker.isEnabled()) { for (int i = 0; i < HookManager.getWildStackerHandler().getEntityAmount(animal) - 1; i++) { - Jobs.action(jDamager, new EntityActionInfo(animal, ActionType.TAME)); + Jobs.action(jDamager, new EntityActionInfo(animal, ActionType.TAME)); } } else if (JobsHook.StackMob.isEnabled() && HookManager.getStackMobHandler().isStacked(animal)) { for (uk.antiperson.stackmob.entity.StackEntity stacked : HookManager.getStackMobHandler().getStackEntities()) { @@ -990,7 +993,7 @@ public final class JobsPaymentListener implements Listener { ItemStack secondSlotItem = inv.getItem(1); if (Jobs.getGCManager().PayForEnchantingOnAnvil && secondSlotItem != null && secondSlotItem.getType() == Material.ENCHANTED_BOOK) { - Map newEnchantments = Util.mapUnique(resultStack.getEnchantments(), firstSlot.getEnchantments()); + Map newEnchantments = mapUnique(resultStack.getEnchantments(), firstSlot.getEnchantments()); for (Map.Entry oneEnchant : newEnchantments.entrySet()) { Enchantment enchant = oneEnchant.getKey(); @@ -998,20 +1001,30 @@ public final class JobsPaymentListener implements Listener { continue; String enchantName = getEnchantName(enchant); - if (enchantName != null) + if (enchantName != null) { Jobs.action(jPlayer, new EnchantActionInfo(enchantName, oneEnchant.getValue(), ActionType.ENCHANT)); + } } } else if (secondSlotItem == null || secondSlotItem.getType() != Material.ENCHANTED_BOOK) { // Enchanted books does not have durability - if (!changed(firstSlot, secondSlotItem, resultStack)) return; - Jobs.action(jPlayer, new ItemActionInfo(resultStack, ActionType.REPAIR)); } } + private static Map mapUnique(Map map1, Map map2) { + Map map = new HashMap(); + for (Entry entry : map1.entrySet()) { + if (map2.get(entry.getKey()) != null && map2.get(entry.getKey()) == entry.getValue()) + continue; + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onEnchantItem(EnchantItemEvent event) { + if (!Jobs.getGCManager().canPerformActionInWorld(event.getEnchanter().getWorld())) return; @@ -1330,7 +1343,7 @@ public final class JobsPaymentListener implements Listener { if (Jobs.getGCManager().payForStackedEntities) { if (JobsHook.WildStacker.isEnabled()) { for (int i = 0; i < HookManager.getWildStackerHandler().getEntityAmount(lVictim) - 1; i++) { - Jobs.action(jDamager, new EntityActionInfo(lVictim, ActionType.KILL), e.getDamager(), lVictim); + Jobs.action(jDamager, new EntityActionInfo(lVictim, ActionType.KILL), e.getDamager(), lVictim); } } else if (JobsHook.StackMob.isEnabled() && HookManager.getStackMobHandler().isStacked(lVictim)) { for (uk.antiperson.stackmob.entity.StackEntity stacked : HookManager.getStackMobHandler().getStackEntities()) {