From dd69dcfd9f5fe5c8e481e275536f130853fc9e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20=C3=85str=C3=B6m?= Date: Sat, 17 Apr 2021 23:03:57 +0200 Subject: [PATCH] Fix efficiency and unbreaking not working together This commit fixes an issue were tools enchanted with both efficiency and unbreaking would loose the effect of one of the enchantments in some conditions. --- mods/ITEMS/mcl_enchanting/enchantments.lua | 6 +++--- mods/ITEMS/mcl_enchanting/engine.lua | 1 + mods/ITEMS/mcl_enchanting/groupcaps.lua | 12 ++++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/mods/ITEMS/mcl_enchanting/enchantments.lua b/mods/ITEMS/mcl_enchanting/enchantments.lua index ca936c31..df6105d5 100644 --- a/mods/ITEMS/mcl_enchanting/enchantments.lua +++ b/mods/ITEMS/mcl_enchanting/enchantments.lua @@ -771,11 +771,11 @@ mcl_enchanting.enchantments.unbreaking = { curse = false, on_enchant = function(itemstack, level) local tool_capabilities = itemstack:get_tool_capabilities() - for group, capability in pairs(tool_capabilities.groupcaps) do - capability.uses = capability.uses * (1 + level) - end tool_capabilities.punch_attack_uses = tool_capabilities.punch_attack_uses * (1 + level) itemstack:get_meta():set_tool_capabilities(tool_capabilities) + + -- Unbreaking for groupcaps is handled in this function. + mcl_enchanting.update_groupcaps(itemstack) end, requires_tool = true, treasure = false, diff --git a/mods/ITEMS/mcl_enchanting/engine.lua b/mods/ITEMS/mcl_enchanting/engine.lua index ea69d186..30e4a9a8 100644 --- a/mods/ITEMS/mcl_enchanting/engine.lua +++ b/mods/ITEMS/mcl_enchanting/engine.lua @@ -17,6 +17,7 @@ function mcl_enchanting.unload_enchantments(itemstack) local meta = itemstack:get_meta() if meta:get_string("name") == "" then meta:set_string("description", "") + meta:set_string("groupcaps_hash", "") end end diff --git a/mods/ITEMS/mcl_enchanting/groupcaps.lua b/mods/ITEMS/mcl_enchanting/groupcaps.lua index 37502954..1a4f8fd1 100644 --- a/mods/ITEMS/mcl_enchanting/groupcaps.lua +++ b/mods/ITEMS/mcl_enchanting/groupcaps.lua @@ -50,13 +50,21 @@ function mcl_enchanting.update_groupcaps(itemstack) end local name = itemstack:get_name() - local level = mcl_enchanting.get_enchantment(itemstack, "efficiency") - local groupcaps = get_efficiency_groupcaps(name, level) + local efficiency = mcl_enchanting.get_enchantment(itemstack, "efficiency") + local groupcaps = get_efficiency_groupcaps(name, efficiency) local hash = itemstack:get_meta():get_string("groupcaps_hash") if not hash or hash ~= groupcaps.hash then local tool_capabilities = itemstack:get_tool_capabilities() tool_capabilities.groupcaps = groupcaps.values + + -- Increase the number of uses depending on the unbreaking level + -- of the tool. + local unbreaking = mcl_enchanting.get_enchantment(itemstack, "unbreaking") + for group, capability in pairs(tool_capabilities.groupcaps) do + capability.uses = capability.uses * (1 + unbreaking) + end + itemstack:get_meta():set_tool_capabilities(tool_capabilities) itemstack:get_meta():set_string("groupcaps_hash", groupcaps.hash) end