From 378b8f8f6c181086a4f71cc1987028cdec2a4909 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Mon, 13 Nov 2023 13:35:12 +0100 Subject: [PATCH 1/2] Fix item not dropping at tool last use --- mods/ENTITIES/mcl_item_entity/init.lua | 113 +++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/mods/ENTITIES/mcl_item_entity/init.lua b/mods/ENTITIES/mcl_item_entity/init.lua index 149b5ed93..2feeb640c 100644 --- a/mods/ENTITIES/mcl_item_entity/init.lua +++ b/mods/ENTITIES/mcl_item_entity/init.lua @@ -362,6 +362,119 @@ function minetest.handle_node_drops(pos, drops, digger) end end +local function user_name(user) + return user and user:get_player_name() or "" +end + +-- Returns a logging function. For empty names, does not log. +local function make_log(name) + return name ~= "" and minetest.log or function() end +end + +function minetest.node_dig(pos, node, digger) + local diggername = user_name(digger) + local log = make_log(diggername) + local def = minetest.registered_nodes[node.name] + -- Copy pos because the callback could modify it + if def and (not def.diggable or + (def.can_dig and not def.can_dig(vector.copy(pos), digger))) then + log("info", diggername .. " tried to dig " + .. node.name .. " which is not diggable " + .. minetest.pos_to_string(pos)) + return false + end + + if minetest.is_protected(pos, diggername) then + log("action", diggername + .. " tried to dig " .. node.name + .. " at protected position " + .. minetest.pos_to_string(pos)) + minetest.record_protection_violation(pos, diggername) + return false + end + + log('action', diggername .. " digs " + .. node.name .. " at " .. minetest.pos_to_string(pos)) + + local wielded = digger and digger:get_wielded_item() + local drops = minetest.get_node_drops(node, wielded and wielded:get_name()) + + -- Check to see if metadata should be preserved. + if def and def.preserve_metadata then + local oldmeta = minetest.get_meta(pos):to_table().fields + -- Copy pos and node because the callback can modify them. + local pos_copy = vector.copy(pos) + local node_copy = {name=node.name, param1=node.param1, param2=node.param2} + local drop_stacks = {} + for k, v in pairs(drops) do + drop_stacks[k] = ItemStack(v) + end + drops = drop_stacks + def.preserve_metadata(pos_copy, node_copy, oldmeta, drops) + end + + -- Handle drops + minetest.handle_node_drops(pos, drops, digger) + + if wielded then + local wdef = wielded:get_definition() + local tp = wielded:get_tool_capabilities() + local dp = minetest.get_dig_params(def and def.groups, tp, wielded:get_wear()) + if wdef and wdef.after_use then + wielded = wdef.after_use(wielded, digger, node, dp) or wielded + else + -- Wear out tool + if not minetest.is_creative_enabled(diggername) then + wielded:add_wear(dp.wear) + if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then + minetest.sound_play(wdef.sound.breaks, { + pos = pos, + gain = 0.5 + }, true) + end + end + end + digger:set_wielded_item(wielded) + end + + local oldmetadata = nil + if def and def.after_dig_node then + oldmetadata = minetest.get_meta(pos):to_table() + end + + -- Remove node and update + minetest.remove_node(pos) + + -- Play sound if it was done by a player + if diggername ~= "" and def and def.sounds and def.sounds.dug then + minetest.sound_play(def.sounds.dug, { + pos = pos, + exclude_player = diggername, + }, true) + end + + -- Run callback + if def and def.after_dig_node then + -- Copy pos and node because callback can modify them + local pos_copy = vector.copy(pos) + local node_copy = {name=node.name, param1=node.param1, param2=node.param2} + def.after_dig_node(pos_copy, node_copy, oldmetadata, digger) + end + + -- Run script hook + for _, callback in ipairs(minetest.registered_on_dignodes) do + local origin = minetest.callback_origins[callback] + minetest.set_last_run_mod(origin.mod) + + -- Copy pos and node because callback can modify them + local pos_copy = vector.copy(pos) + local node_copy = {name=node.name, param1=node.param1, param2=node.param2} + callback(pos_copy, node_copy, digger) + end + + return true +end + -- Drop single items by default function minetest.item_drop(itemstack, dropper, pos) if dropper and dropper:is_player() then From e53d9ec8d73bb634bfbab6f9cc3bbc14159344e3 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sat, 18 Nov 2023 02:30:33 +0100 Subject: [PATCH 2/2] Added comments --- mods/ENTITIES/mcl_item_entity/init.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_item_entity/init.lua b/mods/ENTITIES/mcl_item_entity/init.lua index 2feeb640c..c12bd045e 100644 --- a/mods/ENTITIES/mcl_item_entity/init.lua +++ b/mods/ENTITIES/mcl_item_entity/init.lua @@ -362,15 +362,16 @@ function minetest.handle_node_drops(pos, drops, digger) end end +-- the following code is pulled from Minetest builtin without changes except for the call order being changed, +-- until a comment saying explicitly it's the end of such code +-- TODO if this gets a fix in the engine, remove the block of code local function user_name(user) return user and user:get_player_name() or "" end - -- Returns a logging function. For empty names, does not log. local function make_log(name) return name ~= "" and minetest.log or function() end end - function minetest.node_dig(pos, node, digger) local diggername = user_name(digger) local log = make_log(diggername) @@ -474,6 +475,7 @@ function minetest.node_dig(pos, node, digger) return true end +-- end of code pulled from Minetest -- Drop single items by default function minetest.item_drop(itemstack, dropper, pos)