diff options
author | sfan5 <sfan5@live.de> | 2021-10-25 20:30:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-25 20:30:27 +0200 |
commit | 660e63dbae5901f8178b39ab40e6f770b8d7a215 (patch) | |
tree | 6b17279e1583716684b9d97bb8d56c8e7b8655f4 /builtin/game | |
parent | d4b89eb106220f43838b039b13a0e356d366c259 (diff) | |
download | minetest-660e63dbae5901f8178b39ab40e6f770b8d7a215.tar.gz minetest-660e63dbae5901f8178b39ab40e6f770b8d7a215.tar.bz2 minetest-660e63dbae5901f8178b39ab40e6f770b8d7a215.zip |
Fix item duplication if player dies during interact callback (alternative) (#11662)
Diffstat (limited to 'builtin/game')
-rw-r--r-- | builtin/game/item.lua | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/builtin/game/item.lua b/builtin/game/item.lua index c495a67bd..039947584 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -499,34 +499,40 @@ function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed return result end end + if itemstack:take_item():is_empty() then + return itemstack + end + local def = itemstack:get_definition() - if itemstack:take_item() ~= nil then - user:set_hp(user:get_hp() + hp_change) - - if def and def.sound and def.sound.eat then - core.sound_play(def.sound.eat, { - pos = user:get_pos(), - max_hear_distance = 16 - }, true) - end + if def and def.sound and def.sound.eat then + core.sound_play(def.sound.eat, { + pos = user:get_pos(), + max_hear_distance = 16 + }, true) + end - if replace_with_item then - if itemstack:is_empty() then - itemstack:add_item(replace_with_item) + -- Changing hp might kill the player causing mods to do who-knows-what to the + -- inventory, so do this before set_hp(). + if replace_with_item then + if itemstack:is_empty() then + itemstack:add_item(replace_with_item) + else + local inv = user:get_inventory() + -- Check if inv is null, since non-players don't have one + if inv and inv:room_for_item("main", {name=replace_with_item}) then + inv:add_item("main", replace_with_item) else - local inv = user:get_inventory() - -- Check if inv is null, since non-players don't have one - if inv and inv:room_for_item("main", {name=replace_with_item}) then - inv:add_item("main", replace_with_item) - else - local pos = user:get_pos() - pos.y = math.floor(pos.y + 0.5) - core.add_item(pos, replace_with_item) - end + local pos = user:get_pos() + pos.y = math.floor(pos.y + 0.5) + core.add_item(pos, replace_with_item) end end end - return itemstack + user:set_wielded_item(itemstack) + + user:set_hp(user:get_hp() + hp_change) + + return nil -- don't overwrite wield item a second time end function core.item_eat(hp_change, replace_with_item) |