Fix minetest.item_eat's replace_with_item, fixes #2292

This commit is contained in:
rubenwardy 2015-02-12 16:57:22 +00:00 committed by Loic Blot
parent 8aebc31a17
commit efa977518a
2 changed files with 49 additions and 25 deletions

@ -357,8 +357,7 @@ function core.item_drop(itemstack, dropper, pos)
return itemstack return itemstack
end end
function core.item_eat(hp_change, replace_with_item) function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
return function(itemstack, user, pointed_thing) -- closure
for _, callback in pairs(core.registered_on_item_eats) do for _, callback in pairs(core.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing) local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
if result then if result then
@ -367,10 +366,29 @@ function core.item_eat(hp_change, replace_with_item)
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
if replace_with_item then
if itemstack:is_empty() then
itemstack:add_item(replace_with_item)
else
local inv = user:get_inventory()
if inv:room_for_item("main", {name=replace_with_item}) then
inv:add_item("main", replace_with_item)
else
local pos = user:getpos()
pos.y = math.floor(pos.y + 0.5)
core.add_item(pos, replace_with_item)
end
end
end
end end
return itemstack return itemstack
end end
function core.item_eat(hp_change, replace_with_item)
return function(itemstack, user, pointed_thing) -- closure
return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
end
end end
function core.node_punch(pos, node, puncher, pointed_thing) function core.node_punch(pos, node, puncher, pointed_thing)

@ -1926,6 +1926,8 @@ and `minetest.auth_reload` call the authetification handler.
* `minetest.create_detached_inventory(name, callbacks)`: returns an `InvRef` * `minetest.create_detached_inventory(name, callbacks)`: returns an `InvRef`
* callbacks: See "Detached inventory callbacks" * callbacks: See "Detached inventory callbacks"
* Creates a detached inventory. If it already exists, it is cleared. * Creates a detached inventory. If it already exists, it is cleared.
* `minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)`: returns left over ItemStack
* See `minetest.item_eat` and `minetest.register_on_item_eat`
### Formspec ### Formspec
* `minetest.show_formspec(playername, formname, formspec)` * `minetest.show_formspec(playername, formname, formspec)`
@ -2037,7 +2039,11 @@ These functions return the leftover itemstack.
* `minetest.item_drop(itemstack, dropper, pos)` * `minetest.item_drop(itemstack, dropper, pos)`
* Drop the item * Drop the item
* `minetest.item_eat(hp_change, replace_with_item)` * `minetest.item_eat(hp_change, replace_with_item)`
* Eat the item. `replace_with_item` can be `nil`. * Eat the item.
* `replace_with_item` is the itemstring which is added to the inventory.
If the player is eating a stack, then replace_with_item goes to a
different spot. Can be `nil`
* See `minetest.do_item_eat`
### Defaults for the `on_punch` and `on_dig` node definition callbacks ### Defaults for the `on_punch` and `on_dig` node definition callbacks
* `minetest.node_punch(pos, node, puncher, pointed_thing)` * `minetest.node_punch(pos, node, puncher, pointed_thing)`