import { world, system } from "@minecraft/server";

world.afterEvents.itemUse.subscribe((data) => {
	const item = data.itemStack;
	const player = data.source;
	const playerName = player.name;
	const notTarget = [
		"minecraft:item",
		"minecraft:armor_stand",
		"minecraft:xp_orb",
		"minecraft:arrow",
	];
	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:skull_crusher` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			38
	) {
		//Play animations
		player.playAnimation(`animation.manalabs_opw.first_person_sword_attack`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.any_animation_finished`,
			controller: `controller_name1`,
		});
		player.playAnimation(`animation.manalabs_opw.third_person_sword_attack`, {
			nextState: `none`,
			blendOutTime: 1,
			stopExpression: `query.any_animation_finished`,
			controller: `controller_name2`,
		});

		player.runCommandAsync(
			`playsound manalabs_opw.skullcrusher_attack @a[r=20] ~~~ 1 1`,
		);
		//Delay commands to match animation
		system.runTimeout(() => {
			//Play sound and particles

			player.canSlam = true;
			player.runCommandAsync(`playsound mob.wither.shoot @a[r=20] ~~~ 1 0.4`);
			player.runCommandAsync(`particle minecraft:egg_destroy_emitter ~ ~0.1 ~`);
			player.runCommandAsync(`effect @s slow_falling 1 0 true`);
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:skull_crusher\" } ] }`,
			);
			player.applyKnockback(0, 0, 0, 1);
			player.runCommandAsync(
				`playsound manalabs_opw.skullcrusher_effect @a[r=20] ~~~ 1 1`,
			);
			const entities = player.dimension.getEntities({
				location: {
					x: player.location.x,
					y: player.location.y,
					z: player.location.z,
				},
				maxDistance: 6,
			});
			//Run code for each entity
			entities.forEach((entity) => {
				//Ignore source player and not target array
				if (entity.name == playerName) return;
				if (notTarget.includes(entity.typeId)) return;
				//Commands to run
				entity.runCommandAsync(`damage @s 2 contact`);
				entity.runCommandAsync(
					`particle minecraft:egg_destroy_emitter ~ ~0.1 ~`,
				);
			});
		}, 8);
	}
});
system.runInterval(() => {
	const players = world.getPlayers({});
	for (let b = 0; b < players.length; b++) {
		const player = players[b];
		if (player.canSlam == true && player.isOnGround) {
			const notTarget = [
				"minecraft:item",
				"minecraft:armor_stand",
				"minecraft:xp_orb",
				"minecraft:arrow",
				"manalabs_opw:mini_snow_golem",
				"manalabs_opw:shadow_vex",
				"manalabs_opw:spell_amethyst",
				"manalabs_opw:spell_bubble",
				"manalabs_opw:spell_cactus",
				"manalabs_opw:spell_ice",
				"manalabs_opw:spell_laser",
				"manalabs_opw:spell_poison",
				"manalabs_opw:spell_shadow",
				"manalabs_opw:spell_skull",
			];
			const playerName = player.name;
			const entities = player.dimension.getEntities({
				location: {
					x: player.location.x,
					y: player.location.y,
					z: player.location.z,
				},
				maxDistance: 6,
			});
			//Run code for each entity
			entities.forEach((entity) => {
				//Ignore source player and not target array
				if (entity.name == playerName) return;
				if (notTarget.includes(entity.typeId)) return;
				//Commands to run
				entity.runCommandAsync(`damage @s 8 contact`);
				entity.applyKnockback(
					entity.location.x - player.location.x,
					entity.location.z - player.location.z,
					3,
					0.5,
				);
				entity.runCommandAsync(`particle manalabs_opw:skull_slam_core ~ ~ ~`);
			});
			player.canSlam = false;
			player.runCommandAsync(`particle manalabs_opw:skull_slam_core ~ ~0.1 ~`);
			player.runCommandAsync(`particle manalabs_opw:skull_slam_dust ~ ~0.1 ~`);
			player.runCommandAsync(`particle manalabs_opw:skull_slam_smoke ~ ~0.1 ~`);
			player.runCommandAsync(
				`playsound manalabs_opw.skullcrusher_effect_landing @a[r=20] ~~~ 1 1`,
			);
			player.runCommandAsync(
				`execute at @s positioned ~~-1~ run function manalabs/opw/break_blocks`,
			);
		}
	}
});
