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

world.afterEvents.itemUse.subscribe((data) => {
	const item = data.itemStack;
	const player = data.source;

	//Check item and cooldown
	if (
		item.typeId === `manalabs_opw:celestial_sword` &&
		item.getComponent("minecraft:cooldown").getCooldownTicksRemaining(player) >
			58
	) {
		//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(
			`particle manalabs_opw:celestial_windswept_target ~ ~ ~`,
		);
		player.runCommandAsync(
			`playsound manalabs_opw.celestial_effect @a[r=20] ~~~ 1 1`,
		);
		//Delay commands to match animation
		system.runTimeout(() => {
			//Play sound and particles
			player.runCommandAsync(
				`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.manalabs_opw:celestial_sword\" } ] }`,
			);
			//Commands to run
			player.runCommandAsync(`effect @s slow_falling 8 0 false`);
			player.runCommandAsync(
				`particle manalabs_opw:celestial_windswept_target ~ ~ ~`,
			);
			player.applyKnockback(0, 0, 0, 2);
		}, 8);
	}
});
