Detect if chest has items?

Post Reply
johalun
Member
Posts: 24
Joined: Sat Sep 26, 2020 14:18
GitHub: johalun

Detect if chest has items?

by johalun » Post

What is the Minetest way of doing this? I would like to get a mesecon line powered if a chest contains any items to start a clock that feeds the items to a furnace by pulsing a filter-injector.

User avatar
sorcerykid
Member
Posts: 1847
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Detect if chest has items?

by sorcerykid » Post

This is the function from my Chests Redux mod that allows for checking whether a container at any given position is empty (you can also optionally specify the list to check, but by default it uses "main").

Code: Select all

default.is_empty = function ( pos, list )
        return minetest.get_meta( pos ):get_inventory( ):is_empty( list or "main" )
end

johalun
Member
Posts: 24
Joined: Sat Sep 26, 2020 14:18
GitHub: johalun

Re: Detect if chest has items?

by johalun » Post

Thanks! But, how would you use this to power a mesecon line?

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: Detect if chest has items?

by AiTechEye » Post

i think it should work

Code: Select all

minetest.register_node("mod:chest_checker", {
	description = "Chest checker",
	tiles = {"default_steel_block.png","default_steel_block.png","default_steel_block.png","default_steel_block.png","default_copper_block.png","default_steel_block.png"},
	groups = {mesecon=2,snappy = 3},
	sounds = default.node_sound_stone_defaults(),
	mesecons = {receptor = {state = "off"}},
	on_construct = function(pos)
		minetest.get_node_timer(pos):start(1)
	end,
	on_timer = function (pos, elapsed)
		local meta=minetest.get_meta(pos)
		local dir = minetest.facedir_to_dir(minetest.get_node(pos).param2)
		local p = {x=pos.x+dir.x,y=pos.y+dir.y,z=pos.z+dir.z}


		if minetest.get_meta(p):get_inventory():is_empty("main") == false then
			mesecon.receptor_on(pos)

			--minetest.swap_node(pos, {name="mod:chest_checker2"})
			--minetest.get_node_timer(pos):start(2)
		end
		return true
	end
})

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests