Fix tools not taking wear when rightclicking

Added the API function mcl_autogroup.get_wear which is used to get the
tool wear for digging a node of a group.  This is used by mcl_tools to
compute the wear of shovels and shears when rightclicking to create
grass paths and carve pumpkins.
This commit is contained in:
Elias Åström 2021-03-18 11:35:29 +01:00
parent ba0a09243b
commit c92f0e5ce3
2 changed files with 28 additions and 41 deletions

View File

@ -49,6 +49,9 @@ mcl_autogroup contains the API functions used to register custom digging groups.
_mcl_autogroup contains most of the code. The leading underscore in the name
"_mcl_autogroup" is used to force Minetest to load that part of the mod as late
as possible. Minetest loads mods in reverse alphabetical order.
This also means that it is very important that no mod adds _mcl_autogroups as a
dependency.
--]]
-- Returns a table containing the unique "_mcl_hardness" for nodes belonging to
@ -233,21 +236,36 @@ end
-- or in the metadata of an enchanted tool.
--
-- Parameters:
-- tool_name - Name of the tool being enchanted (like "mcl_tools:diamond_pickaxe")
-- toolname - Name of the tool being enchanted (like "mcl_tools:diamond_pickaxe")
-- efficiency - The efficiency level the tool is enchanted with (default 0)
--
-- NOTE:
-- Mods calling this function (like mcl_enchanting) should _not_ have
-- _mcl_autogroups as a dependency. It is very important that this mod is
-- loaded last. This also means this function can only be called by other mods
-- after all mods have been initialized.
function mcl_autogroup.get_groupcaps(tool_name, efficiency)
local tdef = minetest.registered_tools[tool_name]
-- This function can only be called after mod initialization. Otherwise a mod
-- would have to add _mcl_autogroup as a dependency which would break the mod
-- loading order.
function mcl_autogroup.get_groupcaps(toolname, efficiency)
local tdef = minetest.registered_tools[toolname]
local groupcaps = table.copy(tdef.tool_capabilities.groupcaps or {})
add_groupcaps(groupcaps, tdef._mcl_autogroup_groupcaps, efficiency)
return groupcaps
end
-- Get the wear from using a tool on a digging group.
--
-- Parameters
-- toolname - Name of the tool used
-- diggroup - The name of the diggroup the tool is used on
--
-- NOTE:
-- This function can only be called after mod initialization. Otherwise a mod
-- would have to add _mcl_autogroup as a dependency which would break the mod
-- loading order.
function mcl_autogroup.get_wear(toolname, diggroup)
local tdef = minetest.registered_tools[toolname]
local uses = tdef._mcl_autogroup_groupcaps[diggroup].uses
return math.ceil(65535 / uses)
end
local overwrite = function()
for nname, ndef in pairs(minetest.registered_nodes) do
local newgroups = table.copy(ndef.groups)

View File

@ -179,26 +179,6 @@ minetest.register_tool("mcl_tools:pick_diamond", {
},
})
local get_shovel_dig_group = function(itemstack)
local itemstring = itemstack:get_name()
local efficiency_level = mcl_enchanting.get_enchantment(itemstack, "efficiency")
local postfix = efficiency_level > 0 and "_efficiency_" .. efficiency_level or ""
if itemstring:find("mcl_tools:shovel_wood") == 1 then
return "shovely_dig_wood" .. postfix
elseif itemstring:find("mcl_tools:shovel_stone") == 1 then
return "shovely_dig_stone" .. postfix
elseif itemstring:find("mcl_tools:shovel_iron") == 1 then
return "shovely_dig_iron" .. postfix
elseif itemstring:find("mcl_tools:shovel_gold") == 1 then
return "shovely_dig_gold" .. postfix
elseif itemstring:find("mcl_tools:shovel_diamond") == 1 then
return "shovely_dig_diamond" .. postfix
else
-- Fallback
return "shovely_dig_wood"
end
end
local make_grass_path = function(itemstack, placer, pointed_thing)
-- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under)
@ -223,15 +203,9 @@ local make_grass_path = function(itemstack, placer, pointed_thing)
end
if not minetest.is_creative_enabled(placer:get_player_name()) then
-- Add wear, as if digging a level 0 shovely node
-- Add wear (as if digging a shovely node)
local toolname = itemstack:get_name()
local def = minetest.registered_items[toolname]
local group = get_shovel_dig_group(itemstack)
local toolcaps = itemstack:get_tool_capabilities()
local base_uses = toolcaps.groupcaps[group].uses
local maxlevel = toolcaps.groupcaps[group].maxlevel
local uses = base_uses * math.pow(3, maxlevel)
local wear = math.ceil(65535 / uses)
local wear = mcl_autogroup.get_wear(toolname, "shovely")
itemstack:add_wear(wear)
end
minetest.sound_play({name="default_grass_footstep", gain=1}, {pos = above}, true)
@ -260,12 +234,7 @@ if minetest.get_modpath("mcl_farming") then
if not minetest.is_creative_enabled(placer:get_player_name()) then
-- Add wear (as if digging a shearsy node)
local toolname = itemstack:get_name()
local def = minetest.registered_items[toolname]
local group = get_shovel_dig_group(toolname)
local base_uses = def.tool_capabilities.groupcaps["shearsy_dig"].uses
local maxlevel = def.tool_capabilities.groupcaps["shearsy_dig"].maxlevel
local uses = base_uses * math.pow(3, maxlevel)
local wear = math.ceil(65535 / uses)
local wear = mcl_autogroup.get_wear(toolname, "shearsy")
itemstack:add_wear(wear)
end
minetest.sound_play({name="default_grass_footstep", gain=1}, {pos = above}, true)