From b398a41056e9f2ea6b50f0aae9a1ee82a115b3bd Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 21 Nov 2022 12:35:18 +0000 Subject: [PATCH] Fixed villager enchants --- .../ecoenchants/mechanics/VillagerSupport.kt | 23 ++++++++++++++++--- .../core-plugin/src/main/resources/config.yml | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/VillagerSupport.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/VillagerSupport.kt index c238bf94..359d19d0 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/VillagerSupport.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/VillagerSupport.kt @@ -33,6 +33,10 @@ class VillagerSupport( val enchants = result.fast().getEnchants(true) + if (NumberUtils.randFloat(0.0, 100.0) < plugin.configYml.getDouble("villager.pass-through-chance")) { + return + } + var multiplier = 0.01 if (result.type == Material.ENCHANTED_BOOK) { @@ -78,14 +82,27 @@ class VillagerSupport( multiplier /= this.plugin.configYml.getDouble("villager.reduction") - enchants[enchantment] = level + if (result.type == Material.ENCHANTED_BOOK) { + // Only allow one enchantment + enchants.clear() + enchants[enchantment] = level + break + } else { + enchants[enchantment] = level + } } val meta = result.itemMeta if (meta is EnchantmentStorageMeta) { - enchants.forEach { (enchant, level) -> meta.addStoredEnchant(enchant, level, true) } + for ((enchant, level) in enchants) { + meta.removeStoredEnchant(enchant) + meta.addStoredEnchant(enchant, level, true) + } } else { - enchants.forEach { (enchant, level) -> meta.addEnchant(enchant, level, true) } + for ((enchant, level) in enchants) { + meta.removeEnchant(enchant) + meta.addEnchant(enchant, level, true) + } } result.itemMeta = meta diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 41281103..658d7286 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -14,6 +14,7 @@ enchanting-table: # Options for obtaining custom enchants from villagers villager: enabled: true # If custom enchantments should be available from villagers + pass-through-chance: 25 # The chance to leave the book as-is with a vanilla/no enchantment applied. book-multiplier: 0.14 # Multiplier applied to the chance of getting an enchantment on a book (to balance enchant numbers) reduction: 5 # The chance to get each subsequent enchantment is divided by this number, e.g. 2nd enchant is 5x less likely than 1st, 3rd is 5x less likely again, etc