From f5109635ff04f9e9b71d288f73009904cba64f96 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 6 Sep 2023 15:48:50 +0500 Subject: [PATCH] Fix for villager books --- .../excellentenchants/config/Config.java | 3 +++ .../enchantment/util/EnchantUtils.java | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java b/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java index 7e0aa92..4fe365c 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java @@ -117,6 +117,9 @@ public class Config { "When enabled, passive enchantments (permanent potion effects, regeneration, etc.) will be applied to mobs as well.", "Disable this if you're experiencing performance issues."); + public static final JOption ENCHANTMENTS_SINGLE_ENCHANT_IN_VILLAGER_BOOKS = JOption.create("Enchantments.Single_Enchant_In_Villager_Books", true, + "Sets whether or not enchanted books in villager trades will have only 1 enchant, vanilla or custom one."); + private static final JOption> OBTAIN_SETTINGS = new JOption>("Enchantments.Obtaining", (cfg, path, def) -> Stream.of(ObtainType.values()).collect(Collectors.toMap(k -> k, v -> ObtainSettings.read(cfg, path + "." + v.getPathName()))), () -> Stream.of(ObtainType.values()).collect(Collectors.toMap(k -> k, v -> new ObtainSettings(true, 4, 80D, 0, 2))), diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java index a307e64..c781005 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java @@ -106,12 +106,23 @@ public class EnchantUtils { public static boolean populate(@NotNull ItemStack item, @NotNull ObtainType obtainType, @Nullable World world) { AtomicBoolean status = new AtomicBoolean(false); - getPopulationCandidates(item, obtainType, world).forEach((enchantment, level) -> { + var population = getPopulationCandidates(item, obtainType, world); + + if (obtainType == ObtainType.VILLAGER && item.getType() == Material.ENCHANTED_BOOK) { + if (Config.ENCHANTMENTS_SINGLE_ENCHANT_IN_VILLAGER_BOOKS.get() && !population.isEmpty()) { + getAll(item).keySet().forEach(enchantment -> remove(item, enchantment)); + } + } + + population.forEach((enchantment, level) -> { if (add(item, enchantment, level, false)) { status.set(true); } }); - updateDisplay(item); + + if (status.get()) { + updateDisplay(item); + } return status.get(); } @@ -188,12 +199,6 @@ public class EnchantUtils { enchRoll--; } - if (!enchantsToAdd.isEmpty()) { - if (obtainType == ObtainType.VILLAGER && item.getType() == Material.ENCHANTED_BOOK && enchRoll == 1) { - getAll(item).keySet().forEach(enchantment -> remove(item, enchantment)); - } - } - return enchantsToAdd; }