Page 1 of 1

Add item to player's inventory?

PostPosted: Tue Jan 17, 2012 22:03
by randomproof
What is the best way to put an item in a player's inventory? I have this function:
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_on_placenode(function(pos, newnode, placer)
    o = GetNodeOwner(pos)
    if o ~= nil then
        if o ~= placer:get_player_name() then
            minetest.chat_send_player(placer:get_player_name(), "Node owned by " .. o)
            minetest.env:remove_node(pos)
            --TODO: add node back into placer's inv
           
        end
    end
end)

And I can't seem to work out the InvRef and ItemStack functions to put 'newnode' back into 'placer' inventory.

PostPosted: Tue Jan 17, 2012 22:56
by Nemo08
old :
local pinv = placer:get_inventory()
pinv:autoinsert_stackstring("main", 'node "portal:rune_seed" 1')

with new itemdef:
placer:get_inventory():add_item("throwing:arrow 1") --????

PostPosted: Tue Jan 17, 2012 23:11
by jn
The player "class" also has an add_to_inventory function (I don't think it's changed with itemdef, except for the item/stack string format).

PostPosted: Wed Jan 18, 2012 07:10
by kahrl
@jn nope, add_to_inventory and add_to_inventory_later have been removed with itemdef, since get_inventory():add_item(...) does the same job and is a more generic interface. Nemo08's code is almost right, but add_item is missing the list argument:
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
placer:get_inventory():add_item("main", "throwing:arrow 1")

See experimental/init.lua (TNT:on_punch) for another example.

PostPosted: Thu Jan 19, 2012 14:54
by randomproof
Thank you, all.

PostPosted: Tue Jan 24, 2012 20:46
by randomproof
for some reason i this in not working for me:
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_on_placenode(function(pos, newnode, placer)
    if not IsPlayerNodeOwner(pos, placer:get_player_name()) then
        minetest.chat_send_player(placer:get_player_name(), "You can not place nodes here.")
       
        --add node back into placer's inv
        leftover = placer:get_inventory():add_item("main", newnode.name .. ' 1')
               
        minetest.env:remove_node(pos)
    end
end)

Nodes just disappear from my inventory.

PostPosted: Tue Jan 24, 2012 21:03
by randomproof
Figured it out. What I am doing here is overwritten in builtin.lua minetest.item_place_node.