Do not include unnecessary tool_capabilities

This commit makes enchanted tools which have no use for
tool_capabilities to not include it in their metadata.  It does this by
not including tool_capabilities in the metadata of an enchanted tool if
at least one of two cases is true:

(1) The tool is not enchanted with unbreaking or efficiency
(2) The tool does not have tool_capabilities defined in its definition

The first case covers situations like having a pickaxe only being
enchanted with silk_touch.  The second case covers situations like a
piece of armor being enchanted with unbreaking.
This commit is contained in:
Elias Åström 2021-04-17 23:40:19 +02:00
parent d6e1fe42d1
commit 1c6d3c2fea
3 changed files with 13 additions and 4 deletions

View File

@ -770,6 +770,11 @@ mcl_enchanting.enchantments.unbreaking = {
description = S("Increases item durability."), description = S("Increases item durability."),
curse = false, curse = false,
on_enchant = function(itemstack, level) on_enchant = function(itemstack, level)
local name = itemstack:get_name()
if not minetest.registered_tools[name].tool_capabilities then
return
end
local tool_capabilities = itemstack:get_tool_capabilities() local tool_capabilities = itemstack:get_tool_capabilities()
tool_capabilities.punch_attack_uses = tool_capabilities.punch_attack_uses * (1 + level) tool_capabilities.punch_attack_uses = tool_capabilities.punch_attack_uses * (1 + level)
itemstack:get_meta():set_tool_capabilities(tool_capabilities) itemstack:get_meta():set_tool_capabilities(tool_capabilities)

View File

@ -12,7 +12,7 @@ end
function mcl_enchanting.unload_enchantments(itemstack) function mcl_enchanting.unload_enchantments(itemstack)
local itemdef = itemstack:get_definition() local itemdef = itemstack:get_definition()
if itemdef.tool_capabilities then if itemdef.tool_capabilities then
itemstack:get_meta():set_tool_capabilities(itemdef.tool_capabilities) itemstack:get_meta():set_tool_capabilities(nil)
end end
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
if meta:get_string("name") == "" then if meta:get_string("name") == "" then

View File

@ -45,12 +45,17 @@ end
-- To make it more efficient it will first check a hash value to determine if -- To make it more efficient it will first check a hash value to determine if
-- the tool needs to be updated. -- the tool needs to be updated.
function mcl_enchanting.update_groupcaps(itemstack) function mcl_enchanting.update_groupcaps(itemstack)
if not itemstack:get_meta():get("tool_capabilities") then local name = itemstack:get_name()
if not minetest.registered_tools[name].tool_capabilities then
return return
end end
local name = itemstack:get_name()
local efficiency = mcl_enchanting.get_enchantment(itemstack, "efficiency") local efficiency = mcl_enchanting.get_enchantment(itemstack, "efficiency")
local unbreaking = mcl_enchanting.get_enchantment(itemstack, "unbreaking")
if unbreaking == 0 and efficiency == 0 then
return
end
local groupcaps = get_efficiency_groupcaps(name, efficiency) local groupcaps = get_efficiency_groupcaps(name, efficiency)
local hash = itemstack:get_meta():get_string("groupcaps_hash") local hash = itemstack:get_meta():get_string("groupcaps_hash")
@ -60,7 +65,6 @@ function mcl_enchanting.update_groupcaps(itemstack)
-- Increase the number of uses depending on the unbreaking level -- Increase the number of uses depending on the unbreaking level
-- of the tool. -- of the tool.
local unbreaking = mcl_enchanting.get_enchantment(itemstack, "unbreaking")
for group, capability in pairs(tool_capabilities.groupcaps) do for group, capability in pairs(tool_capabilities.groupcaps) do
capability.uses = capability.uses * (1 + unbreaking) capability.uses = capability.uses * (1 + unbreaking)
end end