import stdlib.List;

import crafttweaker.api.ingredient.IIngredient;
import crafttweaker.api.item.IItemStack;
import crafttweaker.api.loot.condition.LootConditions;
import crafttweaker.api.loot.condition.LootTableIdRegexLootCondition;
import crafttweaker.api.loot.modifier.CommonLootModifiers;
import crafttweaker.api.recipe.replacement.Replacer;

public function checkDamage(item: IItemStack, damage: int): bool {
	if (!item.tag.isEmpty() && "tic_broken" in item.tag.asMap()) {
		return item.tag.asMap()["tic_broken"].asInt() == 0;
	}
	return item.maxDamage - item.damage > damage - 2;
}

public function addShapelessAxeRecipe(uniqueID: string, ingredients: IIngredient[], itemOut: IItemStack, damage: int = 1): void {
	var tmp = ingredients as stdlib.List<IIngredient>;
	tmp.insert(0, <tag:items:minecraft:axes>.asIIngredient().onlyIf(uniqueID + "_durabililty", (i) => checkDamage(i, damage)).transformDamage(damage));
	craftingTable.addShapeless(uniqueID + "_with_axe", itemOut, tmp as IIngredient[]);
}

val blank = <item:minecraft:air>;
val stick = <item:minecraft:stick>;
val stringItem = <item:minecraft:string>;
val ironPickHead = <item:tconstruct:pick_head>.withTag({Material: "tconstruct:iron"});
val steelHammerHead = <item:tconstruct:hammer_head>.withTag({Material: "tconstruct:steel"});
val steelAxeHead = <item:tconstruct:small_axe_head>.withTag({Material: "tconstruct:steel"});
val steelNugget = <tag:items:forge:nuggets/steel>;
val steelPlate = <tag:items:forge:plates/steel>;
val bucket = <item:minecraft:bucket>;
//val battery = <item:ad_astra:etrionic_capacitor>;
val furnace = <item:minecraft:furnace>;
//val fan = <item:ad_astra:fan>;
//val gasTank = <item:ad_astra:gas_tank>;
val lRod = <item:minecraft:lightning_rod>;
val anyWool = <tag:items:minecraft:wool>;
val magHelm = <item:irons_spellbooks:wandering_magician_helmet>;
val magChest = <item:irons_spellbooks:wandering_magician_chestplate>;
val magLegs = <item:irons_spellbooks:wandering_magician_leggings>;
val magBoots = <item:irons_spellbooks:wandering_magician_boots>;
val arcaneEssence = <item:irons_spellbooks:arcane_essence>;
val leatherHelm = <item:minecraft:leather_helmet>;
val leatherChest = <item:minecraft:leather_chestplate>;
val leatherLegs = <item:minecraft:leather_leggings>;
val leatherBoots = <item:minecraft:leather_boots>;

val strippingRecipes = [
	[<item:minecraft:oak_log>, <item:minecraft:stripped_oak_log>],
	[<item:minecraft:spruce_log>, <item:minecraft:stripped_spruce_log>],
	[<item:minecraft:birch_log>, <item:minecraft:stripped_birch_log>],
	[<item:minecraft:jungle_log>, <item:minecraft:stripped_jungle_log>],
	[<item:minecraft:acacia_log>, <item:minecraft:stripped_acacia_log>],
	[<item:minecraft:dark_oak_log>, <item:minecraft:stripped_dark_oak_log>],
	[<item:minecraft:mangrove_log>, <item:minecraft:stripped_mangrove_log>],
	[<item:minecraft:cherry_log>, <item:minecraft:stripped_cherry_log>],
	//[<item:ad_astra:glacian_log>, <item:ad_astra:stripped_glacian_log>],
	[<item:quark:blossom_log>, <item:quark:stripped_blossom_log>],
	[<item:quark:ancient_log>, <item:quark:stripped_ancient_log>],
	[<item:quark:azalea_log>, <item:quark:stripped_azalea_log>],
	[<item:alexscaves:pewen_log>, <item:alexscaves:stripped_pewen_log>],
	[<item:alexscaves:thornwood_log>, <item:alexscaves:stripped_thornwood_log>],
	[<item:tconstruct:greenheart_log>, <item:tconstruct:stripped_greenheart_log>],
	[<item:tconstruct:skyroot_log>, <item:tconstruct:stripped_skyroot_log>],
	[<item:tconstruct:enderbark_log>, <item:tconstruct:stripped_enderbark_log>],
	[<item:minecraft:oak_wood>, <item:minecraft:stripped_oak_wood>],
	[<item:minecraft:spruce_wood>, <item:minecraft:stripped_spruce_wood>],
	[<item:minecraft:birch_wood>, <item:minecraft:stripped_birch_wood>],
	[<item:minecraft:jungle_wood>, <item:minecraft:stripped_jungle_wood>],
	[<item:minecraft:acacia_wood>, <item:minecraft:stripped_acacia_wood>],
	[<item:minecraft:dark_oak_wood>, <item:minecraft:stripped_dark_oak_wood>],
	[<item:minecraft:mangrove_wood>, <item:minecraft:stripped_mangrove_wood>],
	[<item:minecraft:cherry_wood>, <item:minecraft:stripped_cherry_wood>],
	[<item:quark:blossom_wood>, <item:quark:stripped_blossom_wood>],
	[<item:quark:ancient_wood>, <item:quark:stripped_ancient_wood>],
	[<item:quark:azalea_wood>, <item:quark:stripped_azalea_wood>],
	[<item:alexscaves:pewen_wood>, <item:alexscaves:stripped_pewen_wood>],
	[<item:alexscaves:thornwood_wood>, <item:alexscaves:stripped_thornwood_wood>],
	[<item:tconstruct:greenheart_wood>, <item:tconstruct:stripped_greenheart_wood>],
	[<item:tconstruct:skyroot_wood>, <item:tconstruct:stripped_skyroot_wood>],
	[<item:tconstruct:enderbark_wood>, <item:tconstruct:stripped_enderbark_wood>]
];

val choppingRecipes = [
	[<item:minecraft:stripped_oak_log>, <item:minecraft:oak_planks>],
	[<item:minecraft:stripped_spruce_log>, <item:minecraft:spruce_planks>],
	[<item:minecraft:stripped_birch_log>, <item:minecraft:birch_planks>],
	[<item:minecraft:stripped_jungle_log>, <item:minecraft:jungle_planks>],
	[<item:minecraft:stripped_acacia_log>, <item:minecraft:acacia_planks>],
	[<item:minecraft:stripped_dark_oak_log>, <item:minecraft:dark_oak_planks>],
	[<item:minecraft:stripped_mangrove_log>, <item:minecraft:mangrove_planks>],
	[<item:minecraft:stripped_cherry_log>, <item:minecraft:cherry_planks>],
	//[<item:ad_astra:stripped_glacian_log>, <item:ad_astra:glacian_planks>],
	[<item:quark:stripped_blossom_log>, <item:quark:blossom_planks>],
	[<item:quark:stripped_ancient_log>, <item:quark:ancient_planks>],
	[<item:quark:stripped_azalea_log>, <item:quark:azalea_planks>],
	[<item:alexscaves:stripped_pewen_log>, <item:alexscaves:pewen_planks>],
	[<item:alexscaves:stripped_thornwood_log>, <item:alexscaves:thornwood_planks>],
	[<item:tconstruct:stripped_greenheart_log>, <item:tconstruct:greenheart_planks>],
	[<item:tconstruct:stripped_skyroot_log>, <item:tconstruct:skyroot_planks>],
	[<item:tconstruct:stripped_enderbark_log>, <item:tconstruct:enderbark_planks>],
	[<item:minecraft:stripped_oak_wood>, <item:minecraft:oak_planks>],
	[<item:minecraft:stripped_spruce_wood>, <item:minecraft:spruce_planks>],
	[<item:minecraft:stripped_birch_wood>, <item:minecraft:birch_planks>],
	[<item:minecraft:stripped_jungle_wood>, <item:minecraft:jungle_planks>],
	[<item:minecraft:stripped_acacia_wood>, <item:minecraft:acacia_planks>],
	[<item:minecraft:stripped_dark_oak_wood>, <item:minecraft:dark_oak_planks>],
	[<item:minecraft:stripped_mangrove_wood>, <item:minecraft:mangrove_planks>],
	[<item:minecraft:stripped_cherry_wood>, <item:minecraft:cherry_planks>],
	[<item:quark:stripped_blossom_wood>, <item:quark:blossom_planks>],
	[<item:quark:stripped_ancient_wood>, <item:quark:ancient_planks>],
	[<item:quark:stripped_azalea_wood>, <item:quark:azalea_planks>],
	[<item:alexscaves:stripped_pewen_wood>, <item:alexscaves:pewen_planks>],
	[<item:alexscaves:stripped_thornwood_wood>, <item:alexscaves:thornwood_planks>],
	[<item:tconstruct:stripped_greenheart_wood>, <item:tconstruct:greenheart_planks>],
	[<item:tconstruct:stripped_skyroot_wood>, <item:tconstruct:skyroot_planks>],
	[<item:tconstruct:stripped_enderbark_wood>, <item:tconstruct:enderbark_planks>],
	[<item:minecraft:oak_planks>, stick],
	[<item:minecraft:spruce_planks>, stick],
	[<item:minecraft:birch_planks>, stick],
	[<item:minecraft:jungle_planks>, stick],
	[<item:minecraft:acacia_planks>, stick],
	[<item:minecraft:dark_oak_planks>, stick],
	[<item:minecraft:mangrove_planks>, stick],
	[<item:minecraft:cherry_planks>, stick],
	//[<item:ad_astra:glacian_planks>, stick],
	[<item:quark:blossom_planks>, stick],
	[<item:quark:ancient_planks>, stick],
	[<item:quark:azalea_planks>, stick],
	[<item:alexscaves:pewen_planks>, stick],
	[<item:alexscaves:thornwood_planks>, stick],
	[<item:tconstruct:greenheart_planks>, stick],
	[<item:tconstruct:skyroot_planks>, stick],
	[<item:tconstruct:enderbark_planks>, stick]
];

val recipeRemoval = [
	//<item:minecraft:wooden_pickaxe>,
	//<item:minecraft:wooden_axe>,
	//<item:minecraft:wooden_shovel>,
	//<item:minecraft:wooden_sword>,
	//<item:minecraft:wooden_hoe>,
	//<item:minecraft:stone_pickaxe>,
	//<item:minecraft:stone_axe>,
	//<item:minecraft:stone_shovel>,
	//<item:minecraft:stone_sword>,
	//<item:minecraft:stone_hoe>,
	<item:minecraft:iron_pickaxe>,
	<item:minecraft:iron_axe>,
	<item:minecraft:iron_shovel>,
	<item:minecraft:iron_sword>,
	<item:minecraft:iron_hoe>,
	<item:minecraft:golden_pickaxe>,
	<item:minecraft:golden_axe>,
	<item:minecraft:golden_shovel>,
	<item:minecraft:golden_sword>,
	<item:minecraft:golden_hoe>,
	//<item:ad_astra:steel_cable>,
	//<item:ad_astra:desh_cable>,
	//<item:ad_astra:desh_fluid_pipe>,
	//<item:ad_astra:ostrum_fluid_pipe>,
	//<item:ad_astra:cable_duct>,
	//<item:ad_astra:fluid_pipe_duct>,
	//<item:ad_astra:coal_generator>,
	//<item:ad_astra:compressor>,
	//<item:ad_astra:etrionic_blast_furnace>,
	//<item:ad_astra:fuel_refinery>,
	//<item:ad_astra:oxygen_loader>,
	//<item:ad_astra:solar_panel>,
	//<item:ad_astra:water_pump>,
	//<item:ad_astra:energizer>,
	//<item:ad_astra:iron_rod>,
	//<item:ad_astra:steel_rod>,
	<item:immersiveengineering:stick_iron>,
	<item:immersiveengineering:stick_steel>,
	<item:immersiveengineering:hammer>,
	<item:immersiveengineering:wirecutter>
] as IIngredient[];

for item in recipeRemoval {
	craftingTable.remove(item);
}

loot.modifiers.register("chest_loot_removal", LootConditions.only(LootTableIdRegexLootCondition.create(".*chests/.*")), CommonLootModifiers.removeAll(recipeRemoval));
loot.modifiers.register("extra_chest_loot_removal", LootConditions.only(LootTableIdRegexLootCondition.create(".*chests/.*")), CommonLootModifiers.removeAll([
	<item:minecraft:iron_ingot>,
	<item:minecraft:iron_block>,
	<item:minecraft:gold_ingot>,
	<item:minecraft:gold_block>,
	<item:minecraft:diamond>,
	<item:minecraft:diamond_block>
]));

loot.modifiers.register("irons_spellbooks_removal", LootConditions.none(), CommonLootModifiers.removeAll([<item:irons_spellbooks:common_ink>, <item:irons_spellbooks:scroll>]));
loot.modifiers.register("irons_spellbooks_ink_downtier1", LootConditions.none(), CommonLootModifiers.replaceWith(<item:irons_spellbooks:uncommon_ink>, <item:irons_spellbooks:common_ink>));
loot.modifiers.register("irons_spellbooks_ink_downtier2", LootConditions.none(), CommonLootModifiers.replaceWith(<item:irons_spellbooks:rare_ink>, <item:irons_spellbooks:uncommon_ink>));
loot.modifiers.register("irons_spellbooks_ink_downtier3", LootConditions.none(), CommonLootModifiers.replaceWith(<item:irons_spellbooks:epic_ink>, <item:irons_spellbooks:rare_ink>));
loot.modifiers.register("irons_spellbooks_ink_downtier4", LootConditions.none(), CommonLootModifiers.replaceWith(<item:irons_spellbooks:legendary_ink>, <item:irons_spellbooks:epic_ink>));

craftingTable.remove(stick);
craftingTable.remove(<item:irons_spellbooks:graybeard_staff>);
craftingTable.remove(<item:ftbquests:loot_crate_opener>);

for recipe in strippingRecipes {
	craftingTable.remove(recipe[0]);
	craftingTable.remove(recipe[1]);
}

for recipe in choppingRecipes {
	craftingTable.remove(recipe[0]);
	craftingTable.remove(recipe[1]);
}

for recipe in strippingRecipes {
	addShapelessAxeRecipe("strip_" + recipe[0].registryName.path, [recipe[0]], recipe[1]);
}

for recipe in choppingRecipes {
	addShapelessAxeRecipe("chop_" + recipe[0].registryName.path, [recipe[0]], recipe[1] * 2);
}

craftingTable.addShaped("wooden_axe_alt", <item:minecraft:wooden_axe>, [
	[<tag:items:minecraft:logs>, <tag:items:minecraft:logs>],
	[blank, <tag:items:minecraft:logs>]
]);

craftingTable.addShaped("iron_pickaxe", <item:minecraft:iron_pickaxe>, [
	[ironPickHead, stringItem],
	[stick]
]);

craftingTable.addShaped("engineers_hammer", <item:immersiveengineering:hammer>, [
	[stringItem, steelHammerHead],
	[  stick,         blank     ]
]);

craftingTable.addShaped("engineers_wire_cutters", <item:immersiveengineering:wirecutter>, [
	[    blank,    steelAxeHead, blank],
	[steelAxeHead, steelNugget,  stick],
	[    blank,       stick,     blank]
]);

//craftingTable.addShaped("fuel_refinery", <item:ad_astra:fuel_refinery>, [
//	[steelPlate, steelPlate, steelPlate],
//	[  bucket,     furnace,    bucket  ],
//	[steelPlate,   battery,  steelPlate]
//]);

//craftingTable.addShaped("oxygen_loader", <item:ad_astra:oxygen_loader>, [
//	[steelPlate,   fan,   steelPlate],
//	[  gasTank,    lRod,   gasTank  ],
//	[steelPlate, battery, steelPlate]
//]);

craftingTable.addShapeless("wool_to_string", stringItem, [anyWool]);

craftingTable.remove(magHelm);
craftingTable.remove(magChest);
craftingTable.remove(magLegs);
craftingTable.remove(magBoots);

craftingTable.addShaped("wandering_magician_helmet", magHelm, [
	[arcaneEssence, arcaneEssence, arcaneEssence],
	[arcaneEssence,  leatherHelm,  arcaneEssence]
]);

craftingTable.addShaped("wandering_magician_chestplate", magChest, [
	[arcaneEssence, leatherChest,  arcaneEssence],
	[arcaneEssence, arcaneEssence, arcaneEssence],
	[arcaneEssence, arcaneEssence, arcaneEssence]
]);

craftingTable.addShaped("wandering_magician_leggings", magLegs, [
	[arcaneEssence, arcaneEssence, arcaneEssence],
	[arcaneEssence,  leatherLegs,  arcaneEssence],
	[arcaneEssence,     blank,     arcaneEssence]
]);

craftingTable.addShaped("wandering_magician_boots", magBoots, [
	[arcaneEssence,     blank,    arcaneEssence],
	[arcaneEssence, leatherBoots, arcaneEssence]
]);

### Script written by FoxMcloud5655