Add item eat callback
authorrubenwardy <rubenwardy@gmail.com>
Tue, 20 May 2014 14:21:09 +0000 (15:21 +0100)
committerShadowNinja <shadowninja@minetest.net>
Mon, 26 May 2014 22:41:58 +0000 (18:41 -0400)
builtin/game/item.lua
builtin/game/register.lua
doc/lua_api.txt

index 8fd172aab6d8cbcc5a84bb27e2930dae1d411fcb..11a36bdc3e8653b0842315d5177910b92dcfd7c3 100644 (file)
@@ -353,6 +353,12 @@ end
 
 function core.item_eat(hp_change, replace_with_item)
        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
                        user:set_hp(user:get_hp() + hp_change)
                        itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
index 8908f51f67ffdcb51d2f3b96bb5be34c9a380a1c..1c9e624221c51cbedf33a115db16479e7c50c5ee 100644 (file)
@@ -8,7 +8,7 @@ local register_item_raw = core.register_item_raw
 core.register_item_raw = nil
 
 local register_alias_raw = core.register_alias_raw
-core.register_item_raw = nil
+core.register_alias_raw = nil
 
 --
 -- 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_craft_predicts, core.register_craft_predict = 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()
 
index 164aa393e9ffbb037fdada0c105a76414ab1201c..803314925597c17087c231f60bb5a81598fcd5f1 100644 (file)
@@ -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
   calling this function before it prints a message, if it does, to allow for
   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:
 minetest.register_chatcommand(cmd, chatcommand definition)