import "./items/index"
import "./test"
import "./spawnReset"
import "./entities/index"
import * as lib from "./lib"
import "./start_book"
import { Effect, EffectType, EffectTypes, Entity, EntityProjectileComponent, ItemStack, 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) => {
    const sound = world.getDynamicProperty("ld_eb:efeitos_sonoros")

    const item = data.itemStack;
    const player = data.source;

    if (player.isSneaking && !player.isOnGround) return
    if (item.typeId === `ld_eb:air_staff`) {
        if (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 4 * 20)
            // tornado
            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(() => {
                    if(sound){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) {
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 6,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "ld_eb:none"],
                                excludeFamilies: ["ld_eb:none"]
                            })
                            entities.forEach(entity => {
                                if (entity === player) return
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (lib.damage.baixo/5), {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                lib.middleImpulse(entity, spell, 2, 0)
                            })
                        } 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(() => {
                    if(sound){player.runCommandAsync(`playsound ld_eb.airspikes @a ~~~`)}
                    const spell = player.dimension.spawnEntity(`ld_eb:air_spikes`, { x: player.location.x + player.getViewDirection().x * 3, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z * 3});
                    {
                        const parLoc = spell.location
                        player.dimension.spawnParticle(`ld_eb:air_staff_tornado_1`, parLoc)
                        for (let index = 1; index <= 3; index++) {
                            player.dimension.spawnParticle(`ld_eb:air_staff_airspikes_${index}`, parLoc)
                        }
                    }
                    player.addEffect("slow_falling", 2 * 20, { amplifier: 1, showParticles: false })
                    const entities = spell.dimension.getEntities({
                        location: spell.location,
                        maxDistance: 18,
                        excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb"],
                        excludeFamilies: ["ld_eb:none"]
                    })
                    entities.forEach(entity => {
                        if (entity.typeId === "minecraft:player") return 
                            try {entity.applyKnockback(0, 0, 0, 1)} catch (error) {}
                            entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (lib.damage.moderado/5), {
                            cause: "contact",
                            damagingEntity: player
                        })
                        try {entity.applyKnockback(0, 0, 0, 12)} catch (error) {}
                    })
                }, 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(() => {
                    if(sound){player.runCommandAsync(`playsound ld_eb.airblow @a ~~~`)}
                    const spell = player.dimension.spawnEntity(`ld_eb:air_blow`, { x: player.location.x + player.getViewDirection().x * 1, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z * 1 });
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            const position = { x: player.location.x + player.getViewDirection().x * 1, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z * 1 }
                            spell.teleport(position)
                            spell.setRotation({ x: player.getRotation().x, y: player.getRotation().y })
                            lib.rayCast(player, 2, 5, /** @param { Entity } entity */ function (entity) {
                                if (entity === player) 
                                try {entity.applyKnockback(
                                    spell.getViewDirection().x,
                                    spell.getViewDirection().z,
                                    0.3,
                                    0.1
                                )} catch (error) {}
                                
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (lib.damage.alto/5), {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                            })
                            // const entities = spell.dimension.getEntities({
                            //     location: position,
                            //     maxDistance: 2,
                            //     excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow"]
                            // })
                            // entities.forEach(entity => {
                            // })
                        } 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 (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 4 * 20)
            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(() => {
                    if(sound){player.runCommandAsync(`playsound ld_eb.groundwave @a ~~~`)}
                    const spell = player.dimension.spawnEntity(`ld_eb:earth_groundwave`, { x: player.location.x + player.getViewDirection().x*2, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z*2 });
                    const entities = spell.dimension.getEntities({
                        location: spell.location,
                        maxDistance: 8,
                        excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", spell.typeId],
                        excludeFamilies: ["ld_eb:none"],
                        excludeNames: [player.nameTag]
                    })
                    {
                        const parLoc = {
                            x: spell.location.x,
                            y: spell.location.y+0.6,
                            z: spell.location.z
                        }
                        spell.dimension.spawnParticle(`ld_eb:earth_groundwave_1`, parLoc)
                        spell.dimension.spawnParticle(`ld_eb:earth_groundwave_2`, parLoc)
                        spell.dimension.spawnParticle(`ld_eb:earth_groundwave_3`, parLoc)
                        spell.dimension.spawnParticle(`ld_eb:earth_groundwave_4`, parLoc)
                    }
                    entities.forEach(entity => {
                        entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.baixo, {
                            cause: "contact",
                            damagingEntity: player
                        })
                        try {entity.applyKnockback(
                            spell.getViewDirection().x,
                            spell.getViewDirection().z,
                            0.4,
                            2
                        )} catch (error) {}
                        
                    })
                }, 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.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(async () => {
                    const spell = player.dimension.spawnEntity(`ld_eb:earth_rockpunch_1`, { x: player.location.x + player.getViewDirection().x * 1.3, y: player.location.y + 1, z: player.location.z + player.getViewDirection().z * 1.3 });

                    await system.waitTicks(0.3 * 20);
                    if(sound){player.runCommandAsync(`playsound ld_eb.rockpunch @a ~~~`)}
                    //
                    const projectile = player.dimension.spawnEntity("ld_eb:ld_eb_rock_punch_projectile", {
                        x: player.getHeadLocation().x + player.getViewDirection().x,
                        y: player.getHeadLocation().y - 0.5 + player.getViewDirection().y,
                        z: player.getHeadLocation().z + player.getViewDirection().z,
                    })
                    /** @type {EntityProjectileComponent} */
                    const projectileComponent = projectile.getComponent(EntityProjectileComponent.componentId)
                    projectileComponent.owner = player
                    projectileComponent.gravity = 0.01
                    let projectileShootVelocity = 1
                    projectileComponent.shoot({
                        x: player.getViewDirection().x * projectileShootVelocity,
                        y: player.getViewDirection().y * projectileShootVelocity,
                        z: player.getViewDirection().z * projectileShootVelocity
                    })
                    let locationOfProjetcile = projectile.location
                    const projectiletick = system.runInterval(async () => {
                        if (!projectile.isValid()) {
                            player.dimension.spawnParticle("ld_eb:earth_rockpunch_1", locationOfProjetcile)
                            player.dimension.spawnParticle("ld_eb:earth_rockpunch_2", locationOfProjetcile)
                            player.dimension.spawnParticle("ld_eb:earth_rockpunch_3", locationOfProjetcile)
                            player.dimension.spawnParticle("ld_eb:earth_rockpunch_4", locationOfProjetcile)
                            player.dimension.spawnParticle("ld_eb:earth_rockpunch_5", locationOfProjetcile)
                            if(sound){player.runCommandAsync(`playsound ld_eb.rockpunch @a ~~~`)}
                            await system.waitTicks(0.1 * 20)
                            player.dimension.getEntities({
                                location: locationOfProjetcile,
                                maxDistance: 7,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", projectile.typeId],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            }).forEach(entity => {
                                lib.middleImpulse(entity, { location: locationOfProjetcile }, -4, 0.5)
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * 10)
                            })
                            system.clearRun(projectiletick)
                            return
                        }
                        locationOfProjetcile = projectile.location
                        const entities = player.dimension.getEntities({
                            location: projectile.location,
                            maxDistance: 4,
                            excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", projectile.typeId],
                            excludeFamilies: ["ld_eb:none"],
                            excludeNames: [player.nameTag]
                        })
                        entities.forEach(entity => {
                            try {entity.applyKnockback(
                                player.getViewDirection().x,
                                player.getViewDirection().z,
                                1,
                                0.01
                            )} catch (error) {}
                            
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.moderado, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                        })
                    }, 0.1 * 20)
                    await system.waitTicks(1 * 20);
                    spell.remove()
                }, 0.5 * 20);
                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(() => {
                    if(sound){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.applyImpulse({ x: spell.getViewDirection().x * 0.2, y: -1, z: spell.getViewDirection().z * 0.2 });
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 4,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", spell.typeId],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            })
                            entities.forEach(entity => {
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.alto, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                try {entity.applyKnockback(
                                        spell.getViewDirection().x,
                                        spell.getViewDirection().z,
                                        1,
                                        0.0
                                    )
                                } catch (error) {}
                            })
                        } 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 (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 4 * 20)
            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(() => {
                    if(sound){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) {
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 5,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", spell.typeId],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            })
                            entities.forEach(entity => {
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * 3, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                try {entity.applyKnockback(
                                        spell.getViewDirection().x,
                                        spell.getViewDirection().z,
                                        1,
                                        0.0
                                    )
                                } catch (error) {}
                            })
                            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(() => {
                    if(sound){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 * 2, y: player.location.y - 0.5, z: player.location.z + player.getViewDirection().z * 2});
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    lib.rayCast(player, 3, 7, /** @param { Entity } entity */ function (entity) {
                        if (entity === player) return
                        try {entity.applyKnockback(
                                player.getViewDirection().x,
                                player.getViewDirection().z,
                                10,
                                0.01
                            )
                        } catch (error) {}
                        entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.moderado, {
                            cause: "contact",
                            damagingEntity: player
                        })
                        entity.addEffect("slowness", 10 * 20, {
                            amplifier: 2,
                            showParticles: false
                        })
                    })
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^1 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^3 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^5 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^7 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^9 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^11 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^1 run damage @e[type=!item,name=!"${player.name}",r=2] 5 contact entity @s`)

                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^3 as @e[type=!item,name=!"${player.name}",r=2] run effect @s slowness 2 10 false`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^5 as @e[type=!item,name=!"${player.name}",r=2] run effect @s slowness 2 10 false`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^7 as @e[type=!item,name=!"${player.name}",r=2] run effect @s slowness 2 10 false`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^9 as @e[type=!item,name=!"${player.name}",r=2] run effect @s slowness 2 10 false`)
                    // spell.runCommandAsync(`execute as @s at @s positioned ^^^11 as @e[type=!item,name=!"${player.name}",r=2] run effect @s slowness 2 10 false`)
                }, 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(() => {
                    if(sound){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 });
                    lib.rayCast(player, 7, 1, /** @param { Entity } entity */ function (entity) {
                        if (entity === player) return
                        // entity.applyKnockback(
                        //     player.getViewDirection().x,
                        //     player.getViewDirection().z,
                        //     10,
                        //     0.01
                        // )
                        lib.middleImpulse(entity, player, -10, 0.1)
                        entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.alto, {
                            cause: "contact",
                            damagingEntity: player
                        })
                        entity.addEffect("slowness", 10 * 20, {
                            amplifier: 2,
                            showParticles: false
                        })
                    })
                }, 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 (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 4 * 20)
            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(() => {
                    if(sound){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) {

                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 6,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow"],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            });

                            entities.forEach(entity => {
                                if (!spell.isValid() || entity == player) return
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.baixo, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                entity.addEffect("slowness", 3*20, {
                                    showParticles: false,
                                    amplifier: 5
                                })
                                if(entity != player) {
                                    entity.setOnFire(1)
                                }
                                lib.middleImpulse(entity, spell, 2, 0)
                            })

                        } 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(() => {
                    if(sound){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, z: player.location.z + player.getViewDirection().z });
                    spell.setRotation({ x: 0, y: player.getRotation().y })
                    const interval = system.runInterval(async () => {
                        if (!spell.isValid()) return system.clearRun(interval)
                        lib.rayCast(player, 2, 5, /** @param { Entity } entity */ function (entity) {
                            if (entity === player) return
                            try {entity.applyKnockback(
                                    spell.getViewDirection().x,
                                    spell.getViewDirection().z,
                                    0.1,
                                    0.01
                                )
                            } catch (error) {}
                            entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.moderado, {
                                cause: "contact",
                                damagingEntity: player
                            })
                            entity.addEffect("slowness", 3*20, {
                                showParticles: false,
                                amplifier: 5
                            })
                            if(entity != player) {
                                entity.setOnFire(1)
                            }
                        })
                        spell.teleport({ x: player.location.x + player.getViewDirection().x, y: player.location.y - 0, z: player.location.z + player.getViewDirection().z })
                        spell.setRotation({ x: 0, y: player.getRotation().y })
                    }, 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(() => {
                    lib.rayCast(player, 3.5, 1, /** @param { Entity } entity */ function (entity) {
                        if (entity === player) return
                        lib.middleImpulse(entity, player, -4, 0.5)
                        entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.alto, {
                            cause: "contact",
                            damagingEntity: player
                        })
                        if(entity != player) {
                            entity.setOnFire(5)
                        }
                        entity.addEffect("slowness", 3*20, {
                            showParticles: false,
                            amplifier: 5
                        })
                    })
                    player.addEffect("fire_resistance", 8 * 20, { amplifier: 1, showParticles: false })
                    if(sound){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 });
                }, 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 (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 4 * 20)
            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` });
                system.runTimeout(() => {
                    lib.animationFilled(player, "animation.ld_eb_spirit_staff.idle_player", item.typeId, 2, true)
                }, 1.5 * 20)
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_celestialcharge`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    if(sound){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.dimension.getEntities({maxDistance:4, location:spell.location, excludeTypes:['minecraft:item', 'minecraft:xp_orb']}).forEach(entity=>{
                                if (entity === player) return
                                lib.middleImpulse(entity, player, -4, 0.5)
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.baixo, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                if(entity != player) {
                                    entity.setOnFire(5)
                                }
                            })
                            spell.runCommandAsync(`tp @s ^^^0.4`)
                            spell.setRotation({ x: player.getRotation().x, y: player.getRotation().y })
                        } 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` });
                system.runTimeout(() => {
                    lib.animationFilled(player, "animation.ld_eb_spirit_staff.idle_player", item.typeId, 2, true)
                }, 0.5 * 20)
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_etherealblades`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    if(sound){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 });
                    player.dimension.spawnParticle("ld_eb:spirit_ethereal_blades_2", player.location)
                    player.dimension.spawnParticle("ld_eb:spirit_ethereal_blades_1", player.location)
                    spell.setRotation({ x: 0, y: player.getRotation().y });
                    const interval = system.runInterval(() => {
                        if (spell.isValid() == true) {
                            lib.rayCast(player, 3, 1, /** @param { Entity } entity */ function (entity) {
                                if (entity === player) return
                                lib.middleImpulse(entity, player, -0.2, 0.05)
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.moderado, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                            })
                            if(sound){player.runCommandAsync(`playsound break.large_amethyst_bud @a ~~~ 0.2`)}
                            player.dimension.spawnParticle("ld_eb:spirit_ethereal_blades_3", player.location)
                        } else {
                            system.clearRun(interval)
                        }
                    }, 0.2 * 20);
                }, 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` });
                system.runTimeout(() => {
                    lib.animationFilled(player, "animation.ld_eb_spirit_staff.idle_player", item.typeId, 2, true)
                }, 1.5 * 20)
                player.playAnimation(`animation.ld_eb_spirit_staff.fpp_nebulavortex`, { nextState: `none`, blendOutTime: 1, stopExpression: `query.any_animation_finished`, controller: `controller_name2` });
                system.runTimeout(() => {
                    if(sound){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(() => {
                        if(sound){player.runCommandAsync(`playsound break.amethyst_cluster @a ~~~`)}
                        // spell.runCommandAsync(`execute positioned ^^^2 run damage @e[type=!item,name=!"${player.name}",r=8] 4 contact entity @s`)
                        const tick = system.runInterval(() => {
                            if (!spell.isValid()) return system.clearRun(tick)
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 8,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow"],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            });

                            entities.forEach(entity => {
                                if (!entity.isValid()) return
                                const directionX = spell.location.x - entity.location.x;
                                const directionZ = spell.location.z - entity.location.z;

                                const distance = Math.sqrt(directionX ** 2 + directionZ ** 2);

                                if (distance > 0) {
                                    const normalizedX = directionX / distance;
                                    const normalizedZ = directionZ / distance;

                                    try{entity.applyKnockback(
                                        normalizedX,
                                        normalizedZ,
                                        0.5,
                                        0.06
                                    )}catch(e){}
                                    entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.alto, {
                                        cause: "contact",
                                        damagingEntity: player
                                    })
                                }
                            })
                        }, 0.2 * 20)
                    }, 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 (player.getItemCooldown(item.typeId) === 0) {
            player.startItemCooldown(item.typeId, 10 * 20)
            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(() => {
                    if(sound){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 entity @s`)
                            spell.runCommandAsync(`tp @s ^^^0.3`)
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 5,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", spell.typeId],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            })
                            entities.forEach(entity => {
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * 3, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                try { entity.applyKnockback(spell.getViewDirection().x, spell.getViewDirection().z, 5, 0.6); } catch (e) {}
                            })
                        } 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(() => {
                    if(sound){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) {
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 6,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow"],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            });

                            entities.forEach(entity => {
                                if (!spell.isValid()) return
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.baixo, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                const directionX = spell.location.x - entity.location.x;
                                const directionZ = spell.location.z - entity.location.z;

                                const distance = Math.sqrt(directionX ** 4 + directionZ ** 4);

                                if (distance > 0) {
                                    const normalizedX = directionX / distance;
                                    const normalizedZ = directionZ / distance;

                                    try { entity.applyKnockback(normalizedX, normalizedZ, 6, 0.06); } catch (e) {}
                                }
                            })
                        } 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(() => {
                    if(sound){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(`tp @s ^^^0.4`)
                            const entities = spell.dimension.getEntities({
                                location: spell.location,
                                maxDistance: 5,
                                excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:arrow", "minecraft:xp_orb", "minecraft:arrow", spell.typeId],
                                excludeFamilies: ["ld_eb:none"],
                                excludeNames: [player.nameTag]
                            })
                            entities.forEach(entity => {
                                entity.applyDamage((player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) * lib.damage.alto, {
                                    cause: "contact",
                                    damagingEntity: player
                                })
                                try { entity.applyKnockback(spell.getViewDirection().x, spell.getViewDirection().z, 1, player.getViewDirection().y * 0.4); } catch (e) {}
                            })
                            spell.setRotation({ x: player.getRotation().x, y: player.getRotation().y })
                        } 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` ||
        item.typeId === `ld_eb:weapons.cetro_do_vazio.unechanted` ||
        item.typeId === `ld_eb:weapons.glaive_congelada.unechanted` ||
        item.typeId === `ld_eb:weapons.martelo_de_metal.unechanted` ||
        item.typeId === `ld_eb:weapons.punhos_derretidos.unechanted`) {
        if(world.getDynamicProperty('ld_eb:efeitos_sonoros')){player.runCommand(`playsound note.bass @s`)}
        player.runCommandAsync(`titleraw @p actionbar { \"rawtext\": [ { \"translate\" : \"actionbar.ld_eb.unenchanted_popup\" } ] }`)
    }
    // if (item.typeId === `ld_eb:guide_book_itema`) {
    //     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`)
    // }
})
// world.afterEvents.entitySpawn.subscribe(data => {
//     const entity = data.entity
//     if (entity.typeId !== "minecraft:item") return
//     let item = entity.getComponent("minecraft:item").itemStack
//     //console1-console.warn(`nametag: ${item.typeId}`)
// })
world.afterEvents.entityDie.subscribe(async (data) => {
    if (!data.damageSource.damagingEntity || data.damageSource.damagingEntity?.typeId !== "minecraft:player") return
    const entity = data.deadEntity,
        cause = data.damageSource.cause,
        { x, y, z } = entity.location,
        dimension = entity.dimension,
        deather = data.damageSource.damagingEntity ? data.damageSource.damagingEntity : data.damageSource.damagingProjectile

    const validEntities = [
        "ld_eb:fire_firebreath",
        "ld_eb:fire_firewhirlpool",
        "ld_eb:fire_firestun"
    ]
    if (!validEntities.includes(deather.typeId)) return
    //console1-console.warn(deather.typeId)

    await system.waitTicks(1)
    const itemEntities = dimension.getEntities({
        type: "minecraft:item",
        location: { x, y, z },
        maxDistance: 4,
    })
    itemEntities.forEach(itemEntity => {
        const item = itemEntity.getComponent("minecraft:item").itemStack
        let newId = item.typeId
        switch (newId) {
            case "minecraft:beef":
                newId = "minecraft:cooked_beef";
                break;
            case "minecraft:chicken":
                newId = "minecraft:cooked_chicken";
                break;
            case "minecraft:mutton":
                newId = "minecraft:cooked_mutton";
                break;
            case "minecraft:porkchop":
                newId = "minecraft:cooked_porkchop";
                break;
            case "minecraft:rabbit":
                newId = "minecraft:cooked_rabbit";
                break;
            case "minecraft:salmon":
                newId = "minecraft:cooked_salmon";
                break;
            case "minecraft:cod":
                newId = "minecraft:cooked_cod";
                break;
        }
        itemEntity.dimension.spawnItem(
            new ItemStack(newId, 1),
            itemEntity.location
        )
        itemEntity.remove()
    })
})
