From b3d83bc953905190e60eaf60c1dba3cec3def1a7 Mon Sep 17 00:00:00 2001 From: Zefram Date: Wed, 30 Apr 2014 10:45:45 +0100 Subject: [PATCH] Support item-dependent refill behaviour The refill slot was only putting as many items as possible in a stack, which does nothing for tools. Tools could benefit from repair, recharging, or other behaviour depending on the type of tool. Change the default refill behaviour to repair mechanical wear as well as fully stacking. Because other kinds of refill will require knowledge of the metadata format, they can't be directly handled here. So add an on_refill hook, that tool definitions can supply to plug in appropriate behaviour. --- callbacks.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/callbacks.lua b/callbacks.lua index ce6bcf0..a1e2396 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -1,3 +1,11 @@ +local function default_refill(stack) + stack:set_count(stack:get_stack_max()) + local itemdef = minetest.registered_items[stack:get_name()] + if itemdef and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear" and stack:get_wear() ~= 0 then + stack:set_wear(0) + end + return stack +end minetest.register_on_joinplayer(function(player) local player_name = player:get_player_name() @@ -40,7 +48,8 @@ minetest.register_on_joinplayer(function(player) end, on_put = function(inv, listname, index, stack, player) local player_name = player:get_player_name() - stack:set_count(stack:get_stack_max()) + local handle_refill = (minetest.registered_items[stack:get_name()] or {}).on_refill or default_refill + stack = handle_refill(stack) inv:set_stack(listname, index, stack) minetest.sound_play("electricity", {to_player=player_name, gain = 1.0})