Add item to player's inventory?

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

Add item to player's inventory?

by randomproof » Tue Jan 17, 2012 22:03

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.
Last edited by randomproof on Tue Jan 17, 2012 22:04, edited 1 time in total.
 

Nemo08
Member
 
Posts: 132
Joined: Mon Dec 26, 2011 04:59

by Nemo08 » Tue Jan 17, 2012 22:56

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") --????
 

jn
Member
 
Posts: 106
Joined: Tue Jan 03, 2012 19:15

by jn » Tue Jan 17, 2012 23:11

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).
 

kahrl
Member
 
Posts: 236
Joined: Fri Sep 02, 2011 07:51

by kahrl » Wed Jan 18, 2012 07:10

@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.
 

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

by randomproof » Thu Jan 19, 2012 14:54

Thank you, all.
 

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

by randomproof » Tue Jan 24, 2012 20:46

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.
 

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

by randomproof » Tue Jan 24, 2012 21:03

Figured it out. What I am doing here is overwritten in builtin.lua minetest.item_place_node.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 17 guests

cron