Add item eat callback

This commit is contained in:
rubenwardy 2014-05-20 15:21:09 +01:00 committed by ShadowNinja
parent ab75b1b923
commit 832d7973c8
3 changed files with 11 additions and 1 deletions

@ -353,6 +353,12 @@ end
function core.item_eat(hp_change, replace_with_item) function core.item_eat(hp_change, replace_with_item)
return function(itemstack, user, pointed_thing) -- closure return function(itemstack, user, pointed_thing) -- closure
for _, callback in pairs(core.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
if result then
return result
end
end
if itemstack:take_item() ~= nil then if itemstack:take_item() ~= nil then
user:set_hp(user:get_hp() + hp_change) user:set_hp(user:get_hp() + hp_change)
itemstack:add_item(replace_with_item) -- note: replace_with_item is optional itemstack:add_item(replace_with_item) -- note: replace_with_item is optional

@ -8,7 +8,7 @@ local register_item_raw = core.register_item_raw
core.register_item_raw = nil core.register_item_raw = nil
local register_alias_raw = core.register_alias_raw local register_alias_raw = core.register_alias_raw
core.register_item_raw = nil core.register_alias_raw = nil
-- --
-- Item / entity / ABM registration functions -- Item / entity / ABM registration functions
@ -407,4 +407,5 @@ core.registered_on_cheats, core.register_on_cheat = make_registration()
core.registered_on_crafts, core.register_on_craft = make_registration() core.registered_on_crafts, core.register_on_craft = make_registration()
core.registered_craft_predicts, core.register_craft_predict = make_registration() core.registered_craft_predicts, core.register_craft_predict = make_registration()
core.registered_on_protection_violation, core.register_on_protection_violation = make_registration() core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
core.registered_on_item_eats, core.register_on_item_eat = make_registration()

@ -1313,6 +1313,9 @@ minetest.register_on_protection_violation(func(pos, name))
^ The provided function should check that the position is protected by the mod ^ The provided function should check that the position is protected by the mod
calling this function before it prints a message, if it does, to allow for calling this function before it prints a message, if it does, to allow for
multiple protection mods. multiple protection mods.
minetest.register_on_item_eat(func(hp_change, replace_with_item, itemstack, user, pointed_thing))
^ Called when an item is eaten, by minetest.item_eat
^ Return true or itemstack to cancel the default item eat response (ie: hp increase)
Other registration functions: Other registration functions:
minetest.register_chatcommand(cmd, chatcommand definition) minetest.register_chatcommand(cmd, chatcommand definition)