//------------------------------------------------------------------------------------------------------------------------------------------------
// PA2 Extras - Tags Mapper
//------------------------------------------------------------------------------------------------------------------------------------------------
// Author: RicTheCoder
//
// Simple script to map multiple tags to single/multi targets instead of the other way around.
//------------------------------------------------------------------------------------------------------------------------------------------------

// Fixes the inconsistence of fluids tags, such as, oil and xp in #minecraft:water
const fixFluidTags = true;

// Blocks mapped to their needed tags
// - 'block' is the block id, can be an array to apply to multiple
// - 'tags' are the tags to add, needs to be an array
const blockToTags = [
	{
		block: ["minecraft:reinforced_deepslate", "minecraft:trial_spawner", "minecraft:trial_vault", "minecraft:trial_vault_ominous"],
		tags: [
			"forge:mineable/paxel", "minecraft:mineable/mattock", "projecte:mineable/morning_star", 
			"projecte:mineable/hammer", "minecraft:mineable/pickaxe"
		]
	}
	
	// QUICK NOTE: Trial spawners and vaults are allowed to drop in the 'drop_fixes.js' but if they are dropped they will never work again, 
	// the purpose is to be cosmetic, as they are nice for some builds. The tags help getting the right tools to mine them.
];

// Items mapped to their needed tags
// - 'item' is the item id, can be an array to apply to multiple
// - 'tags' are the tags to add, needs to be an array
const itemToTags = [
	{
		item: "draconicevolution:deepslate_draconium_ore",
		tags: [
			"forge:ores", "forge:ores/draconium", "balm:ores"
		]
	},
	{
		item: "gag:time_sand_pouch",
		tags: ["curios:bundle"]
	},
	{
		item: /cyclic:mattock.+/,
		tags: ["forge:tools/mattocks"]
	},
	{
		item: "redstone_arsenal:flux_sickle",
		tags: ["forge:tools/sickles"]
	},
	{
		item: ["refinedstorage:creative_controller", /refinedstorage:.+_creative_controller/],
		tags: ["refinedstorage:creative_controller"]
	},
	{
		item: /rftoolsdim:.+_dimlet/,
		tags: ["rftoolsdim:dimlet"]
	},
	{
		item: /rftoolsdim:empty_.+_dimlet/,
		tags: ["rftoolsdim:empty_dimlet"]
	}
];

// Remaps blocks with a tag to use another tag instead
// - Key is the tag to replace, value the tag to repalce with
const replaceBlockTags = {};

//---[CODE]---------------------------------------------------------------------------------------------------------------------------------------

// Applies the tags provided above
ServerEvents.tags("block", event => {
	// Adds tags to blocks
	blockToTags.forEach(entry => {
		entry.tags.forEach(tag => {
			event.add(tag, entry.block);
		});
	});
	
	// Replaces tags in blocks
	Object.keys(replaceBlockTags).forEach(tag => {
		event.get(tag).getObjectIds().forEach(item => {
			event.add(replaceBlockTags[tag], item);
		});
		
		event.removeAll(tag);
	});
});

ServerEvents.tags("item", event => {
	// Adds tags to items
	itemToTags.forEach(entry => {
		entry.tags.forEach(tag => {
			event.add(tag, entry.item);
		});
	});
});

ServerEvents.tags("fluid", event => {
	// Applies fluid tag fixes
	if (fixFluidTags)
	{
		event.remove("minecraft:water", /(?!:water)/);
		event.add("minecraft:water", ["minecraft:water", "minecraft:flowing_water"]);
	}
	
	//event.get("minecraft:water").getObjectIds().forEach(id => console.log(id));
});