local original_register_craft = minetest.register_craft
local craft_list = {}

minetest.register_craft = function(recipe)
	craft_list[#craft_list+1] = recipe
	original_register_craft(recipe)
end

local function get_name(inp)
	inp = string.gsub(inp, ' [0-9]$', '')
	local item = minetest.registered_items[inp]
	if item and item.description then
		return item.description
	end
	return inp
end

fun_caves.print_recipes = function()
	--print(dump(recipe))
	table.sort(craft_list, function(a, b)
		if a.output and b.output and a.output < b.output then
			return true
		end
	end)
	for _, recipe in pairs(craft_list) do
		if recipe.type == 'cooking' then
			local input = get_name(recipe.recipe)
			local output = get_name(recipe.output)
			print('Cook '..input..' to produce '..output..'.')
		elseif recipe.type == 'shapeless' then
			local inputs = {}
			for _, input in pairs(recipe.recipe) do
				inputs[#inputs+1] = get_name(input)
			end
			local output = get_name(recipe.output)
			print('Combine '..table.concat(inputs, ', ')..' to produce '..output..'.')
		elseif recipe.type == 'fuel' then
			-- nop
		else
			local output = get_name(recipe.output)
			print('Combine the following to produce '..output..':')
			for _, line in pairs(recipe.recipe) do
				local out = {}
				for _, input in pairs(line) do
					input = get_name(input)
					if input == '' then
						input = '[empty]'
					end
					out[#out+1] = input
				end
				print('  '..table.concat(out, ', '))
			end
		end
		print()
	end
end
