Update the groupcaps of all enchanted tools

Not just those enchanted with efficiency.
This commit is contained in:
Elias Åström 2021-03-17 17:39:15 +01:00
parent 4c46eb2b4b
commit 8f9650abe4
4 changed files with 20 additions and 23 deletions

View File

@ -155,7 +155,7 @@ mcl_enchanting.enchantments.efficiency = {
description = S("Increases mining speed."), description = S("Increases mining speed."),
curse = false, curse = false,
on_enchant = function(itemstack, level) on_enchant = function(itemstack, level)
mcl_enchanting.apply_efficiency(itemstack, level) mcl_enchanting.update_groupcaps(itemstack)
end, end,
requires_tool = false, requires_tool = false,
treasure = false, treasure = false,

View File

@ -235,12 +235,7 @@ local function get_after_use_callback(itemdef)
-- one too. -- one too.
return function(itemstack, user, node, digparams) return function(itemstack, user, node, digparams)
itemdef.after_use(itemstack, user, node, digparams) itemdef.after_use(itemstack, user, node, digparams)
mcl_enchanting.update_groupcaps(itemstack)
local enchantments = mcl_enchanting.get_enchantments(itemstack)
local level = enchantments.efficiency
if level then
mcl_enchanting.apply_efficiency(itemstack, level)
end
end end
end end
@ -252,10 +247,7 @@ local function get_after_use_callback(itemdef)
end end
local enchantments = mcl_enchanting.get_enchantments(itemstack) local enchantments = mcl_enchanting.get_enchantments(itemstack)
local level = enchantments.efficiency mcl_enchanting.update_groupcaps(itemstack)
if level then
mcl_enchanting.enchantments.efficiency.on_enchant(itemstack, level)
end
end end
end end

View File

@ -1,4 +1,4 @@
local efficiency_cache_table = {} local groupcaps_cache = {}
-- Compute a hash value. -- Compute a hash value.
function compute_hash(value) function compute_hash(value)
@ -8,18 +8,23 @@ function compute_hash(value)
return string.sub(minetest.get_password_hash("ryvnf", minetest.serialize(value)), 1, 8) return string.sub(minetest.get_password_hash("ryvnf", minetest.serialize(value)), 1, 8)
end end
-- Get the efficiency groupcaps and hash for a tool and efficiency level. If -- Get the groupcaps and hash for an enchanted tool. If this function is called
-- this function is called repeatedly with the same values it will return data -- repeatedly with the same values it will return data from a cache.
-- from a cache. --
-- Parameters:
-- toolname - Name of the tool
-- level - The efficiency level of the tool
-- --
-- Returns a table with the following two fields: -- Returns a table with the following two fields:
-- values - the groupcaps table -- values - The groupcaps table
-- hash - the hash of the groupcaps table -- hash - The hash of the groupcaps table
local function get_efficiency_groupcaps(toolname, level) local function get_efficiency_groupcaps(toolname, level)
local toolcache = efficiency_cache_table[toolname] local toolcache = groupcaps_cache[toolname]
local level = level
if not toolcache then if not toolcache then
toolcache = {} toolcache = {}
efficiency_cache_table[toolname] = toolcache groupcaps_cache[toolname] = toolcache
end end
local levelcache = toolcache[level] local levelcache = toolcache[level]
@ -33,15 +38,15 @@ local function get_efficiency_groupcaps(toolname, level)
return levelcache return levelcache
end end
-- Apply efficiency enchantment to a tool. This will update the tools -- Update groupcaps of an enchanted tool. This function will be called
-- tool_capabilities to give it new digging times. This function will be called
-- repeatedly to make sure the digging times stored in groupcaps stays in sync -- repeatedly to make sure the digging times stored in groupcaps stays in sync
-- when the digging times of nodes can change. -- when the digging times of nodes can change.
-- --
-- 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.apply_efficiency(itemstack, level) function mcl_enchanting.update_groupcaps(itemstack)
local name = itemstack:get_name() local name = itemstack:get_name()
local level = mcl_enchanting.get_enchantment(itemstack, "efficiency")
local groupcaps = get_efficiency_groupcaps(name, level) local groupcaps = get_efficiency_groupcaps(name, level)
local hash = itemstack:get_meta():get_string("groupcaps_hash") local hash = itemstack:get_meta():get_string("groupcaps_hash")

View File

@ -59,7 +59,7 @@ mcl_enchanting = {
} }
dofile(modpath .. "/engine.lua") dofile(modpath .. "/engine.lua")
dofile(modpath .. "/efficiency.lua") dofile(modpath .. "/groupcaps.lua")
dofile(modpath .. "/enchantments.lua") dofile(modpath .. "/enchantments.lua")
minetest.register_chatcommand("enchant", { minetest.register_chatcommand("enchant", {