Difference between revisions of "Talk:InvRef"

From Minetest Developer Wiki
Jump to navigation Jump to search
(Created page with "please add something like room_for_items() to test if more items can find place in the inventory ~~~~")
 
 
Line 1: Line 1:
 
please add something like room_for_items() to test if more items can find place in the inventory [[User:Hybrid Dog|Hybrid Dog]] ([[User talk:Hybrid Dog|talk]]) 14:21, 10 May 2014 (MSK)
 
please add something like room_for_items() to test if more items can find place in the inventory [[User:Hybrid Dog|Hybrid Dog]] ([[User talk:Hybrid Dog|talk]]) 14:21, 10 May 2014 (MSK)
 +
<source> on_drop = function(itemstack, dropper)
 +
local inv = dropper:get_inventory()
 +
if not inv then
 +
return
 +
end
 +
local free_slots = 0
 +
for _,i in ipairs(inv:get_list("main")) do
 +
if i:get_count() == 0 then
 +
free_slots = free_slots+1
 +
end
 +
end
 +
if free_slots < 2 then
 +
return
 +
end
 +
itemstack:take_item()
 +
inv:add_item("main", "nether:fruit_leaf")
 +
inv:add_item("main", "nether:fruit_no_leaf")
 +
return itemstack
 +
end,</source>
 +
I need to do it this way instead. [[User:Hybrid Dog|Hybrid Dog]] ([[User talk:Hybrid Dog|talk]]) 14:44, 10 May 2014 (MSK)

Latest revision as of 10:44, 10 May 2014

please add something like room_for_items() to test if more items can find place in the inventory Hybrid Dog (talk) 14:21, 10 May 2014 (MSK)

	on_drop = function(itemstack, dropper)
		local inv = dropper:get_inventory()
		if not inv then
			return
		end
		local free_slots = 0
		for _,i in ipairs(inv:get_list("main")) do
			if i:get_count() == 0 then
				free_slots = free_slots+1
			end
		end
		if free_slots < 2 then
			return
		end
		itemstack:take_item()
		inv:add_item("main", "nether:fruit_leaf")
		inv:add_item("main", "nether:fruit_no_leaf")
		return itemstack
	end,

I need to do it this way instead. Hybrid Dog (talk) 14:44, 10 May 2014 (MSK)