2021-03-17 17:39:15 +01:00
|
|
|
local groupcaps_cache = {}
|
2021-03-16 12:21:42 +01:00
|
|
|
|
2021-03-16 16:57:50 +01:00
|
|
|
-- Compute a hash value.
|
|
|
|
function compute_hash(value)
|
2022-02-12 20:08:03 +01:00
|
|
|
return string.sub(minetest.sha1(minetest.serialize(value)), 1, 8)
|
2021-03-16 16:57:50 +01:00
|
|
|
end
|
|
|
|
|
2021-03-17 17:39:15 +01:00
|
|
|
-- Get the groupcaps and hash for an enchanted tool. If this function is called
|
|
|
|
-- repeatedly with the same values it will return data from a cache.
|
|
|
|
--
|
|
|
|
-- Parameters:
|
|
|
|
-- toolname - Name of the tool
|
|
|
|
-- level - The efficiency level of the tool
|
2021-03-16 12:21:42 +01:00
|
|
|
--
|
|
|
|
-- Returns a table with the following two fields:
|
2021-03-17 17:39:15 +01:00
|
|
|
-- values - The groupcaps table
|
|
|
|
-- hash - The hash of the groupcaps table
|
2021-03-16 12:21:42 +01:00
|
|
|
local function get_efficiency_groupcaps(toolname, level)
|
2021-03-17 17:39:15 +01:00
|
|
|
local toolcache = groupcaps_cache[toolname]
|
|
|
|
local level = level
|
|
|
|
|
2021-03-16 12:21:42 +01:00
|
|
|
if not toolcache then
|
|
|
|
toolcache = {}
|
2021-03-17 17:39:15 +01:00
|
|
|
groupcaps_cache[toolname] = toolcache
|
2021-03-16 12:21:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local levelcache = toolcache[level]
|
|
|
|
if not levelcache then
|
|
|
|
levelcache = {}
|
|
|
|
levelcache.values = mcl_autogroup.get_groupcaps(toolname, level)
|
2021-03-16 16:57:50 +01:00
|
|
|
levelcache.hash = compute_hash(levelcache.values)
|
2021-03-16 12:21:42 +01:00
|
|
|
toolcache[level] = levelcache
|
|
|
|
end
|
|
|
|
|
|
|
|
return levelcache
|
|
|
|
end
|
|
|
|
|
2021-03-17 17:39:15 +01:00
|
|
|
-- Update groupcaps of an enchanted tool. This function will be called
|
2021-03-16 12:21:42 +01:00
|
|
|
-- repeatedly to make sure the digging times stored in groupcaps stays in sync
|
|
|
|
-- when the digging times of nodes can change.
|
|
|
|
--
|
|
|
|
-- To make it more efficient it will first check a hash value to determine if
|
|
|
|
-- the tool needs to be updated.
|
2021-03-17 17:39:15 +01:00
|
|
|
function mcl_enchanting.update_groupcaps(itemstack)
|
2021-04-17 23:40:19 +02:00
|
|
|
local name = itemstack:get_name()
|
|
|
|
if not minetest.registered_tools[name].tool_capabilities then
|
2021-03-21 12:18:24 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-04-17 23:03:57 +02:00
|
|
|
local efficiency = mcl_enchanting.get_enchantment(itemstack, "efficiency")
|
2021-04-17 23:40:19 +02:00
|
|
|
local unbreaking = mcl_enchanting.get_enchantment(itemstack, "unbreaking")
|
|
|
|
if unbreaking == 0 and efficiency == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-04-17 23:03:57 +02:00
|
|
|
local groupcaps = get_efficiency_groupcaps(name, efficiency)
|
2021-03-16 12:21:42 +01:00
|
|
|
local hash = itemstack:get_meta():get_string("groupcaps_hash")
|
|
|
|
|
|
|
|
if not hash or hash ~= groupcaps.hash then
|
|
|
|
local tool_capabilities = itemstack:get_tool_capabilities()
|
2021-04-23 15:49:37 +02:00
|
|
|
tool_capabilities.groupcaps = table.copy(groupcaps.values)
|
2021-04-17 23:03:57 +02:00
|
|
|
|
|
|
|
-- Increase the number of uses depending on the unbreaking level
|
|
|
|
-- of the tool.
|
|
|
|
for group, capability in pairs(tool_capabilities.groupcaps) do
|
|
|
|
capability.uses = capability.uses * (1 + unbreaking)
|
|
|
|
end
|
|
|
|
|
2021-03-16 12:21:42 +01:00
|
|
|
itemstack:get_meta():set_tool_capabilities(tool_capabilities)
|
|
|
|
itemstack:get_meta():set_string("groupcaps_hash", groupcaps.hash)
|
|
|
|
end
|
|
|
|
end
|