From 2ca3544741cc1bade6128bd5cd604bf1cdaa45c6 Mon Sep 17 00:00:00 2001 From: JeBobs Date: Mon, 27 Dec 2021 14:29:54 -0500 Subject: [PATCH] Added option to disallow using enchanted materials to repair items. (#4693) --- .../gmail/nossr50/config/AdvancedConfig.java | 1 + .../nossr50/skills/repair/RepairManager.java | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java index fd2736190..dda173802 100644 --- a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java +++ b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java @@ -680,6 +680,7 @@ public class AdvancedConfig extends AutoUpdateConfigLoader { /* REPAIR */ public double getRepairMasteryMaxBonus() { return config.getDouble("Skills.Repair.RepairMastery.MaxBonusPercentage", 200.0D); } public int getRepairMasteryMaxLevel() { return config.getInt("Skills.Repair.RepairMastery.MaxBonusLevel", 100); } + public boolean getAllowEnchantedRepairMaterials() { return config.getBoolean("Skills.Repair.Use_Enchanted_Materials", false); } public boolean getArcaneForgingEnchantLossEnabled() { return config.getBoolean("Skills.Repair.ArcaneForging.May_Lose_Enchants", true); } public double getArcaneForgingKeepEnchantsChance(int rank) { return config.getDouble("Skills.Repair.ArcaneForging.Keep_Enchants_Chance.Rank_" + rank); } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java index aec9f007c..42bc3cdea 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java @@ -28,8 +28,13 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import java.util.Arrays; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collector; +import java.util.stream.Collectors; public class RepairManager extends SkillManager { private boolean placedAnvil; @@ -128,6 +133,35 @@ public class RepairManager extends SkillManager { // toRemove should be refreshed before the event call. toRemove = inventory.getItem(inventory.first(repairMaterial)).clone(); + + // Check if we allow enchanted materials to be used to repair objects. + // (Servers may provide enchanted items that don't follow their intended use) + if (!mcMMO.p.getAdvancedConfig().getAllowEnchantedRepairMaterials()) { + + // See if our proposed item is even enchanted in the first place. + if (toRemove.getEnchantments().size() > 0) { + + // Lots of array sorting to find a potential non-enchanted candidate item. + Optional possibleMaterial = Arrays.stream(inventory.getContents()) + .filter(Objects::nonNull) + .filter(p -> p.getType() == repairMaterial) + .filter(p -> p.getEnchantments().isEmpty()) + .findFirst(); + + // Fail out with "you need material" if we don't find a suitable alternative. + if (possibleMaterial.isEmpty()) { + String prettyName = repairable.getRepairMaterialPrettyName() == null ? StringUtils.getPrettyItemString(repairMaterial) : repairable.getRepairMaterialPrettyName(); + + String materialsNeeded = ""; + + NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Skills.NeedMore.Extra", prettyName, materialsNeeded); + return; + } + + // Update our toRemove item to our suggested possible material. + toRemove = possibleMaterial.get().clone(); + } + } // Call event if (EventUtils.callRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item).isCancelled()) {