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.
This commit is contained in:
Elias Åström 2021-04-17 23:03:57 +02:00
parent bcd058feb1
commit dd69dcfd9f
3 changed files with 14 additions and 5 deletions

@ -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,

@ -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

@ -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