From a3ee77e70d16dc48ffbc21e3a6827dc878b7a9b2 Mon Sep 17 00:00:00 2001 From: BeepoWuff <10000712+BeepoWuff@users.noreply.github.com> Date: Sun, 19 Sep 2021 00:10:39 +1000 Subject: [PATCH 1/4] Consistent enchantment name handling between anvil and enchanting table enchanting --- .../jobs/listeners/JobsPaymentListener.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index beab53cd..2e9f924e 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -840,6 +840,19 @@ public final class JobsPaymentListener implements Listener { return a.getAmount() + b.getAmount() <= a.getType().getMaxStackSize(); } + private static String getEnchantName(Enchantment enchant) { + try { + return enchant.getKey().getKey().toLowerCase().replace("_", "").replace("minecraft:", ""); + + } catch (Throwable e) { + CMIEnchantment cmiEnchant = CMIEnchantment.get(enchant); + if (cmiEnchant != null) + return cmiEnchant.toString(); + } + + return null; + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryRepair(InventoryClickEvent event) { // If event is nothing or place, do nothing @@ -956,9 +969,9 @@ public final class JobsPaymentListener implements Listener { if (enchant == null) continue; - CMIEnchantment e = CMIEnchantment.get(enchant); - if (e != null) - Jobs.action(jPlayer, new EnchantActionInfo(e.toString(), oneEnchant.getValue(), ActionType.ENCHANT)); + String enchantName = getEnchantName(enchant); + 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 Jobs.action(jPlayer, new ItemActionInfo(resultStack, ActionType.REPAIR)); @@ -1013,16 +1026,7 @@ public final class JobsPaymentListener implements Listener { if (enchant == null) continue; - String enchantName = null; - - try { - enchantName = enchant.getKey().getKey().toLowerCase().replace("_", "").replace("minecraft:", ""); - } catch (Throwable e) { - CMIEnchantment ench = CMIEnchantment.get(enchant); - if (ench != null) - enchantName = ench.toString(); - } - + String enchantName = getEnchantName(enchant); if (enchantName != null) Jobs.action(jPlayer, new EnchantActionInfo(enchantName, oneEnchant.getValue(), ActionType.ENCHANT)); } From bf58cf07663e199feda54f3a68184a25fc9aa2ed Mon Sep 17 00:00:00 2001 From: BeepoWuff <10000712+BeepoWuff@users.noreply.github.com> Date: Sun, 19 Sep 2021 00:48:42 +1000 Subject: [PATCH 2/4] Add explicit job info handling for EnchantActionInfo to ensure correct matching --- .../jobs/actions/EnchantActionInfo.java | 4 ++++ .../java/com/gamingmesh/jobs/container/Job.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java b/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java index 2aad8f72..0a940ceb 100644 --- a/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java +++ b/src/main/java/com/gamingmesh/jobs/actions/EnchantActionInfo.java @@ -40,4 +40,8 @@ public class EnchantActionInfo extends BaseActionInfo { public String getNameWithSub() { return name + ":" + level; } + + public int getLevel() { + return level; + } } diff --git a/src/main/java/com/gamingmesh/jobs/container/Job.java b/src/main/java/com/gamingmesh/jobs/container/Job.java index 2510203e..cbb6e483 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Job.java +++ b/src/main/java/com/gamingmesh/jobs/container/Job.java @@ -29,6 +29,8 @@ import java.util.Map; import java.util.Random; import java.util.function.BiPredicate; +import com.gamingmesh.jobs.CMILib.CMIEnchantment; +import com.gamingmesh.jobs.actions.EnchantActionInfo; import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; @@ -307,6 +309,19 @@ public class Job { return jobInfo.getName().equalsIgnoreCase(subName) || (jobInfo.getName() + ":" + jobInfo.getMeta()).equalsIgnoreCase(subName); } + if (actionInfo instanceof EnchantActionInfo) { + EnchantActionInfo enchantActionInfo = (EnchantActionInfo)actionInfo; + + String enchantName = CMIEnchantment.get(actionInfo.getName()).toString(); + + return ( + // Enchantment without level e.g. silk_touch + jobInfo.getName().equalsIgnoreCase(enchantName) || + // Enchantment with level e.g. fire_aspect:1 + jobInfo.getName().equalsIgnoreCase(enchantName + ":" + enchantActionInfo.getLevel()) + ); + } + return jobInfo.getName().equalsIgnoreCase(action.getNameWithSub()) || (jobInfo.getName() + ":" + jobInfo.getMeta()).equalsIgnoreCase(action.getNameWithSub()) || jobInfo.getName().equalsIgnoreCase(action.getName()); From a224545d799a6b17c7e134cf503a999d2d64b527 Mon Sep 17 00:00:00 2001 From: BeepoWuff <10000712+BeepoWuff@users.noreply.github.com> Date: Tue, 21 Sep 2021 01:05:13 +1000 Subject: [PATCH 3/4] Only pay for new enchantments when enchanting items with books on an anvil --- .../gamingmesh/jobs/listeners/JobsPaymentListener.java | 4 +++- src/main/java/com/gamingmesh/jobs/stuff/Util.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 2e9f924e..8fbd1604 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -964,7 +964,9 @@ public final class JobsPaymentListener implements Listener { ItemStack secondSlotItem = inv.getItem(1); if (Jobs.getGCManager().PayForEnchantingOnAnvil && secondSlotItem != null && secondSlotItem.getType() == Material.ENCHANTED_BOOK) { - for (Map.Entry oneEnchant : resultStack.getEnchantments().entrySet()) { + Map newEnchantments = Util.mapUnique(resultStack.getEnchantments(), firstSlot.getEnchantments()); + + for (Map.Entry oneEnchant : newEnchantments.entrySet()) { Enchantment enchant = oneEnchant.getKey(); if (enchant == null) continue; diff --git a/src/main/java/com/gamingmesh/jobs/stuff/Util.java b/src/main/java/com/gamingmesh/jobs/stuff/Util.java index 902fcfb8..76bcc2d1 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/Util.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/Util.java @@ -395,4 +395,14 @@ public final class Util { return listOfCommands; } + + public static Map mapUnique(Map left, Map right) { + Map difference = new HashMap<>(); + + difference.putAll(left); + difference.putAll(right); + difference.entrySet().removeAll(right.entrySet()); + + return difference; + } } From e9c1937953f3c2ba5cf4c68f5908d8dc06ce462e Mon Sep 17 00:00:00 2001 From: BeepoWuff <10000712+BeepoWuff@users.noreply.github.com> Date: Tue, 21 Sep 2021 02:08:15 +1000 Subject: [PATCH 4/4] Update quest objective matching to better match job task matching. Allows the default enchanter quest to actually detect ARROW_DAMAGE enchantment. --- .../com/gamingmesh/jobs/container/Job.java | 13 +---- .../jobs/container/QuestProgression.java | 54 +++++++++++++------ .../java/com/gamingmesh/jobs/stuff/Util.java | 13 +++++ 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/container/Job.java b/src/main/java/com/gamingmesh/jobs/container/Job.java index cbb6e483..66b0d2c4 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Job.java +++ b/src/main/java/com/gamingmesh/jobs/container/Job.java @@ -29,8 +29,8 @@ import java.util.Map; import java.util.Random; import java.util.function.BiPredicate; -import com.gamingmesh.jobs.CMILib.CMIEnchantment; import com.gamingmesh.jobs.actions.EnchantActionInfo; +import com.gamingmesh.jobs.stuff.Util; import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; @@ -310,16 +310,7 @@ public class Job { } if (actionInfo instanceof EnchantActionInfo) { - EnchantActionInfo enchantActionInfo = (EnchantActionInfo)actionInfo; - - String enchantName = CMIEnchantment.get(actionInfo.getName()).toString(); - - return ( - // Enchantment without level e.g. silk_touch - jobInfo.getName().equalsIgnoreCase(enchantName) || - // Enchantment with level e.g. fire_aspect:1 - jobInfo.getName().equalsIgnoreCase(enchantName + ":" + enchantActionInfo.getLevel()) - ); + return Util.enchantMatchesActionInfo(jobInfo.getName(), (EnchantActionInfo) actionInfo); } return jobInfo.getName().equalsIgnoreCase(action.getNameWithSub()) || diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java index 00c70c68..e9c71744 100644 --- a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java +++ b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java @@ -4,6 +4,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import com.gamingmesh.jobs.actions.EnchantActionInfo; +import com.gamingmesh.jobs.stuff.Util; import org.bukkit.Bukkit; import org.bukkit.event.server.ServerCommandEvent; @@ -97,7 +99,9 @@ public class QuestProgression { return; Map byAction = quest.getObjectives().get(action.getType()); - if (byAction != null && !byAction.containsKey(action.getNameWithSub()) && !byAction.containsKey(action.getName())) + QuestObjective objective = objectiveForAction(action); + + if (byAction != null && objective == null) return; org.bukkit.entity.Player player = jPlayer.getPlayer(); @@ -118,20 +122,12 @@ public class QuestProgression { } } - if (!isCompleted()) { - QuestObjective objective = null; - - if (byAction != null) { - objective = byAction.get(action.getName()); - - if (objective == null) - objective = byAction.get(action.getNameWithSub()); - } - - if (objective != null) { - Integer old = done.getOrDefault(objective, 0); - done.put(objective, old < objective.getAmount() ? old + 1 : objective.getAmount()); - } + if ( + !isCompleted() && + objective != null + ) { + Integer old = done.getOrDefault(objective, 0); + done.put(objective, old < objective.getAmount() ? old + 1 : objective.getAmount()); } jPlayer.setSaved(false); @@ -159,4 +155,32 @@ public class QuestProgression { public void setGivenReward(boolean givenReward) { this.givenReward = givenReward; } + + private boolean objectiveKeyMatches(String objectiveKey, ActionInfo actionInfo) { + if (actionInfo instanceof EnchantActionInfo) { + return Util.enchantMatchesActionInfo(objectiveKey, (EnchantActionInfo) actionInfo); + } + + return ( + objectiveKey.equalsIgnoreCase(actionInfo.getNameWithSub()) || + objectiveKey.equalsIgnoreCase(actionInfo.getName()) + ); + } + + private QuestObjective objectiveForAction(ActionInfo actionInfo) { + Map byAction = quest.getObjectives().get(actionInfo.getType()); + if (byAction == null) { + return null; + } + + for (Map.Entry objectiveEntry : byAction.entrySet()) { + String objectiveKey = objectiveEntry.getKey(); + + if (objectiveKeyMatches(objectiveKey, actionInfo)) { + return objectiveEntry.getValue(); + } + } + + return null; + } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/Util.java b/src/main/java/com/gamingmesh/jobs/stuff/Util.java index 76bcc2d1..a03070b1 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/Util.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/Util.java @@ -14,6 +14,8 @@ import java.util.UUID; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import com.gamingmesh.jobs.CMILib.CMIEnchantment; +import com.gamingmesh.jobs.actions.EnchantActionInfo; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -405,4 +407,15 @@ public final class Util { return difference; } + + public static boolean enchantMatchesActionInfo(String enchant, EnchantActionInfo actionInfo) { + String enchantName = CMIEnchantment.get(actionInfo.getName()).toString(); + + return ( + // Enchantment without level e.g. silk_touch + enchant.equalsIgnoreCase(enchantName) || + // Enchantment with level e.g. fire_aspect:1 + enchant.equalsIgnoreCase(enchantName + ":" + actionInfo.getLevel()) + ); + } }