1
0
Fork 0

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 <the-real-herowl@noreply.git.minetest.land>
Co-authored-by: Araca <araca.prod@gmail.com>
Co-committed-by: Araca <araca.prod@gmail.com>
hollow_logs_ref_recovery
Araca 2023-11-06 20:12:36 +00:00 committed by the-real-herowl
parent 5a069af072
commit 52b65554d9
1 changed files with 8 additions and 12 deletions

View File

@ -180,16 +180,18 @@ function mcl_enchanting.combine(itemstack, combine_with)
return false return false
end end
local enchantments = mcl_enchanting.get_enchantments(itemstack) 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 for enchantment, combine_level in pairs(mcl_enchanting.get_enchantments(combine_with)) do
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
local enchantment_level = 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 if enchantment_level == combine_level then
enchantment_level = math.min(enchantment_level + 1, enchantment_def.max_level) enchantment_level = math.min(enchantment_level + 1, enchantment_def.max_level)
else else
enchantment_level = math.max(enchantment_level, combine_level) enchantment_level = math.max(enchantment_level, combine_level)
end 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 local supported = true
for incompatible in pairs(enchantment_def.incompatible) do for incompatible in pairs(enchantment_def.incompatible) do
if enchantments[incompatible] then if enchantments[incompatible] then
@ -199,24 +201,18 @@ function mcl_enchanting.combine(itemstack, combine_with)
end end
if supported then if supported then
enchantment_level = combine_level enchantment_level = combine_level
any_new_enchantment = true
end end
end end
if enchantment_level and enchantment_level > 0 then if enchantment_level and enchantment_level > 0 then
enchantments[enchantment] = enchantment_level enchantments[enchantment] = enchantment_level
end end
end end
local any_enchantment = false if any_new_enchantment then
for enchantment, enchantment_level in pairs(enchantments) do
if enchantment_level > 0 then
any_enchantment = true
break
end
end
if any_enchantment then
itemstack:set_name(enchanted_itemname) itemstack:set_name(enchanted_itemname)
end
mcl_enchanting.set_enchantments(itemstack, enchantments) mcl_enchanting.set_enchantments(itemstack, enchantments)
return true end
return any_new_enchantment
end end
function mcl_enchanting.enchantments_snippet(_, _, itemstack) function mcl_enchanting.enchantments_snippet(_, _, itemstack)