Move efficiency code to its own file
This commit is contained in:
parent
5193730652
commit
2e9b3c2259
46
mods/ITEMS/mcl_enchanting/efficiency.lua
Normal file
46
mods/ITEMS/mcl_enchanting/efficiency.lua
Normal file
|
@ -0,0 +1,46 @@
|
|||
local efficiency_cache_table = {}
|
||||
|
||||
-- Get the efficiency groupcaps and hash for a tool and efficiency level. If
|
||||
-- this function is called repeatedly with the same values it will return data
|
||||
-- from a cache.
|
||||
--
|
||||
-- Returns a table with the following two fields:
|
||||
-- values - the groupcaps table
|
||||
-- hash - the hash of the groupcaps table
|
||||
local function get_efficiency_groupcaps(toolname, level)
|
||||
local toolcache = efficiency_cache_table[toolname]
|
||||
if not toolcache then
|
||||
toolcache = {}
|
||||
efficiency_cache_table[toolname] = toolcache
|
||||
end
|
||||
|
||||
local levelcache = toolcache[level]
|
||||
if not levelcache then
|
||||
levelcache = {}
|
||||
levelcache.values = mcl_autogroup.get_groupcaps(toolname, level)
|
||||
levelcache.hash = mcl_util.hash(levelcache.values)
|
||||
toolcache[level] = levelcache
|
||||
end
|
||||
|
||||
return levelcache
|
||||
end
|
||||
|
||||
-- Apply efficiency enchantment to a tool. This will update the tools
|
||||
-- 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
|
||||
-- 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.
|
||||
function mcl_enchanting.apply_efficiency(itemstack, level)
|
||||
local name = itemstack:get_name()
|
||||
local groupcaps = get_efficiency_groupcaps(name, level)
|
||||
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
|
||||
itemstack:get_meta():set_tool_capabilities(tool_capabilities)
|
||||
itemstack:get_meta():set_string("groupcaps_hash", groupcaps.hash)
|
||||
end
|
||||
end
|
|
@ -1,53 +1,6 @@
|
|||
local S = minetest.get_translator("mcl_enchanting")
|
||||
local F = minetest.formspec_escape
|
||||
|
||||
local efficiency_cache_table = {}
|
||||
|
||||
-- Get the efficiency groupcaps and hash for a tool and efficiency level. If
|
||||
-- this function is called repeatedly with the same values it will return data
|
||||
-- from a cache.
|
||||
--
|
||||
-- Returns a table with the following two fields:
|
||||
-- values - the groupcaps table
|
||||
-- hash - the hash of the groupcaps table
|
||||
local function get_efficiency_groupcaps(toolname, level)
|
||||
local toolcache = efficiency_cache_table[toolname]
|
||||
if not toolcache then
|
||||
toolcache = {}
|
||||
efficiency_cache_table[toolname] = toolcache
|
||||
end
|
||||
|
||||
local levelcache = toolcache[level]
|
||||
if not levelcache then
|
||||
levelcache = {}
|
||||
levelcache.values = mcl_autogroup.get_groupcaps(toolname, level)
|
||||
levelcache.hash = mcl_util.hash(levelcache.values)
|
||||
toolcache[level] = levelcache
|
||||
end
|
||||
|
||||
return levelcache
|
||||
end
|
||||
|
||||
-- Apply efficiency enchantment to a tool. This will update the tools
|
||||
-- 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
|
||||
-- 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.
|
||||
function mcl_enchanting.apply_efficiency(itemstack, level)
|
||||
local name = itemstack:get_name()
|
||||
local groupcaps = get_efficiency_groupcaps(name, level)
|
||||
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
|
||||
itemstack:get_meta():set_tool_capabilities(tool_capabilities)
|
||||
itemstack:get_meta():set_string("groupcaps_hash", groupcaps.hash)
|
||||
end
|
||||
end
|
||||
|
||||
function mcl_enchanting.is_book(itemname)
|
||||
return itemname == "mcl_books:book" or itemname == "mcl_enchanting:book_enchanted" or itemname == "mcl_books:book_enchanted"
|
||||
end
|
||||
|
@ -286,7 +239,7 @@ local function get_after_use_callback(itemdef)
|
|||
local enchantments = mcl_enchanting.get_enchantments(itemstack)
|
||||
local level = enchantments.efficiency
|
||||
if level then
|
||||
mcl_enchanting.enchantments.efficiency.on_enchant(itemstack, level)
|
||||
mcl_enchanting.apply_efficiency(itemstack, level)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -59,6 +59,7 @@ mcl_enchanting = {
|
|||
}
|
||||
|
||||
dofile(modpath .. "/engine.lua")
|
||||
dofile(modpath .. "/efficiency.lua")
|
||||
dofile(modpath .. "/enchantments.lua")
|
||||
|
||||
minetest.register_chatcommand("enchant", {
|
||||
|
|
Loading…
Reference in New Issue
Block a user