From 52b65554d9e68362214302af700aedb347a96687 Mon Sep 17 00:00:00 2001 From: Araca Date: Mon, 6 Nov 2023 20:12:36 +0000 Subject: [PATCH] Do not output enchanted item if enchantment is impossible (#3998) To Fix #3672 , I avoid displaying the output item if enchantment is not possible for various reason * Level of added enchantment is below the one from input item * Incompatible enchantment Co-authored-by: Araca <> Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/3998 Reviewed-by: the-real-herowl Co-authored-by: Araca Co-committed-by: Araca --- mods/ITEMS/mcl_enchanting/engine.lua | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/mods/ITEMS/mcl_enchanting/engine.lua b/mods/ITEMS/mcl_enchanting/engine.lua index b5766dd28..67ef72056 100644 --- a/mods/ITEMS/mcl_enchanting/engine.lua +++ b/mods/ITEMS/mcl_enchanting/engine.lua @@ -180,16 +180,18 @@ function mcl_enchanting.combine(itemstack, combine_with) return false end local enchantments = mcl_enchanting.get_enchantments(itemstack) + local any_new_enchantment = false for enchantment, combine_level in pairs(mcl_enchanting.get_enchantments(combine_with)) do local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_level = enchantments[enchantment] - if enchantment_level then + if enchantment_level then -- The enchantment already exist in the provided item if enchantment_level == combine_level then enchantment_level = math.min(enchantment_level + 1, enchantment_def.max_level) else enchantment_level = math.max(enchantment_level, combine_level) end - elseif mcl_enchanting.item_supports_enchantment(itemname, enchantment) then + any_new_enchantment = any_new_enchantment or ( enchantment_level ~= enchantments[enchantment] ) + elseif mcl_enchanting.item_supports_enchantment(itemname, enchantment) then -- this is a new enchantement to try to add local supported = true for incompatible in pairs(enchantment_def.incompatible) do if enchantments[incompatible] then @@ -199,24 +201,18 @@ function mcl_enchanting.combine(itemstack, combine_with) end if supported then enchantment_level = combine_level + any_new_enchantment = true end end if enchantment_level and enchantment_level > 0 then enchantments[enchantment] = enchantment_level end end - local any_enchantment = false - for enchantment, enchantment_level in pairs(enchantments) do - if enchantment_level > 0 then - any_enchantment = true - break - end - end - if any_enchantment then + if any_new_enchantment then itemstack:set_name(enchanted_itemname) + mcl_enchanting.set_enchantments(itemstack, enchantments) end - mcl_enchanting.set_enchantments(itemstack, enchantments) - return true + return any_new_enchantment end function mcl_enchanting.enchantments_snippet(_, _, itemstack)