import { world, system, ItemStack, BlockPermutation } from '@minecraft/server';
import * as game from "./game";
import * as vector3 from "./vector3";
import * as quest from "./quest";

const airBlock = BlockPermutation.resolve("air");

const data = [
	// Generic

	{ id: "pond_1", name: "§bJungle Pond§f", range: 26, 
		hint: "§6[§fHint§6]§f A peaceful pond, perfect for some relaxing fishing." },
	{ id: "pond_2", name: "§bAxolotl Pond§f", range: 26, 
		hint: "§6[§fHint§6]§f A paradise for axolotls - bring a bucket, make a friend, and swim like nobody's watching!" },
	{ id: "pond_4", name: "§bRocky Waterfall§f", range: 26, 
		hint: "§6[§fHint§6]§f A peaceful pond, perfect for fishing and relaxing." },

	{ id: "wagon_1", name: "§nSmall Wagon§f", range: 26, 
		hint: "§6[§fHint§6]§f A lone traveler's temporary refuge, still holding the remains of a wandering life." },
	{ id: "wagon_2", name: "§nMedium Wagon§f", range: 26, 
		hint: "§6[§fHint§6]§f Once filled with exotic goods, now left behind as its owner vanished without a trace." },
	{ id: "wagon_3", name: "§nBig Wagon§f", range: 26, 
		hint: "§6[§fHint§6]§f A humble home on wheels bearing the marks of long told journeys. " },
	{ id: "air_balloon_4", name: "§dPurple Air Baloon§f", range: 35, 
		hint: "§6[§fHint§6]§f Left to the mercy of the winds, this balloon drifts aimlessly, its journey forever unfinished." },
	{ id: "air_balloon_5", name: "§bBlue Air Balloon§f", range: 35, 
		hint: "§6[§fHint§6]§f This balloon stands as a silent beacon, inviting the curious to explore unknown horizons." },
	{ id: "air_balloon_6", name: "§aGreen Air Baloon§f", range: 35, 
		hint: "§6[§fHint§6]§f A balloon floats aimlessly in the sky, its basket filled with lost treasures from an unknown traveler, but be careful..." },

	{ id: "excalibur", name: "§7Excalibur§f", range: 41, 
		hint: "§6[§fHint§6]§f A grand monument to the legendary sword - though Excalibur remains in stone, its spirit still calls to the worthy." },
	{ id: "ancient_temple_2", name: "§7Ancient Temple§f", range: 44, 
		hint: "§6[§fHint§6]§f A forgotten ancient temple, its halls filled with treasure... and the echoes of those who never made it out!" },
	{ id: "skeleton_dungeon_top", name: "§fSkeleton Dungeon§f", range: 42, 
		hint: "§6[§fHint§6]§f Bones rattle from deep within the Skeleton Dungeon.\nClear them out before this place becomes a graveyard..." },
	{ id: "creaking_dungeon_top", name: "§nCreaking Dungeon§f", range: 43, 
		hint: "§6[§fHint§6]§f An old dungeon that whispers lost creaky squeaks - tread carefully, or you might become part of the legend!" },
	{ id: "space_rocket", name: "§fSpace Rocket§f", range: 36, 
		hint: "§6[§fHint§6]§f A rocket stranded on this world - was it built for escape, or did its pilot never return?" },
	{ id: "ufo", name: "§dUFO§f", range: 39, 
		hint: "§6[§fHint§6]§f A strange hum surrounds the crashed UFO.\nEndermites skitter around it, born from something not of this world. Clear them out before they spread." },
	{ id: "villager_knight_statue", name: "§7Villager Knight Statue§f", range: 34, 
		hint: "§6[§fHint§6]§f A tribute to the noblest of knights - will you take up their legacy or just loot the place?" },
	{ id: "cozy_campsite", name: "§6Cozy Campsite§f", range: 34, 
		hint: "§6[§fHint§6]§f A small tent, a crackling fire, and a kettle swinging in the breeze - this Cozy Campsite feels like home." },
	{ id: "creeper_statue", name: "§aCreeper Statue§f", range: 37, 
		hint: "§6[§fHint§6]§f A towering Creeper Statue is filled with creepers!\nIts presence draws creepers in swarms, approach carefully or go out with a bang." },

	// Badlands
	{ id: "badlands_hut", name: "§6Badlands Hut§f", range: 37, 
		hint: "§6[§fHint§6]§f This lonely hut stands quiet against the orange cliffs, weathered by sun and time." },
	{ id: "badlands_temple", name: "§6Badlands Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f Hidden in the heart of the Badlands Temple lie veins of gold... and danger." },
	{ id: "campsite", name: "§6Campsite§f", range: 30, 
		hint: "§6[§fHint§6]§f The firewood's dry, the pots are empty. Hunt, cook, and feed those left behind." },
	{ id: "badlands_shelter", name: "§6Badlands Shelter§f", range: 34, 
		hint: "§6[§fHint§6]§f A cosy refuge from the harsh Badlands sun." },
	{ id: "phoenix_monument", name: "§ePhoenix Monument§f", range: 38, 
		hint: "§6[§fHint§6]§f The Phoenix Monument stands cold and lifeless. Only fire can bring it back, defeat the Blazes and reignite its heart." },

	// Beach
	{ id: "lighthouse", name: "§cLighthouse§f", range: 34, 
		hint: "§6[§fHint§6]§f The Lighthouse once kept the darkness at bay. Now Drowned swarm its base, strike back, or the light will fade forever." },
	{ id: "beach_house", name: "§cBeach House§f", range: 34, 
		hint: "§6[§fHint§6]§f A safe retreat nestled by the shore, offering a peaceful ocean view." },
	{ id: "drowned_outpost", name: "§bDrowned Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f Do the Drowned still guard this cold outpost?\nDefeat them and claim their treasure chest." },

	// Beach+Desert
	{ id: "palm_1", name: "§nTall Palm§f", range: 29 },
	{ id: "palm_2", name: "§nOasis Palm§f", range: 30 },
	{ id: "palm_3", name: "§nBig Palm§f", range: 29 },
	{ id: "palm_4", name: "§nBeach Palm§f", range: 26 },
	{ id: "desert_hut", name: "§eDesert Hut§f", range: 26,
		hint: "§6[§fHint§6]§f The Desert Hut looks like a safe retreat from the sun." },
	{ id: "desert_campsite", name: "§eDesert Campsite§f", range: 26,
		hint: "§6[§fHint§6]§f Scorching winds have swept away most traces of life, but fresh tracks suggest someone - or something - is nearby." },

	// Cherry Grove
	{ id: "amethyst_tower", name: "§eAmethyst Tower§f", range: 31,
		hint: "§6[§fHint§6]§f The Amethyst Tower glows strong!\nParkour between amethyst crytals, only the nimble will find the End Crystal waiting at the top." },
	{ id: "hobbit_house", name: "§aHobbit house§f", range: 34, 
		hint: "§6[§fHint§6]§f Tucked amid a vibrant flowery hill, the small hobbit house hides in plain sight like a well-kept secret - a humble retreat for the wandering soul." },
	
	// Desert
	{ id: "desert_haven", name: "§eDesert Haven§f", range: 34, 
		hint: "§6[§fHint§6]§f Looks like a refuge from the scorching desert sun, but is it truly safe, or is there something lurking inside?" },
	{ id: "desert_homestead", name: "§aDesert Homestead§f", range: 36, 
		hint: "§6[§fHint§6]§f Take a moment to rest, but don't stay too long... the desert has many mysteries to explore!" },
	{ id: "small_campsite", name: "§6Small Campsite§f", range: 26, 
		hint: "§6[§fHint§6]§f A lone campfire flickers in the night, a sign that someone was here before..." },
	{ id: "pond_3", name: "§bDesert Pond§f", range: 26, 
		hint: "§6[§fHint§6]§f A tiny water miracle in the middle of the desert..." },
	{ id: "obelisk", name: "§eObelisk§f", range: 35, 
		hint: "§6[§fHint§6]§f The Obelisk crackles with Wither energy.\nWither skeletons patrol its cursed stones. take them down or let the land rot beneath its shadow" },
	{ id: "desert_ruins", name: "§eDesert Ruins§f", range: 44, 
		hint: "§6[§fHint§6]§f Monster spawners hidden in sand breathe life into the Husk horde, destroy them before they grow stronger." },
	{ id: "great_sphinx", name: "§eGreat Sphinx§f", range: 44, 
		hint: "§6[§fHint§6]§f A majestic guardian of the desert, standing watch over lost secrets buried beneath the dunes!" },
	{ id: "desert_pyramid", name: "§eDesert Pyramid§f", range: 44, 
		hint: "§6[§fHint§6]§f Sand and silence fill the Desert Pyramid... and Husk footsteps.\nThey infest the inner chambers - cleanse it, or let the ruin fall completely." },

	// Flowers
	{ id: "bee_statue", name: "§eBee Statue§f", range: 31, 
		hint: "§6[§fHint§6]§f A giant tribute to the gentle pollinators - sweet rewards await those who step inside!" },
	{ id: "sniffer_statue", name: "§cSniffer Statue§f", range: 31, 
		hint: "§6[§fHint§6]§f A grand monument to the ancient Sniffer - step inside and uncover the secrets of forgotten flora!" },
	{ id: "greenhouse", name: "§aGreenhouse§f", range: 39, 
		hint: "§6[§fHint§6]§f A lonely greenhouse stands as a quiet haven - a modest sanctuary where tender blooms thrive under nature's gentle care." },
	{ id: "windmill", name: "§fWindmill§f", range: 39, 
		hint: "§6[§fHint§6]§f The windmill turns with the breeze, once used for grinding grain and pumping water." },

	// Forest
	{ id: "forest_cabin", name: "§aForest Cabin§f", range: 34, 
		hint: "§6[§fHint§6]§f A quiet cabin in the heart of the forest - warm and inviting, but something watches from the trees." },
	{ id: "zombie_statue", name: "§aZombie Statue§f", range: 31, 
		hint: "§6[§fHint§6]§f A towering tribute to the undead - those who enter may never return unchanged." },
	{ id: "ancient_temple", name: "§7Forest Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f Deep within this lush, green forest, an abandoned ancient temple stands in quiet decay - its weathered stones holding the silent echoes of a bygone age." },
	{ id: "zombie_outpost", name: "§7Zombie Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f The Zombie Outpost has fallen to decay and darkness.\nClear them out before this place becomes a graveyard of hope." },
	{ id: "dark_tower", name: "§nDark Tower§f", range: 35, 
		hint: "§6[§fHint§6]§f Dark energy coils around the Dark Tower!\nEvokers chant in shadows, Pillagers watch from above, stop the ritual before it consumes more than the tower." },
	
	// Jungle
	{ id: "ancient_ruins", name: "§7Ancient Ruins§f", range: 34, 
		hint: "§6[§fHint§6]§f Mysterious ruins lay in the forest as a remnant of a civilization long forgotten." },
	{ id: "jungle_hut", name: "§nJungle Hut§f", range: 34, 
		hint: "§6[§fHint§6]§f A secluded hut deep in the jungle. The perfect place for a good night's sleep." },
	{ id: "ruined_tower", name: "§7Ruined Tower§f", range: 34, 
		hint: "§6[§fHint§6]§f A ruined tower, long abandoned, yet something stirs within its broken walls." },
	{ id: "enderman_statue", name: "§dEnderman Statue§f", range: 35, 
		hint: "§6[§fHint§6]§f A towering tribute to the mysterious Endermen - only the most skilled can reach the top and unlock its secrets!" },
	
	// Mooshroom Isles
	{ id: "mushroom_temple", name: "§cMushroom Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f The Mushroom Temple glows softly with fungal light.\nRed mushrooms flourish in every corner, pluck them carefully." },
	{ id: "mushroom_hut", name: "§cMushroom House§f", range: 34, 
		hint: "§6[§fHint§6]§f A quiet home in the heart of mushroom lands, but who once lived here, and why did they leave it behind?" },
	{ id: "mushroom_hut_2", name: "§cMushroom Hut§f", range: 31, 
		hint: "§6[§fHint§6]§f A cozy little hut built from mushroom blocks." },
	{ id: "mushroom_tree", name: "§cMushroom Tree§f", range: 31 },

	// Ocean
	{ id: "sunken_temple", name: "§bDrowned Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f A drowned temple, standing atop an island, holds treasures within, untouched by time and guarded by drowned." },
	{ id: "sunken_temple2", name: "§bOcean Temple§f", range: 34, 
		hint: "§6[§fHint§6]§f Lost in time, this floating temple drifts upon the ocean, hiding forgotten power beneath the waves!" },
	{ id: "ship", name: "§nShip§f", range: 44, 
		hint: "§6[§fHint§6]§f Once a proud vessel, now left adrift upon the open sea, waiting for a new captain!" },
	{ id: "pirate_ship", name: "§nPirate Ship§f", range: 44, 
		hint: "§6[§fHint§6]§f Are you ready to become a captain of this Pirate Ship?" },
	{ id: "frozen_shipwreck", name: "§bFrozen Shipwrek§f", range: 31, 
		hint: "§6[§fHint§6]§f Beneath the frozen wood of this shipwreck, Strays gather.\nThey've broken through the deck - deal with them before they sink what's left." },
	// Plains

	{ id: "village_castle", name: "§nVillage Castle§f", range: 44, 
		hint: "§6[§fHint§6]§f The Village Castle needs help.\nEmeralds are low, and without trade, the people can't survive. Gather what you can, and save the day." },
	{ id: "big_oak_tree", name: "§nBig Oak Tree§f", range: 26 },
	{ id: "market_stand", name: "§cMarket Stand§f", range: 26, 
		hint: "§6[§fHint§6]§f A lively marketplace where traders offer valuable wares to those with the right goods to barter." },
	{ id: "plains_homestead", name: "§nPlains Homestead§f", range: 34, 
		hint: "§6[§fHint§6]§f A peaceful farmstead in the open plains, where hard work and trade keep the land thriving." },
	{ id: "iron_golem_statue", name: "§fIron Golem Statue§f", range: 31, 
		hint: "§6[§fHint§6]§f A mighty guardian shelter for those who seek refuge within its iron heart. Get ready to live inside an Iron Golem statue." },
	{ id: "cottage", name: "§nLakeside Cottage§f", range: 34, 
		hint: "§6[§fHint§6]§f A thriving farmland full of crops, waiting to be harvested - will you reap the rewards or leave it to grow?" },
	{ id: "animal_farm", name: "§aAnimal Farm§f", range: 28, 
		hint: "§6[§fHint§6]§f Milk and wool are needed, but gathering them won't be as simple as it sounds..." },
	{ id: "iron_golem_farm", name: "§fIron Golem Farm§f", range: 28, 
		hint: "§6[§fHint§6]§f A powerful iron farm at work - watch as Iron Golems fall, fueling an endless supply of iron for your needs!" },
	{ id: "tree_house", name: "§nSmall Tree House§f", range: 35, 
		hint: "§6[§fHint§6]§f Can you reach this hidden sanctuary high up in the treetops? You can stay as long as you like." },
	{ id: "giant_tree_house", name: "§nGiant Tree House§f", range: 44, 
		hint: "§6[§fHint§6]§f High above the forest, apples grow in the canopy.\nGather them for the hungry ones, if you can brave the dizzying heights." },
	{ id: "villager_campsite", name: "§6Villager Campsite§f", range: 34, 
		hint: "§6[§fHint§6]§f Left in a hurry, the Villager's Campsite still smells like fresh stew" },
	{ id: "huge_nether_portal", name: "§dHuge Nether Portal§f", range: 31, 
		hint: "§6[§fHint§6]§f The Huge Nether Portal sits dormant, heavy with purpose.\nObsidian is needed - gather the blocks, light the flame, and prepare for what lies beyond." },

	// Savanna
	{ id: "savanna_temple", name: "§6Savanna Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f Deep in the vast savanna, a high temple rises - a silent monument of bygone eras, inviting the curious to trace its ancient treasures. Parkour your way up and claim the treasure!" },
	{ id: "savanna_lodge", name: "§6Savanna Lodge§f", range: 34, 
		hint: "§6[§fHint§6]§f Relax in the warmth of the Savanna Lodge, a peaceful retreat in the vast plains. But keep an eye out - danger may be closer than you think!" },
	{ id: "villager_statue", name: "§nVillager Statue§f", range: 31, 
		hint: "§6[§fHint§6]§f A towering tribute to villagers! Could there be a hidden secret within?" },
	{ id: "mighty_prison_tower", name: "§7Mighty Prison Tower§f", range: 35, 
		hint: "§6[§fHint§6]§f Skeletons have seized the Mighty Prison Tower.\nIts cells now echo with the sound of bone and steel. Break in, clear them out, and reclaim the fortress." },
	{ id: "ancient_tower", name: "§7Ancient Tower§f", range: 29,
		hint: "§6[§fHint§6]§f The Ancient Tower crumbles into the clouds.\nSomewhere near its shattered top, a Beacon shines - if you can reach it without falling to your doom." },
	// Snowy
	{ id: "ice_castle", name: "§bIce Castle§f", range: 41, 
		hint: "§6[§fHint§6]§f The Ice Castle is frozen in time - and crawling with Strays.\nTheir arrows cut sharper than the wind. Warm up your courage and head inside." },
	{ id: "stray_outpost", name: "§bStray Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f Do the Stray still guard this cold outpost? Defeat them and claim their treasure chest." },
	{ id: "ice_temple", name: "§fIce Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f Concealed in plain sight, a hidden room awaits, waiting to be discovered." },
	{ id: "frozen_temple", name: "§fFrozen Temple§f", range: 37, 
		hint: "§6[§fHint§6]§f Walk carefully, Powdered Snow traps await! Get stuck, and you may never escape..." },
	{ id: "snowy_cabin", name: "§nSnowy Cabin§f", range: 34, 
		hint: "§6[§fHint§6]§f Warm up by the fireplace and rest after a long day of adventures." },

	// Swamp
	{ id: "swamp_hideaway", name: "§nSwamp Hideaway§f", range: 34, 
		hint: "§6[§fHint§6]§f A hidden refuge deep within the murky swamp. Explore its secrets, but beware of what lurks in the shadows!" },
	{ id: "witch_outpost", name: "§dWitch Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f Dark magic lingers at the Witch Outpost! Dare to face the witches and claim their mystical treasures." },
	{ id: "wizard_tower", name: "§dWizard Tower§f", range: 34, 
		hint: "§6[§fHint§6]§f Witches lurk in its corners, guarding enchanted books - slay them and recover the magical pages." },
	{ id: "swamp_cabin", name: "§nSwamp Cabin§f", range: 34, 
		hint: "§6[§fHint§6]§f An eerie cabin nestled in the misty swamp. Step inside - if you dare - and find out what lies within!" },
	{ id: "nether_skull_portal", name: "§dNether Skull Portal§f", range: 35, 
		hint: "§6[§fHint§6]§f Built from obsidian and bone, the Nether Skull Portal pulses with dark magic." },
	{ id: "haunted_mansion", name: "§nHaunted Mansion§f", range: 31, 
		hint: "§6[§fHint§6]§f A cursed mansion, filled with whispers of the past - step inside, if you dare!" },

	// Taiga
	{ id: "skeleton_outpost", name: "§7Skeleton Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f Brace yourself for an undead ambush at the Skeleton Outpost!" },
	{ id: "taiga_hall", name: "§nTaiga Hall§f", range: 34, 
		hint: "§6[§fHint§6]§f Step into the heart of the Taiga Hall, where ancient secrets lie. PS: There might be some hidden room..." },
	{ id: "shelter", name: "§nTaiga Shelter§f", range: 34, 
		hint: "§6[§fHint§6]§f A safe haven in the wilderness. Rest, regroup, and prepare for the dangers lurking outside!" },
	{ id: "medieval_tower", name: "§7Knight's Outpost§f", range: 34, 
		hint: "§6[§fHint§6]§f A stronghold of fallen warriors, defeat the raiders, claim their outpost, and restore its honor!" },
	{ id: "graveyard", name: "§7Graveyard§f", range: 31, 
		hint: "§6[§fHint§6]§f A resting place for the lost - yet some refuse to stay buried... disturb the graves at your own risk!" },
	{ id: "beacon_tower", name: "§eBeacon Tower§f", range: 31, 
		hint: "§6[§fHint§6]§f The Beacon Tower shoots light into the sky, but its glow is fading.\nIts foundation shakes as hostile mobs draw near - something wants to extinguish it. Protect the tower... or lose its signal forever." },
	// The End
	{ id: "end_castle", name: "§eEnd Castle§f", range: 40, 
		hint: "§6[§fHint§6]§f Tall and twisted, the End Castle hums with void energy.\nEndermen wander its halls, silent and watching, face them, or flee in fear." },
	{ id: "end_temple", name: "§eEnd Temple§f", range: 34, 
		hint: "§6[§fHint§6]§f An ancient temple pulsing with energy - activate its beacon and discover the secrets within!" },
	{ id: "end_tower", name: "§dEnd Tower§f", range: 34, 
		hint: "§6[§fHint§6]§f Shulkers cling to the walls of the End Tower.\nTheir blasts send intruders flying, only the determined will survive the climb and the fight." },
	{ id: "obsidian_pillar", name: "§dObisidan Pillar§f", range: 26, 
		hint: "§6[§fHint§6]§f An obisidan pillar from another world - its mysteries wait for those brave enough to climb it!" },
	{ id: "end_rock", name: "§eEnd Rock§f", range: 26 }
];
const questNote = "§6[Note] There is a SPECIAL QUEST!§f";

function getData(id)
{
	if (id === undefined)
		return undefined;
	let structureIndex = data.findIndex((element) => element.id === id || element.item === id);
	if (structureIndex === -1)
		return undefined;
	return data[structureIndex];
}

export async function onTick() {
	try {
		let allStructures = await game.getEntities("tcs_ohs:structure");
		for(let structure of allStructures)
		{
			if (structure === undefined)
				continue;
			let structureData = getData(structure.getDynamicProperty("ths_ohs_id"));
			if (structureData === undefined)
				continue;

			let corner1 = vector3.copy(structure.location).subtract(structureData.range);
			let corner2 = vector3.copy(structure.location).add(structureData.range);
			let players = await game.getPlayersInArea(structure.dimension, corner1, corner2, "xz");
			if (players === undefined || players.length == 0)
				continue;


			discover(structure, structureData, corner1, corner2);
			//if (structureData.mobs !== undefined)
			//	spawnMobs(structure, structureData, corner1, corner2);
		}
		
	} catch(error) {}
}
export async function discover(structure, structureData, corner1, corner2)
{
	let structureDiscovered = world.getDynamicProperty(`tcs_ohs_discovered_${structureData.id}`);
	let structureHinted = (structure.getDynamicProperty("tcs_ohs_hint") === true);
	if ((structureData.hint === undefined || structureHinted ) && structureDiscovered === true)
		return;

	let areaSelector = vector3.areaToSelector(corner1.subtract(10), corner2.add(10));

	if (structureDiscovered === undefined)
	{
		world.setDynamicProperty(`tcs_ohs_discovered_${structureData.id}`, true);
		let structuresDiscovered = await game.advanceWorldParam("tcs_ohs_structures_discovered");

		game.tellraw(structure, `@a`, `§f[§aDiscovery§f] ${structureData.name}§f`);
		game.tellraw(structure, `@a`, `§aCongratulations, you discovered ${structuresDiscovered}/${data.length} structures.§f`);
		if (structuresDiscovered < data.length)
		{
			game.tellraw(structure, `@a`, `§aKeep exploring and find them all!§f`);
			structure.runCommandAsync(`playsound tcs_ohs:hint @a ~ ~1 ~ 100`);
		}
		else
		{
			structure.runCommandAsync(`playsound tcs_ohs:hint @a ~ ~1 ~ 100`);
			system.runTimeout(() => { announceDiscoveredAllStructures(); }, 140);
		}
	}
	else
		structure.runCommandAsync(`playsound tcs_ohs:hint @a[${areaSelector}] ~ ~1 ~ 100`);
	if (structureData.hint !== undefined && !structureHinted)
	{
		game.tellraw(structure, `@a[${areaSelector}]`, structureData.hint);
		structure.setDynamicProperty("tcs_ohs_hint", true);
		
		if (structureData.id === "skeleton_dungeon_top")
			game.runMobCommand(structure, `structure load tcs_ohs:skeleton_dungeon_bottom ~-22 ~-29 ~-22`);
		if (structureData.id === "creaking_dungeon_top")
			game.runMobCommand(structure, `structure load tcs_ohs:creaking_dungeon_bottom ~-23 ~-41 ~-21`);
	
		let questData = quest.getData(structureData.id);
		if (questData !== undefined)
			game.tellraw(structure, `@a[${areaSelector}]`, questNote);
	}
}

export async function announceDiscoveredAllStructures()
{
	await game.host.runCommandAsync("title @a subtitle §6You've discovered all the structures!!");
	await game.host.runCommandAsync("title @a title §aCONGRATULATIONS!");
	game.host.runCommandAsync(`playsound tcs_ohs:quest_finished @a ~ ~1 ~ 100`);
	game.host.runCommandAsync(`execute as @a run particle tcs_ohs:quest_floor ~ ~ ~`);
	game.host.runCommandAsync(`execute as @a run particle tcs_ohs:quest_fireworks ~ ~ ~`);
	system.runTimeout(() => { announceDiscoveredAllStructures2(); }, 140);
}
export async function announceDiscoveredAllStructures2()
{
	game.tellraw(game.host, `@a`, `§aYour journey through every biome, structure has proven your unmatched courage, curiosity, and skill.\n\nYou're not just an adventurer, you're a Master Explorer!\n\nGood job!§f`);
	game.host.runCommandAsync(`playsound tcs_ohs:hint @a ~ ~1 ~ 100`);
}



export async function onScriptEventReceive(event) 
{
	if (event.id === "tcs_ohs:structure_load")
	{
		let structureData = getData(event.message.replace("tcs_ohs:",""));
		event.sourceEntity.setDynamicProperty("ths_ohs_id", structureData.id);
		event.sourceEntity.addTag("ths_ohs_"+structureData.id);
		let commandBlockLocation = vector3.Down.add(event.sourceEntity.location);
		event.sourceEntity.dimension.setBlockPermutation(commandBlockLocation, airBlock);
	}
}
export async function removeStructure(structure)
{
	structure.triggerEvent("tcs_ohs:remove");
}

/*
STRUCTURE LOAD COMMANDS
----------------------
These commands can be used to load structures with proper positioning.
Format: structure load tcs_ohs:id ~dx ~ ~dz
Where dx = (x1-x2)/2 and dz = min(-40, z2-z1)

// Generic
Name: Jungle Pond
structure load tcs_ohs:pond_1 ~-7 ~ ~-14
Name: Axolotl Pond
structure load tcs_ohs:pond_2 ~-7 ~ ~-14
Name: Desert Pond
structure load tcs_ohs:pond_3 ~-7 ~ ~-14
Name: Rocky Waterfall
structure load tcs_ohs:pond_4 ~-8 ~ ~-16
Name: Small Wagon
structure load tcs_ohs:wagon_1 ~-6 ~ ~-12
Name: Medium Wagon
structure load tcs_ohs:wagon_2 ~-6 ~ ~-12
Name: Big Wagon
structure load tcs_ohs:wagon_3 ~-7 ~ ~-12
Name: Purple Air Balloon
structure load tcs_ohs:air_balloon_4 ~-14 ~ ~-28
Name: Blue Air Balloon
structure load tcs_ohs:air_balloon_5 ~-14 ~ ~-28
Name: Green Air Balloon
structure load tcs_ohs:air_balloon_6 ~-14 ~ ~-28
Name: Skeleton Dungeon
structure load tcs_ohs:skeleton_dungeon_top ~-21 ~ ~-42
Name: Ancient Temple
structure load tcs_ohs:ancient_temple_2 ~-23 ~ ~-46
Name: Creaking Dungeon
structure load tcs_ohs:creaking_dungeon_top ~-22 ~ ~-44
Name: Space Rocket
structure load tcs_ohs:space_rocket ~-16 ~ ~-32
Name: UFO
structure load tcs_ohs:ufo ~-19 ~ ~-38
Name: Villager Knight Statue
structure load tcs_ohs:villager_knight_statue ~-14 ~ ~-28
Name: Creeper Statue
structure load tcs_ohs:creeper_statue ~-17 ~ ~-34
Name: Excalibur
structure load tcs_ohs:excalibur ~-21 ~ ~-42
Name: Cozy Campsite
structure load tcs_ohs:cozy_campsite ~-11 ~ ~-22

// Badlands
Name: Badlands Hut
structure load tcs_ohs:badlands_hut ~-9 ~ ~-18
Name: Badlands Temple
structure load tcs_ohs:badlands_temple ~-17 ~ ~-34
Name: Campsite
structure load tcs_ohs:campsite ~-10 ~ ~-20
Name: Badlands Shelter
structure load tcs_ohs:badlands_shelter ~-14 ~ ~-28
Name: Phoenix Monument
structure load tcs_ohs:phoenix_monument ~-18 ~ ~-36

// Beach
Name: Lighthouse
structure load tcs_ohs:lighthouse ~-14 ~ ~-28
Name: Beach House
structure load tcs_ohs:beach_house ~-14 ~ ~-28
Name: Drowned Outpost
structure load tcs_ohs:drowned_outpost ~-13 ~ ~-26

// Beach+Desert
Name: Tall Palm
structure load tcs_ohs:palm_1 ~-9 ~ ~-18
Name: Oasis Palm
structure load tcs_ohs:palm_2 ~-9 ~ ~-18
Name: Big Palm
structure load tcs_ohs:palm_3 ~-9 ~ ~-18
Name: Beach Palm
structure load tcs_ohs:palm_4 ~-6 ~ ~-12
Name: Desert Hut
structure load tcs_ohs:desert_hut ~-9 ~ ~-18
Name: Desert Campsite
structure load tcs_ohs:desert_campsite ~-11 ~ ~-22

// Cherry Grove
Name: Amethyst Tower
structure load tcs_ohs:amethyst_tower ~-15 ~ ~-30
Name: Hobbit house
structure load tcs_ohs:hobbit_house ~-12 ~ ~-24

// Desert
Name: Desert Haven
structure load tcs_ohs:desert_haven ~-14 ~ ~-28
Name: Desert Homestead
structure load tcs_ohs:desert_homestead ~-14 ~ ~-28
Name: Small Campsite
structure load tcs_ohs:small_campsite ~-6 ~ ~-12
Name: Desert Campsite
structure load tcs_ohs:desert_campsite ~-11 ~ ~-22
Name: Obelisk
structure load tcs_ohs:obelisk ~-14 ~ ~-28
Name: Desert Ruins
structure load tcs_ohs:desert_ruins ~-23 ~ ~-46
Name: Great Sphinx
structure load tcs_ohs:great_sphinx ~-23 ~ ~-46
Name: Desert Pyramid
structure load tcs_ohs:desert_pyramid ~-19 ~ ~-38

// Flowers
Name: Bee Statue
structure load tcs_ohs:bee_statue ~-11 ~ ~-22
Name: Sniffer Statue
structure load tcs_ohs:sniffer_statue ~-11 ~ ~-22
Name: Greenhouse
structure load tcs_ohs:greenhouse ~-19 ~ ~-38
Name: Windmill
structure load tcs_ohs:windmill ~-13 ~ ~-26

// Forest
Name: Forest Cabin
structure load tcs_ohs:forest_cabin ~-14 ~ ~-28
Name: Zombie Statue
structure load tcs_ohs:zombie_statue ~-11 ~ ~-22
Name: Forest Temple
structure load tcs_ohs:ancient_temple ~-17 ~ ~-34
Name: Zombie Outpost
structure load tcs_ohs:zombie_outpost ~-14 ~ ~-28
Name: Dark Tower
structure load tcs_ohs:dark_tower ~-15 ~ ~-30

// Jungle
Name: Ancient Ruins
structure load tcs_ohs:ancient_ruins ~-14 ~ ~-28
Name: Jungle Hut
structure load tcs_ohs:jungle_hut ~-14 ~ ~-28
Name: Ruined Tower
structure load tcs_ohs:ruined_tower ~-14 ~ ~-28
Name: Enderman Statue
structure load tcs_ohs:enderman_statue ~-15 ~ ~-30

// Mooshroom Isles
Name: Mushroom Temple
structure load tcs_ohs:mushroom_temple ~-17 ~ ~-34
Name: Mushroom House
structure load tcs_ohs:mushroom_hut ~-14 ~ ~-28
Name: Mushroom Hut
structure load tcs_ohs:mushroom_hut_2 ~-10 ~ ~-20
Name: Mushroom Tree
structure load tcs_ohs:mushroom_tree ~-12 ~ ~-24

// Ocean
Name: Drowned Temple
structure load tcs_ohs:sunken_temple ~-12 ~ ~-24
Name: Ocean Temple
structure load tcs_ohs:sunken_temple2 ~-11 ~ ~-22
Name: Ship
structure load tcs_ohs:ship ~-10 ~ ~-44
Name: Pirate Ship
structure load tcs_ohs:pirate_ship ~-11 ~ ~-46
Name: Frozen Shipwrek
structure load tcs_ohs:frozen_shipwreck ~-12 ~ ~-32

// Plains
Name: Village Castle
structure load tcs_ohs:village_castle ~-21 ~ ~-42
Name: Big Oak Tree
structure load tcs_ohs:big_oak_tree ~-6 ~ ~-12
Name: Market Stand
structure load tcs_ohs:market_stand ~-6 ~ ~-12
Name: Plains Homestead
structure load tcs_ohs:plains_homestead ~-14 ~ ~-28
Name: Iron Golem Statue
structure load tcs_ohs:iron_golem_statue ~-11 ~ ~-22
Name: Lakeside Cottage
structure load tcs_ohs:cottage ~-14 ~ ~-28
Name: Animal Farm
structure load tcs_ohs:animal_farm ~-8 ~ ~-16
Name: Iron Golem Farm
structure load tcs_ohs:iron_golem_farm ~-8 ~ ~-16
Name: Small Tree House
structure load tcs_ohs:tree_house ~-15 ~ ~-30
Name: Giant Tree House
structure load tcs_ohs:giant_tree_house ~-23 ~ ~-46
Name: Huge Nether Portal
structure load tcs_ohs:huge_nether_portal ~-23 ~ ~-46
Name: Villager Campsite
structure load tcs_ohs:villager_campsite ~-11 ~ ~-22

// Savanna
Name: Savanna Temple
structure load tcs_ohs:savanna_temple ~-17 ~ ~-34
Name: Savanna Lodge
structure load tcs_ohs:savanna_lodge ~-14 ~ ~-28
Name: Villager Statue
structure load tcs_ohs:villager_statue ~-11 ~ ~-22
Name: Mighty Prison Tower
structure load tcs_ohs:mighty_prison_tower ~-15 ~ ~-30
Name: Ancient Tower
structure load tcs_ohs:ancient_tower ~-13 ~ ~-22

// Snowy
Name: Ice Castle
structure load tcs_ohs:ice_castle ~-21 ~ ~-42
Name: Stray Outpost
structure load tcs_ohs:stray_outpost ~-14 ~ ~-28
Name: Ice Temple
structure load tcs_ohs:ice_temple ~-17 ~ ~-34
Name: Frozen Temple
structure load tcs_ohs:frozen_temple ~-17 ~ ~-34
Name: Snowy Cabin
structure load tcs_ohs:snowy_cabin ~-14 ~ ~-28

// Swamp
Name: Swamp Hideaway
structure load tcs_ohs:swamp_hideaway ~-14 ~ ~-28
Name: Witch Outpost
structure load tcs_ohs:witch_outpost ~-14 ~ ~-28
Name: Wizard Tower
structure load tcs_ohs:wizard_tower ~-14 ~ ~-28
Name: Swamp Cabin
structure load tcs_ohs:swamp_cabin ~-14 ~ ~-28
Name: Nether Skull Portal
structure load tcs_ohs:nether_skull_portal ~-18 ~ ~-32
Name: Haunted Mansion
structure load tcs_ohs:haunted_mansion ~-11 ~ ~-22

// Taiga
Name: Skeleton Outpost
structure load tcs_ohs:skeleton_outpost ~-14 ~ ~-28
Name: Taiga Hall
structure load tcs_ohs:taiga_hall ~-14 ~ ~-28
Name: Taiga Shelter
structure load tcs_ohs:shelter ~-14 ~ ~-28
Name: Knight's Outpost
structure load tcs_ohs:medieval_tower ~-14 ~ ~-28
Name: Beacon Tower
structure load tcs_ohs:beacon_tower ~-9 ~ ~-22
Name: Graveyard
structure load tcs_ohs:graveyard ~-11 ~ ~-22

// The End
Name: End Castle
structure load tcs_ohs:end_castle ~-20 ~ ~-40
Name: End Temple
structure load tcs_ohs:end_temple ~-14 ~ ~-28
Name: End Tower
structure load tcs_ohs:end_tower ~-14 ~ ~-28
Name: Obisidan Pillar
structure load tcs_ohs:obsidian_pillar ~-5 ~ ~-10
Name: End Rock
structure load tcs_ohs:end_rock ~-6 ~ ~-12
*/