Page 1 of 1

add_item to inventory - Unexpected behavior?

PostPosted: Fri Jul 15, 2016 01:30
by isaiah658
I'm making a berry bush that when you right click on it you get a berry in your inventory. I'm running into odd behavior when using clicker:get_inventory():add_item("main", "farming:blueberrybush"). This does not add an item to the inventory if the spot where the item would be added is your wielded hand. Meaning if the spot where it would be added is currently your selected spot on the hotbar with nothing in it (hand) the item doesn't get added. In every other situation the berry gets added to the first blank spot like I expect it would. Is this the intended behavior of add_item? Do I need to get the wielded item of the player and if it is the hand add the item specifically to that slot?

Re: add_item to inventory - Unexpected behavior?

PostPosted: Fri Jul 15, 2016 08:30
by Krock
Yes, you would need to get the selected slot and add the item to that (empty) stack manually. add_item is not only made for the player's inventory - the furnace and pipeworks systems with chests use this function too.

Re: add_item to inventory - Unexpected behavior?

PostPosted: Fri Jul 15, 2016 11:33
by everamzah
Strange for me, because I have been using:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local item = inv:add_item("main", stack)
if item then
    minetest.add_item(pos, item)
end

This seems to fill all slots, and drop whatever won't fit.

Re: add_item to inventory - Unexpected behavior?

PostPosted: Sat Jul 16, 2016 09:46
by everamzah
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node("tmp:tmp",{description="tmp",tiles={"default_wood.png"},on_rightclick=function(pos,node,clicker,itemstack,pointed_thing)local item=clicker:get_inventory():add_item("main",{name="tmp:tmp"})if item then minetest.add_item(clicker:getpos(),item)end;print(dump(item:to_table()))end})


Two things are weird about this:
1.) I expected the code snippet box to show a horizontal scrollbar, and not wrap.
2.) add_item() is supposed to return whatever stack wasn't added, but doesn't do so if the wieldhand is slotted. It thinks it was successful!

Re: add_item to inventory - Unexpected behavior?

PostPosted: Sat Jul 16, 2016 17:58
by everamzah
Here's a rather useless video demonstrating what I mean, but probably failing to illustrate my thought. Basically, normally, if there's not room and inv:add_item() was unable to find room in the player's inventory, it would return the leftover as an ItemStack.

Here, when the wieldhand is empty, there is indeed room, but inv:add_item() fails to insert into it. The odd thing, to me, is that there's no leftover returned as an ItemStack.

Re: add_item to inventory - Unexpected behavior?

PostPosted: Sat Jul 16, 2016 20:46
by isaiah658
everamzah wrote:Here's a rather useless video demonstrating what I mean, but probably failing to illustrate my thought. Basically, normally, if there's not room and inv:add_item() was unable to find room in the player's inventory, it would return the leftover as an ItemStack.

Here, when the wieldhand is empty, there is indeed room, but inv:add_item() fails to insert into it. The odd thing, to me, is that there's no leftover returned as an ItemStack.


Yes that is exactly what I was trying to describe in post! Thank you for making a video about it so others can see. I don't get why that would be the intended behavior. Just because the empty slot happens to be the wielded slot shouldn't mean no item gets added.

I'm also trying to get around this but struggling to do so. I've added this to my code:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local stack = clicker:get_wielded_item()
if stack:get_name() == "" then
   clicker:set_wielded_item(ItemStack("farming:blueberrybush"))
end


According to the Minetest dev wiki,
set_wielded_item(item) — replaces the wielded item, returns true if successful
.
I've tested and it is indeed returning true but the item is not being added! I don't understand. I've spent how many hours trying to get this to work.

Re: add_item to inventory - Unexpected behavior?

PostPosted: Sat Jul 16, 2016 23:16
by everamzah
Well, for what it's worth, this should do what you're trying for:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node("tmp:tmp", {
        description = "tmp",
        tiles = {"default_wood.png"},
        on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
                if itemstack:item_fits({name = "tmp:tmp"}) then
                        itemstack:add_item({name = "tmp:tmp"})
                else
                        local item = clicker:get_inventory():add_item("main", {name = "tmp:tmp"})
                        if item then
                                minetest.add_item(clicker:getpos(), item)
                        end
                end
        end,
})

Re: add_item to inventory - Unexpected behavior?

PostPosted: Sun Jul 17, 2016 02:08
by isaiah658
Ok something else that is interesting to note. Adding an item to the inventory with on_punch makes the item go into the open slot even if it is the wielded slot. On_rightclick does not have the item go there.

This works:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
on_punch = function(pos, node, player, pointed_thing)
   local inv = player:get_inventory()
   local left = inv:add_item("main", "default:dirt")
end,


This doesn't:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
   local inv = player:get_inventory()
   local left = inv:add_item("main", "default:dirt")
end,