add_item to inventory - Unexpected behavior?

isaiah658
Member
 
Posts: 36
Joined: Sun Jan 24, 2016 14:58

add_item to inventory - Unexpected behavior?

by isaiah658 » Fri Jul 15, 2016 01:30

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?
 

User avatar
Krock
Developer
 
Posts: 4391
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: add_item to inventory - Unexpected behavior?

by Krock » Fri Jul 15, 2016 08:30

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.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: add_item to inventory - Unexpected behavior?

by everamzah » Fri Jul 15, 2016 11:33

Strange for me, because I have been using:

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.
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: add_item to inventory - Unexpected behavior?

by everamzah » Sat Jul 16, 2016 09:46

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!
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: add_item to inventory - Unexpected behavior?

by everamzah » Sat Jul 16, 2016 17:58

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.
 

isaiah658
Member
 
Posts: 36
Joined: Sun Jan 24, 2016 14:58

Re: add_item to inventory - Unexpected behavior?

by isaiah658 » Sat Jul 16, 2016 20:46

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:

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.
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: add_item to inventory - Unexpected behavior?

by everamzah » Sat Jul 16, 2016 23:16

Well, for what it's worth, this should do what you're trying for:
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,
})
 

isaiah658
Member
 
Posts: 36
Joined: Sun Jan 24, 2016 14:58

Re: add_item to inventory - Unexpected behavior?

by isaiah658 » Sun Jul 17, 2016 02:08

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:
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:
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,
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 3 guests