mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 10:03:45 +01:00
Fix minetest.item_eat's replace_with_item, fixes #2292
This commit is contained in:
parent
8aebc31a17
commit
efa977518a
@ -357,19 +357,37 @@ function core.item_drop(itemstack, dropper, pos)
|
||||
return itemstack
|
||||
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
|
||||
function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
|
||||
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)
|
||||
|
||||
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
|
||||
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
|
||||
end
|
||||
return itemstack
|
||||
end
|
||||
return itemstack
|
||||
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
|
||||
|
||||
|
@ -1926,6 +1926,8 @@ and `minetest.auth_reload` call the authetification handler.
|
||||
* `minetest.create_detached_inventory(name, callbacks)`: returns an `InvRef`
|
||||
* callbacks: See "Detached inventory callbacks"
|
||||
* 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
|
||||
* `minetest.show_formspec(playername, formname, formspec)`
|
||||
@ -2037,7 +2039,11 @@ These functions return the leftover itemstack.
|
||||
* `minetest.item_drop(itemstack, dropper, pos)`
|
||||
* Drop the 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
|
||||
* `minetest.node_punch(pos, node, puncher, pointed_thing)`
|
||||
|
Loading…
Reference in New Issue
Block a user