ServerEvents.recipes(event => {
	// Metal tiers:
	// Copper, iron, tin, nickel, gold, zinc, brass, invar, bronze - Pre-basic build tier, the only ores that don't require a pulverizer or smelter to process
	// silver, platinum, lead, iridum, osmium - Gated behind the pulverizer
	// lumium, enderium, electrum, signalum, steel - Alloys are gated behind the smelter as well
	// uranium - requires basic energetic grist to energize into ingots

	// Gear tiers:
	// copper, iron, tin, nickel, zinc, brass, bronze - Pre-gear die tier
	// invar, bronze, brass - the only alloys you can make without grist
	// constantan - requires 1 build grist to make the dust alloy
	// invar, gold, silver, electrum, signalum, lead, steel - Gated behind gear die
	// lumium, enderium, other alloys - Gated behind heavy duty gear die

	let metalForm = (metal, tag, form) => {
		let itemType = AlmostUnified.getPreferredItemForTag(`forge:${tag}/${metal}`);
		if (itemType.id == 'minecraft:air') {
			console.log(`metals: ${metal} ${form} has unknown tag forge:${tag}/${metal}?`)
			let ret = Item.of(`alltheores:${metal}_${form}`);
			if (ret.id == "minecraft:air") {
				return Item.of(`thermal:${metal}_${form}`);
			}
			return ret;
		}
		console.log(`metals: forge:${tag}/${metal} found`);
		return itemType;
	}

	event.shapeless('thermal:quartz_dust', ['8x minecraft:granite', '#alltheores:ore_hammers']).id('kubejs:quartz_dust_from_granit_crushing_manual_only');
	event.shapeless('thermal:quartz_dust', ['minecraft:quartz', '#alltheores:ore_hammers']).id('kubejs:quartz_dust_crushing_manual_only');

	// All metals can be processed in the pulverizer/crusher, make plates in the press, and make gears with the die
	{
	let _ = ['copper', 'rose_gold', 'aluminum', 'constantan', 'iron', 'tin', 'nickel', 'gold', 'zinc', 'brass', 'invar', 'bronze', 'silver', 'platinum', 'lead', 'iridium', 'osmium', 'lumium', 'enderium', 'electrum', 'signalum', 'steel', 'uranium'].forEach(metal => {
		console.log(`Setting up ${metal} processing...`);

		let gearType = metalForm(metal, 'gears', 'gear');
		let dustType = metalForm(metal, 'dusts', 'dust');
		let plateType = metalForm(metal, 'plates', 'plate');
		let ingotType = metalForm(metal, 'ingots', 'ingot');
		let rawType = metalForm(metal, 'raw_materials', 'ore');
		let blockType = metalForm(`raw_${metal}`, 'storage_blocks', 'block');

		event.remove({output: gearType});
		event.remove({output: plateType});
		
		event.remove({input: `#forge:ores/${metal}`});
		event.remove({input: `#forge:raw_materials/${metal}`});

		// By default, you can't make any blends in the crafting table.
		event.remove({output: dustType, type: 'minecraft:crafting'});
		event.remove({output: ingotType, input: 'minecraft:fire_charge'});

		event.remove({id: `alltheores:${metal}_dust_from_alloy_blending`});

		console.log(`crushing ${rawType.toJson()} ${dustType.toJson()}`);
		if (rawType.id != "minecraft:air") {
			event.custom({
				type: "thermal:pulverizer",
				ingredients: [
					{tag: `forge:raw_materials/${metal}`}
				],
				result: [
					{item: dustType.id, chance: 1.05},
					{item: dustType.id, chance: 0.25}
				],
				experience: 0.5
			});

			event.custom({
				type: "create:crushing",
				ingredients: [
					{tag: `forge:raw_materials/${metal}`}
				],
				results: [
					{item: dustType.id, chance: 1.05},
					{item: dustType.id, chance: 0.25},
					{item: 'create:experience_nugget', count: 9, chance: 0.75}
				],
				experience: 0.5
			});
		}

		if (blockType.id != "minecraft:air") {
			event.custom({
				type: "thermal:pulverizer",
				ingredients: [
					{tag: `forge:storage_blocks/raw_${metal}`}
				],
				result: [
					{item: dustType.id, chance: 1.05 * 9},
					{item: dustType.id, chance: 0.25 * 9}
				],
				experience: 0.5
			});

			event.custom({
				type: "create:crushing",
				ingredients: [
					{tag: `forge:raw_materials/${metal}`}
				],
				results: [
					{item: dustType.id, count: 9, chance: 1.05},
					{item: dustType.id, count: 9, chance: 0.25},
					{item: 'create:experience_nugget', count: 9 * 9, chance: 0.75}
				],
				experience: 0.5
			});
		}

		// 1 dust = 1 ingot
		event.custom({
			type: "thermal:pulverizer",
			ingredients: [
				ingotType.toJson()
			],
			result: [
				dustType.toJson()
			],
		});

		event.custom({
			type: "create:crushing",
			ingredients: [
				ingotType.toJson()
			],
			results: [
				dustType.toJson()
			],
		});

		// 4 ingots = 1 gear
		event.custom({
			type: "thermal:press",
			ingredients: [
				ingotType.withCount(4).toJson(),
				{ item: "thermal:press_gear_die" }
			],
			result: [gearType.toJson()]
		});

		// 1 ingot = 1 plate
		event.custom({
			type: "thermal:press",
			ingredients: [
				ingotType.toJson(),
			],
			result: [plateType.toJson()]
		});
		event.custom({
			type: "create:pressing",
			ingredients: [
				ingotType.toJson(),
			],
			results: [plateType.toJson()]
		});
	});}

	// But only these metals can be processed without a machine
	{let _ = ['copper', 'iron', 'tin', 'nickel', 'zinc', 'brass', 'bronze', 'lead', 'aluminum'].forEach(metal => {
		console.log(`Setting up ${metal} manual processing...`);
		let gearID = metalForm(metal, 'gears', 'gear');
		let plateID = metalForm(metal, 'plates', 'plate');
		let dustID = metalForm(metal, 'dusts', 'dust');
		event.shaped(gearID, [
			' A ',
			'ABA',
			' A '
		], {
			'A': global.ingotItem(metal),
			'B': '#forge:gems'
		});
		event.shapeless(plateID, [global.ingotItem(metal, 2), '#alltheores:ore_hammers']).id(`kubejs:plate_crushing_${metal}_manual_only`);
		event.shapeless(dustID, [global.ingotItem(metal), '#alltheores:ore_hammers']).id(`kubejs:ore_crushing_${metal}_manual_only`);
	});}

	// And only these ores can be hammered or crushed
	{let _ = ['copper', 'iron', 'tin', 'nickel', 'zinc', 'lead', 'aluminum', 'gold'].forEach(ore => {
		console.log(`Setting up ${ore} hammering...`);
		let dustType = metalForm(ore, 'dusts', 'dust');
		event.shapeless(dustType.withCount(2), [`#forge:raw_materials/${ore}`, '#alltheores:ore_hammers']).id(`kubejs:ore_crushing_${ore}_manual_only`);
		event.shapeless(dustType.withCount(2 * 9), [`#forge:storage_blocks/raw_${ore}`, '#alltheores:ore_hammers']).id(`kubejs:ore_block_crushing_${ore}_manual_only`);
	});}

	// And only copper can be smelted without pulverizing
	event.smelting(global.ingotItem('copper'), metalForm('copper', 'raw_materials', 'ore'));

	console.log(`Setting up blends...`);
	// These are the only blends you can make by hand, aka pre-smelter alloys
	// FIXME: Remove the hammer from the create mixer
    event.shapeless(Item.of(metalForm('bronze', 'dusts', 'dust').withCount(4)), [
		global.dustItem('tin', 2), 
		global.dustItem('copper', 2),
		'#alltheores:ore_hammers'
	]).id('kubejs:bronze_mixing_manual_only');
	event.custom({
		type: "create:mixing",
		ingredients: [
			global.dustItem('tin', 2),
			global.dustItem('copper', 2),
		],
		results: [global.dustItem('tin', 4)]
	});

	event.shapeless(Item.of(metalForm('brass', 'dusts', 'dust').withCount(4)), [
		global.dustItem('copper', 3),
		global.dustItem('zinc', 1),
		'#alltheores:ore_hammers'
	]).id(`kubejs:brass_mixing_manual_only`);
	event.custom({
		type: "create:mixing",
		ingredients: [
			global.dustItem('copper', 3),
			global.dustItem('zinc', 1),
		],
		results: [global.dustItem('brass', 4)]
	});

	event.shapeless(Item.of(metalForm('invar', 'dusts', 'dust').withCount(4)), [
		global.dustItem('iron', 2),
		global.dustItem('nickel', 2),
		'#alltheores:ore_hammers'
	]).id('kubejs:invar_mixing_manual_only');
	event.custom({
		type: "create:mixing",
		ingredients: [
			global.dustItem('iron', 2),
			global.dustItem('nickel', 2),
		],
		results: [global.dustItem('invar', 4)]
	});

	// Constantan dust consumes your first build grist, and it unlocks the basic grist production line
	event.shapeless(Item.of(metalForm('constantan', 'dusts', 'dust').withCount(4)),[
		global.ingotItem('nickel', 2),
		global.ingotItem('copper', 2),
		'#kubejs:grist/build/basic',
		'#alltheores:ore_hammers'
	]).id('kubejs:constantan_dust_mixing_manual_only');
	// Constantan is the only metal you can't work on the bench except for gears
	event.shaped('thermal:constantan_gear', [
        ' A ',
        'ABA',
        ' A '
    ], {
        'A': global.ingotItem('constantan'),
        'B': '#forge:gems'
    });

	event.shaped('thermal:diamond_gear', [
		' A ',
		'ABA',
		' A '
	], {
		'A': 'minecraft:diamond',
		'B': 'minecraft:crying_obsidian'
	});
	
	// Lapis can be pulverized and hammered.
	event.remove({output: 'thermal:lapis_dust', type: 'thermal:pulverizer'});
	event.custom({
		type: "thermal:pulverizer",
		ingredients: [
			{ item: "minecraft:lapis_lazuli" }
		],
		result: [
			{ item: "thermal:lapis_dust", chance: 1.05 },
			{ item: "thermal:lapis_dust", chance: 0.25 }
		],
		experience: 0.5
	});
	event.shapeless("2x thermal:lapis_dust", ["minecraft:lapis_lazuli", '#alltheores:ore_hammers']).id('kubejs:lapis_dust_crushing_manual_only');

	// Create clay by centrifuging gravel and ash bricks
	event.custom({
		type: "thermal:centrifuge",
		ingredients: [
			{ item: "supplementaries:ash_brick" }
		],
		result: [
			{ item: "minecraft:clay_ball", chance: 1.05 },
			{ item: "minecraft:clay_ball", chance: 0.25 }
		],
		experience: 0.5
	});

	event.custom({
		type: "thermal:centrifuge",
		ingredients: [
			{ item: "minecraft:gravel" }
		],
		result: [
			{ item: "minecraft:sand", chance: 2.00 },
			{ item: "minecraft:clay_ball", chance: 0.75 }
		],
		experience: 0.5
	});
})