]> git.lizzy.rs Git - minetest.git/commitdiff
Fix core.item_eat for same-item replace_with_item and split stacks before dropping...
authorDS <ds.desour@proton.me>
Mon, 27 Mar 2023 18:02:23 +0000 (20:02 +0200)
committerGitHub <noreply@github.com>
Mon, 27 Mar 2023 18:02:23 +0000 (20:02 +0200)
The replace_with_item can be added to the slot of the wield item, which
is afterwards overwritten. This causes item loss.

builtin/game/item.lua
doc/lua_api.txt

index 71126b0a7094b6ca703194b79f973f4dbb44fd86..17d081f3d3fe1bac39db61d1f15ff9e13d448906 100644 (file)
@@ -390,22 +390,20 @@ function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed
 
        -- 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 pos = user:get_pos()
-                               pos.y = math.floor(pos.y + 0.5)
-                               core.add_item(pos, replace_with_item)
-                       end
+       replace_with_item = itemstack:add_item(replace_with_item)
+       user:set_wielded_item(itemstack)
+       if not replace_with_item:is_empty() then
+               local inv = user:get_inventory()
+               -- Check if inv is null, since non-players don't have one
+               if inv then
+                       replace_with_item = inv:add_item("main", replace_with_item)
                end
        end
-       user:set_wielded_item(itemstack)
+       if not replace_with_item:is_empty() then
+               local pos = user:get_pos()
+               pos.y = math.floor(pos.y + 0.5)
+               core.add_item(pos, replace_with_item)
+       end
 
        user:set_hp(user:get_hp() + hp_change)
 
index fdc8e77d840e0b0bed8fb8c8621f8e07a0218201..2179f44b537a52b619c707f3cf12a2775298ab74 100644 (file)
@@ -6082,8 +6082,8 @@ Defaults for the `on_place` and `on_drop` item definition functions
     * Returns `function(itemstack, user, pointed_thing)` as a
       function wrapper for `minetest.do_item_eat`.
     * `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.
+      If the player is eating a stack and `replace_with_item` doesn't fit onto
+      the eaten stack, then the remainings go to a different spot, or are dropped.
 
 Defaults for the `on_punch` and `on_dig` node definition callbacks
 ------------------------------------------------------------------