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

let airStaffCooldown = world.scoreboard.getObjective(`ld_eb_air_cooldown`);
let earthGloveCooldown = world.scoreboard.getObjective(`ld_eb_earth_cooldown`);
let electricityStaffCooldown = world.scoreboard.getObjective(`ld_eb_electricity_cooldown`);
let fireStaffCooldown = world.scoreboard.getObjective(`ld_eb_fire_cooldown`);
let spiritStaffCooldown = world.scoreboard.getObjective(`ld_eb_spirit_cooldown`);
let waterStaffCooldown = world.scoreboard.getObjective(`ld_eb_water_cooldown`);

world.afterEvents.itemUse.subscribe((data) => {
    airStaffCooldown = world.scoreboard.getObjective(`ld_eb_air_cooldown`);
    earthGloveCooldown = world.scoreboard.getObjective(`ld_eb_earth_cooldown`);
    electricityStaffCooldown = world.scoreboard.getObjective(`ld_eb_electricity_cooldown`);
    fireStaffCooldown = world.scoreboard.getObjective(`ld_eb_fire_cooldown`);
    spiritStaffCooldown = world.scoreboard.getObjective(`ld_eb_spirit_cooldown`);
    waterStaffCooldown = world.scoreboard.getObjective(`ld_eb_water_cooldown`);
    
    const item = data.itemStack;
    const player = data.source;

    if (item.typeId === `ld_eb:air_staff`) {
        if (airStaffCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_air_staff.tpp_tornado`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_air_staff.fpp_tornado`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.tornado @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:air_tornado`, { x: player.location.x + player.getViewDirection().x * 3, y: player.location.y, z: player.location.z + player.getViewDirection().z * 3 });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=4] 4 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_air_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:air_staff.air_tornado\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_air_staff.tpp_airspikes`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_air_staff.fpp_airspikes`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.airspikes @a ~~~`)
                    player.dimension.spawnEntity(`ld_eb:air_spikes`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    player.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=18] 5 contact`)
                }, 25);
                player.runCommandAsync(`scoreboard players set @s ld_eb_air_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:air_staff.airspikes\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_air_staff.tpp_airblow`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_air_staff.fpp_airblow`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                player.playAnimation(`animation.ld_eb_air_staff.fpp_airblow_fix`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name3` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.airblow @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:air_blow`, { x: player.location.x + player.getViewDirection().x * 4, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z * 4 });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=3] 2 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_air_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:air_staff.airblow\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:earth_glove`) {
        if (earthGloveCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_earth_glove.tpp_groundwave`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_earth_glove.fpp_groundwave`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                player.playAnimation(`animation.ld_eb_earth_glove.fpp_groundwave_fix`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name3` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.groundwave @a ~~~`)
                    player.dimension.spawnEntity(`ld_eb:earth_groundwave`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    player.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=15] 5 contact`)
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_earth_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:earth_glove.groundwave\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_earth_glove.tpp_rockpunch`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_earth_glove.fpp_rockpunch`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.rockpunch @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:earth_rockpunch`, { x: player.location.x + player.getViewDirection().x, y: player.location.y, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=2] 3 contact`)
                            spell.runCommandAsync(`tp @s ^^^0.5`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 15);
                player.runCommandAsync(`scoreboard players set @s ld_eb_earth_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:earth_glove.rockpunch\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_earth_glove.tpp_rockwall`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_earth_glove.fpp_rockwall`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ld_eb.rockwall @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:earth_rockwall`, { x: player.location.x + player.getViewDirection().x * 4, y: player.location.y, z: player.location.z + player.getViewDirection().z * 4 });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=3] 2 contact`)
                            spell.applyImpulse({ x: spell.getViewDirection().x * 0.05, y: -1, z: spell.getViewDirection().z * 0.05 });
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 15);
                player.runCommandAsync(`scoreboard players set @s ld_eb_earth_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:earth_glove.rockwall\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:electricity_staff`) {
        if (electricityStaffCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_electricity_staff.tpp_electricshower`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_electricity_staff.fpp_electricshower`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.weather.thunder @a ~~~ 0.8`)
                    const spell = player.dimension.spawnEntity(`ld_eb:electricity_electricshower`, { x: player.location.x + player.getViewDirection().x, y: player.location.y, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=5] 3 contact`)
                            spell.runCommandAsync(`tp @s ^^^0.125`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_electricity_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:electricity_staff.electricshower\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_electricity_staff.tpp_electricbeam`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_electricity_staff.fpp_electricbeam`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.weather.thunder @a ~~~ 0.8`)
                    const spell = player.dimension.spawnEntity(`ld_eb:electricity_electricbeam`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^1 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^3 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^5 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^7 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^9 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                    spell.runCommandAsync(`execute as @s at @s positioned ^^^11 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact`)
                }, 58);
                player.runCommandAsync(`scoreboard players set @s ld_eb_electricity_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:electricity_staff.electricbeam\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_electricity_staff.tpp_electricoutburst`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_electricity_staff.fpp_electricoutburst`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.weather.thunder @a ~~~ 0.8`)
                    const spell = player.dimension.spawnEntity(`ld_eb:electricity_electricoutburst`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=7] 5 contact`)
                }, 45);
                player.runCommandAsync(`scoreboard players set @s ld_eb_electricity_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:electricity_staff.electricoutburst\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:fire_staff`) {
        if (fireStaffCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_fire_staff.tpp_firewhirlpool`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_fire_staff.fpp_firewhirlpool`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound mob.blaze.shoot @a ~~~`)

                    const spell = player.dimension.spawnEntity(`ld_eb:fire_firewhirlpool`, { x: player.location.x + player.getViewDirection().x * 2, y: player.location.y, z: player.location.z + player.getViewDirection().z * 2 });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    spell.runCommandAsync(`particle ld_eb:fire_staff_whirlpool_4 ^^^1`)
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=4] 4 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_fire_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:fire_staff.firewhirlpool\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_fire_staff.tpp_firebreath`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_fire_staff.fpp_firebreath`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                player.playAnimation(`animation.ld_eb_fire_staff.fpp_firebreath_fix`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name3` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound mob.blaze.shoot @a ~~~`)

                    const spell = player.dimension.spawnEntity(`ld_eb:fire_firebreath`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`execute as @s at @s positioned ^^^1 run damage @e[type=!item,name=!"${player.name}",r=2] 4 contact`)
                            spell.runCommandAsync(`execute as @s at @s positioned ^^^3 run damage @e[type=!item,name=!"${player.name}",r=2] 4 contact`)
                            spell.runCommandAsync(`execute as @s at @s positioned ^^^5 run damage @e[type=!item,name=!"${player.name}",r=2] 4 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 25);
                player.runCommandAsync(`scoreboard players set @s ld_eb_fire_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:fire_staff.firebreath\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_fire_staff.tpp_firestun`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_fire_staff.fpp_firestun`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound mob.blaze.shoot @a ~~~`)

                    const spell = player.dimension.spawnEntity(`ld_eb:fire_firestun`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    spell.runCommandAsync(`execute positioned ^^^2 run damage @e[type=!item,name=!"${player.name}",r=3.5] 5 contact`)
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_fire_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:fire_staff.firestun\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:spirit_staff`) {
        if (spiritStaffCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_spirit_staff.tpp_celestialcharge`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_celestialcharge`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound break.amethyst_block @a ~~~`)
                    player.runCommandAsync(`execute positioned ~~1~ run particle ld_eb:spirit_celestialcharge_3 ^^^1`)
                    const spell = player.dimension.spawnEntity(`ld_eb:spirit_celestialcharge`, { x: player.location.x + player.getViewDirection().x, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=5] 3 contact`)
                            spell.runCommandAsync(`tp @s ^^^0.4`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_spirit_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:spirit_staff.celestialcharge\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_spirit_staff.tpp_etherealblades`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_etherealblades`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound break.large_amethyst_bud @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:spirit_etherealblades`, { x: player.location.x + player.getViewDirection().x, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            player.runCommandAsync(`playsound break.large_amethyst_bud @a ~~~ 0.2`)
                            spell.runCommandAsync(`execute as @s at @s positioned ^^^1 run damage @e[type=!item,name=!"${player.name}",r=2] 4 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 5);
                player.runCommandAsync(`scoreboard players set @s ld_eb_spirit_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:spirit_staff.etherealblades\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_spirit_staff.tpp_nebulavortex`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_nebulavortex`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound break.amethyst_cluster @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:spirit_nebulavortex`, { x: player.location.x + player.getViewDirection().x, y: player.location.y + 2, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    system.runTimeout(() => {
                        player.runCommandAsync(`playsound break.amethyst_cluster @a ~~~`)
                        spell.runCommandAsync(`execute positioned ^^^2 run damage @e[type=!item,name=!"${player.name}",r=8] 4 contact`)
                    }, 35);
                }, 10);
                player.runCommandAsync(`scoreboard players set @s ld_eb_spirit_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:spirit_staff.nebulavortex\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:water_staff`) {
        if (waterStaffCooldown.getScore(player) == 0) {
            if (!player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_water_staff.tpp_tsunamiwavewall`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_water_staff.fpp_tsunamiwavewall`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.underwater.enter @a ~~~`)

                    const spell = player.dimension.spawnEntity(`ld_eb:water_tsunamiwavewall`, { x: player.location.x + player.getViewDirection().x, y: player.location.y, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=5] 3 contact`)
                            spell.runCommandAsync(`tp @s ^^^0.075`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_water_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:water_staff.tsunamiwavewall\" } ] }`)
            } else if (player.isSneaking && player.isOnGround) {
                player.playAnimation(`animation.ld_eb_water_staff.tpp_whirlpoolformation`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_water_staff.fpp_whirlpoolformation`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.underwater.enter @a ~~~`)
                    const spell = player.dimension.spawnEntity(`ld_eb:water_whirlpoolformation`, { x: player.location.x + player.getViewDirection().x * 2, y: player.location.y, z: player.location.z + player.getViewDirection().z * 2 });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=3] 5 contact`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 40);
                player.runCommandAsync(`scoreboard players set @s ld_eb_water_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:water_staff.whirlpoolformation\" } ] }`)
            } else if (!player.isOnGround) {
                player.playAnimation(`animation.ld_eb_water_staff.tpp_formingwaterbubble`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name1` });
                player.playAnimation(`animation.ld_eb_water_staff.fpp_formingwaterbubble`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    player.runCommandAsync(`playsound ambient.underwater.exit @a ~~~`)
                    player.runCommandAsync(`execute positioned ~~1~ run particle ld_eb:water_staff_tsunamiwavewall_4 ^^^1`)
                    const spell = player.dimension.spawnEntity(`ld_eb:water_formingwaterbubble`, { x: player.location.x + player.getViewDirection().x, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            spell.runCommandAsync(`damage @e[type=!item,name=!"${player.name}",r=5] 3 contact`)
                            spell.runCommandAsync(`tp @s ^^^0.4`)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 1);
                }, 20);
                player.runCommandAsync(`scoreboard players set @s ld_eb_water_cooldown 100`)
                player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb:water_staff.formingwaterbubble\" } ] }`)
            }
        } else {
            player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.cooldown_popup\" } ] }`)
        }
    } else if (item.typeId === `ld_eb:unenchanted_air_staff` || item.typeId === `ld_eb:unenchanted_earth_glove` || item.typeId === `ld_eb:unenchanted_electricity_staff` || item.typeId === `ld_eb:unenchanted_fire_staff` || item.typeId === `ld_eb:unenchanted_spirit_staff` || item.typeId === `ld_eb:unenchanted_water_staff`) {
        player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.unenchanted_popup\" } ] }`)
    } else if (item.typeId === `ld_eb:guide_book_item`) {
        player.dimension.spawnEntity(`ld_eb:guide_book`, { x: player.location.x + player.getViewDirection().x * 2, y: player.location.y, z: player.location.z + player.getViewDirection().z * 2 });
        player.runCommandAsync(`clear @s ld_eb:guide_book_item 0 1`)
    }
})