local update_disassembler = function(pos)
	local m = minetest.get_meta(pos)
	local i = m:get_inventory()
	local paper = i:get_stack('paper',1)
	local item = i:get_stack('item',1)

	local ink_count
	for j=1,i:get_size('ink') do
		local ink = i:get_stack('ink',j)
		local c = ink:get_count()
		if ink_count then
			if c < ink_count then ink_count = c end
		else ink_count = c end
	end
	
	ink_count = ink_count or 0
	local maxrecs = math.min(ink_count, paper:get_count(), item:get_count())

	if maxrecs > 0 and sorcery.cookbook.classes.craft.find(item:get_name()) then
		local rec = ItemStack{name = 'sorcery:recipe', count = maxrecs}
		sorcery.cookbook.setrecipe(rec, 'craft', item:get_name())
		i:set_stack('output',1,rec)
	else
		i:set_stack('output',1,ItemStack())
	end
end
local dsbox = {
	type = 'fixed';
	fixed = {
		-0.5, -0.5, -0.5;
		 0.5,  0.0,  0.5;
	};
}
minetest.register_node('sorcery:disassembler', {
	description = 'Disassembly Kit';
	drawtype = 'mesh';
	mesh = 'sorcery-disassembler.obj';
	paramtype = 'light', sunlight_propagates = true;
	paramtype2 = 'facedir';
	groups = { cracky = 2, sorcery_tech = 1 };
	selection_box = dsbox;
	collision_box = dsbox;
	tiles = {
		'default_copper_block.png';
		'default_wood.png';
		'default_steel_block.png';
		'default_stone.png';
		'default_gold_block.png';
		'default_coal_block.png';
	};
	after_dig_node = sorcery.lib.node.purge_container;
	on_construct = function(pos)
		local m = minetest.get_meta(pos)
		local i = m:get_inventory()
		i:set_size('item',1)
		i:set_size('paper',1)
		i:set_size('ink',3)
		i:set_size('output',1)
		m:set_string('infotext','Disassembly Kit')
		m:set_string('formspec', [[
			formspec_version[3]
			size[10.25,7] real_coordinates[true]
			list[current_player;main;0.25,2;8,4]

			list[context;item;0.25,0.5;1,1;]
			list[context;paper;2.25,0.5;1,1;]
			list[context;ink;4.25,0.5;3,1;]
			list[context;output;9,0.5;1,1;]

			listring[current_player;main] listring[context;item]
			listring[current_player;main] listring[context;paper]
			listring[current_player;main] listring[context;ink]
		]])
	end;
	on_metadata_inventory_put = update_disassembler;
	on_metadata_inventory_take = function(pos,list,idx,stack,user)
		local m = minetest.get_meta(pos)
		local i = m:get_inventory()
		local paper = i:get_stack('paper',1)
		local item = i:get_stack('item',1)

		if list == 'output' then
			local count = stack:get_count()
			item:take_item(count)
			i:set_stack('item',1,item)
			for j=1,i:get_size('ink') do
				local ink = i:get_stack('ink',j)
				ink:take_item(count)
				i:set_stack('ink',j,ink)
			end
		end

		update_disassembler(pos)
	end;
	allow_metadata_inventory_put = function(pos,list,idx,stack,user)
		local name = stack:get_name()
		if list == 'paper' then
			if name == 'default:paper' then return stack:get_count() end
		elseif list == 'ink' then
			if minetest.get_item_group(name,'dye') > 0 then return stack:get_count() end
		elseif list == 'item' then
			return stack:get_count()
		elseif list == 'output' then
			return 0
		end
		return 0
	end;
	allow_metadata_inventory_move = function(pos, fl, fi, tl, ti, count)
		if fl == 'ink' and tl == 'ink'
			then return count
			else return 0
		end
	end;
})

minetest.register_craft {
	output = 'sorcery:disassembler';
	recipe = {
		{'group:wood','default:gold_ingot','dye:black'};
		{'sorcery:vice','screwdriver:screwdriver','xdecor:hammer'};
		{'stairs:slab_stone', 'stairs:slab_stone', 'stairs:slab_stone'};
	};
}
