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.
This commit is contained in:
Zefram 2014-04-30 10:45:45 +01:00
parent d1e554b73d
commit b3d83bc953

View File

@ -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) minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
@ -40,7 +48,8 @@ minetest.register_on_joinplayer(function(player)
end, end,
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
local player_name = player:get_player_name() 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) inv:set_stack(listname, index, stack)
minetest.sound_play("electricity", minetest.sound_play("electricity",
{to_player=player_name, gain = 1.0}) {to_player=player_name, gain = 1.0})