import * as Minecraft from "@minecraft/server"
import "./morph.js"
import "./blocks.js"
import "./crops.js"
import "./tree.js"
import "./fog.js"
import "./shield.js"
import "./decorations.js"
import "./weapons.js"
import "./night.js"
import "./corpse_mob.js"
import "./spawn_rule.js"
import "./buff.js"
import "./slice_blocks.js"
import "./arena.js"
import "./food.js"
import "./interface.js"
import "./morph_menu.js"

import "./chicken.js"



const shadowArmorCounter = new Map()
const boneArmorCounter = new Map()
const flashlightDurability = new Map()
let hostId = null



Minecraft.system.run(function tick() {
    Minecraft.system.run(tick)
    for (const dynamicProperty of Minecraft.world.getDynamicPropertyIds()) {
        if (dynamicProperty.startsWith("aria_ah.flashlight.location.")) {
            const playerId = dynamicProperty.split(".")[3]
            const location = Minecraft.world.getDynamicProperty(dynamicProperty)
            const dimension = Minecraft.world.getDimension(Minecraft.world.getDynamicProperty("aria_ah.flashlight.dimension." + playerId))

            const volume = Minecraft.world.scoreboard.getObjective("aria_ah.general").getScore("fix_light") ? `${location.x - 1} ${location.y - 1} ${location.z - 1} ${location.x + 1} ${location.y + 1} ${location.z + 1}` : `${location.x} ${location.y} ${location.z} ${location.x} ${location.y} ${location.z}`
            let blocksFound = dimension.runCommand(`fill ${volume} minecraft:air replace minecraft:light_block_14`).successCount
            if (dimension.runCommand(`fill ${volume} minecraft:air replace minecraft:light_block_8`).successCount) {
                blocksFound = 1
            }
            if (dimension.runCommand(`fill ${volume} minecraft:air replace minecraft:light_block_5`).successCount) {
                blocksFound = 1
            }
            if (blocksFound) {
                Minecraft.world.setDynamicProperty(dynamicProperty)
                Minecraft.world.setDynamicProperty("aria_ah.flashlight.dimension." + playerId)
            }
        } else if (dynamicProperty.startsWith("aria_ah.light_ray.location.")) {
            const playerId = dynamicProperty.split(".")[3]
            const blockId = dynamicProperty.split(".")[4]
            const time = dynamicProperty.split(".")[5]
            const location = Minecraft.world.getDynamicProperty(dynamicProperty)
            const dimension = Minecraft.world.getDimension(Minecraft.world.getDynamicProperty(`aria_ah.light_ray.dimension.${playerId}.${blockId}.${time}`))

            if (Date.now() > time) {
                dimension.runCommand(`fill ${location.x} ${location.y} ${location.z} ${location.x} ${location.y} ${location.z} minecraft:air replace minecraft:light_block_15`)
                Minecraft.world.setDynamicProperty(dynamicProperty)
                Minecraft.world.setDynamicProperty(`aria_ah.light_ray.dimension.${playerId}.${blockId}.${time}`)
            }
        }
    }

    for (const player of Minecraft.world.getPlayers()) {
        let handItem = player.getComponent("inventory").container.getItem(player.selectedSlotIndex)
        const playerEquipment = player.getComponent("equippable")
        let offhand = false
        if (handItem?.typeId != "aria_ah:flashlight_on") {
            handItem = playerEquipment.getEquipment(Minecraft.EquipmentSlot.Offhand)
            offhand = true
        }

        if (handItem?.typeId == "aria_ah:flashlight_on" || handItem?.typeId == "aria_ah:flashlight_off") {
            const durability = handItem?.getComponent(Minecraft.ItemComponentTypes.Durability)

            if (durability?.damage < durability?.maxDurability && handItem.typeId == "aria_ah:flashlight_on") {
                let brightness = [5, 8, 14]
                let damagePercent = ((durability.maxDurability - durability.damage) / durability.maxDurability)
                if (handItem.getDynamicProperty("aria_ah.flashlight.disabledUntil") > Date.now()) {
                    continue
                } else if ((damagePercent <= 0.1 && Math.floor(Math.random() * 100) < damagePercent * 150)) {
                    handItem.setDynamicProperty("aria_ah.flashlight.disabledUntil", Date.now() + 5000 * damagePercent)
                    if (offhand) {
                        playerEquipment.setEquipment(Minecraft.EquipmentSlot.Offhand, handItem)
                    } else {
                        player.getComponent("inventory").container.setItem(player.selectedSlotIndex, handItem)
                    }
                    player.runCommand(`playsound aria_ah.light_flicker @a[r=4]`)
                    continue
                }

                brightness = brightness[damagePercent <= 0.33 ? 0 : damagePercent <= 0.66 ? 1 : 2]

                for (let i = 1; i <= 15; i++) {
                    const loc = player.getViewDirection()
                    let x = player.location.x + loc.x * i
                    let y = player.location.y + loc.y * i + 1
                    let z = player.location.z + loc.z * i
                    const blockExist = player.runCommand(`testforblock ^ ^1 ^${i} minecraft:air`).successCount

                    if (!blockExist) {
                        x = player.location.x + loc.x * (i - 1)
                        y = player.location.y + loc.y * i + 2
                        z = player.location.z + loc.z * (i - 1)
                        if (i == 1) {
                            if (player.runCommand(`testforblock ${player.location.x} ${player.location.y} ${player.location.z} minecraft:air`).successCount) {
                                x = player.location.x
                                y = player.location.y
                                z = player.location.z
                            } else if (player.runCommand(`testforblock ${player.location.x} ${player.location.y + 1} ${player.location.z} minecraft:air`).successCount) {
                                x = player.location.x
                                y = player.location.y + 1
                                z = player.location.z
                            } else {
                                x = player.location.x
                                y = player.location.y + 2
                                z = player.location.z
                            }
                        }
                        player.runCommand(`fill ${x} ${y} ${z} ${x} ${y} ${z} minecraft:light_block["block_light_level"=${brightness}] replace air`)

                        Minecraft.world.setDynamicProperty("aria_ah.flashlight.location." + player.id, { x, y, z })
                        Minecraft.world.setDynamicProperty("aria_ah.flashlight.dimension." + player.id, player.dimension.id)
                        break
                    } else if (i == 15) {
                        if (!player.runCommand(`testforblock ${x} ${y} ${z} minecraft:air`).successCount) {
                            y += 1
                            if (!player.runCommand(`testforblock ${x} ${y} ${z} minecraft:air`).successCount) {
                                break
                            }
                        }
                        player.runCommand(`fill ${x} ${y} ${z} ${x} ${y} ${z} light_block["block_light_level"=${brightness}] replace air`)

                        Minecraft.world.setDynamicProperty("aria_ah.flashlight.location." + player.id, { x, y, z })
                        Minecraft.world.setDynamicProperty("aria_ah.flashlight.dimension." + player.id, player.dimension.id)
                    }
                }
            }

            if (handItem.typeId == "aria_ah:flashlight_on" || handItem.typeId == "aria_ah:flashlight_off") {
                if (durability.damage == durability.maxDurability) {
                    const playerInventory = player.getComponent("inventory").container
                    
                    for (let i = 0; i < playerInventory.size; i++) {
                        const item = playerInventory.getItem(i)
                        if (item?.typeId == "aria_ah:battery") {
                            if (item.amount > 1) {
                                item.amount--
                                playerInventory.setItem(i, item)
                            } else {
                                playerInventory.setItem(i, null)
                            }

                            handItem.getComponent(Minecraft.ItemComponentTypes.Durability).damage = 0
                            if (offhand) {
                                playerEquipment.setEquipment(Minecraft.EquipmentSlot.Offhand, handItem)
                            } else {
                                playerInventory.setItem(player.selectedSlotIndex, handItem)
                            }
                            break
                        }
                    }
                }
            }
        }

        if (
            player.isSneaking
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Head)?.typeId == "aria_ah:shadow_boss_helmet"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Chest)?.typeId == "aria_ah:shadow_boss_chestplate"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Legs)?.typeId == "aria_ah:shadow_boss_leggings"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Feet)?.typeId == "aria_ah:shadow_boss_boots"
        ) {
            if (!shadowArmorCounter.get(player.id) || shadowArmorCounter.get(player.id) < 50) {
                const currentCount = shadowArmorCounter.get(player.id) || 0;
                shadowArmorCounter.set(player.id, currentCount + 1);

                // Start sound on first activation
                if (currentCount === 0) {
                    player.runCommand("playsound aria_ah.fog_loop @s");
                }

                const actionBarText = ["█", "█", "█", "█", "█"];
                actionBarText.splice(Math.floor(shadowArmorCounter.get(player.id) / 10), 0, "§8");
                player.onScreenDisplay.setActionBar("§a" + actionBarText.join(""));
            } else if (shadowArmorCounter.get(player.id) == 50) {
                player.runCommand("function aria/ah/armor_dark_fog");
                shadowArmorCounter.set(player.id, 51);

                // Stop the fog loop sound when ability triggers
                player.runCommand("stopsound @s aria_ah.fog_loop");
            } else {
                player.onScreenDisplay.setActionBar({
                    translate: "aria_ah.cooldown.seconds",
                    with: [(10 - Math.floor((shadowArmorCounter.get(player.id) - 50) / 20)).toString()]
                });
            }
        } else if (!player.isSneaking && shadowArmorCounter.get(player.id) < 50) {
            // Stop the fog loop sound if sneaking is interrupted
            player.runCommand("stopsound @s aria_ah.fog_loop");
            shadowArmorCounter.delete(player.id);
        }

        if (shadowArmorCounter.get(player.id) >= 51) {
            shadowArmorCounter.set(player.id, (shadowArmorCounter.get(player.id)) + 1);
            if (shadowArmorCounter.get(player.id) >= 251) {
                shadowArmorCounter.delete(player.id);
            }
        }



        if (
            playerEquipment.getEquipment(Minecraft.EquipmentSlot.Head)?.typeId == "aria_ah:fox_helmet"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Chest)?.typeId == "aria_ah:fox_chestplate"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Legs)?.typeId == "aria_ah:fox_leggings"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Feet)?.typeId == "aria_ah:fox_boots"
        ) {
            if (player.isSneaking) {
                boneArmorCounter.set(player.id, (boneArmorCounter.get(player.id) || 0) + 1)

                if (boneArmorCounter.get(player.id) <= 50) {
                    const actionBarText = ["█", "█", "█", "█", "█"]
                    actionBarText.splice(Math.floor(boneArmorCounter.get(player.id) / 10), 0, "§8")
                    player.onScreenDisplay.setActionBar("§a" + actionBarText.join(""))
                    
                    if (boneArmorCounter.get(player.id) % 10 == 1) {
                        player.dimension.spawnParticle("aria_ah:fire_black", player.location)
                        player.playSound("aria_ah.armor_fire")
                    }
                } else if (10 - Math.floor((boneArmorCounter.get(player.id) - 50) / 20) <= 0) {
                    boneArmorCounter.delete(player.id)
                } else if (boneArmorCounter.get(player.id) > 50) {
                    player.onScreenDisplay.setActionBar({
                        rawtext: [{
                            translate: "aria_ah.cooldown.seconds",
                            with: [(10 - Math.floor((boneArmorCounter.get(player.id) - 50) / 20)).toString()]
                        }]
                    });
                    return
                }

                for (const entity of player.dimension.getEntities({ maxDistance: 10, location: player.location })) {
                    const families = entity.getComponent(Minecraft.EntityComponentTypes.TypeFamily)?.getTypeFamilies();
                    if (families && (families.includes("aria_ah_creature") || families.includes("mob"))) {
                        entity.setOnFire(1)

                        if (boneArmorCounter.get(player.id) == 50) {
                            entity.applyDamage(10)
                        }
                    }
                }
            } else if (boneArmorCounter.get(player.id) < 251) {
                boneArmorCounter.set(player.id, (boneArmorCounter.get(player.id) || 0) + 1)
            } else if (boneArmorCounter.get(player.id) >= 251) {
                boneArmorCounter.delete(player.id)
            }
        }
    }
})

Minecraft.world.afterEvents.playerSpawn.subscribe((event) => {
    if (!Minecraft.world.gameRules.doDayLightCycle) {
        event.player.sendMessage({
            rawtext: [{ translate: "aria_ah.day_night_cycle.disabled" }]
        })
    }

    for (const player of Minecraft.world.getPlayers()) {
        const playerInventory = player.getComponent("inventory").container
        
        for (let i = 0; i < playerInventory.size; i++) {
            const item = playerInventory.getItem(i)
            
            if (item?.typeId == "aria_ah:light_ray" && item?.lockMode == Minecraft.ItemLockMode.slot) {
                playerInventory.setItem(i, new Minecraft.ItemStack("aria_ah:light_ray_off"))
            }
        }
    }
})

Minecraft.world.afterEvents.itemUse.subscribe((event) => {
    const playerInventory = event.source.getComponent("inventory").container

    if (event.itemStack.typeId == "aria_ah:flashlight_off") {
        const item = new Minecraft.ItemStack("aria_ah:flashlight_on")
        const durability = event.itemStack.getComponent(Minecraft.ItemComponentTypes.Durability)
        if (durability.damage == durability.maxDurability) {
            let foundBattery = false

            for (let i = 0; i < playerInventory.size; i++) {
                const item = playerInventory.getItem(i)
                if (item?.typeId == "aria_ah:battery") {
                    foundBattery = true
                }
            }
            if (!foundBattery) {
                event.source.playSound("aria_ah.wrong")
                event.source.sendMessage({ rawtext: [{ translate: "aria_ah.requires_battery" }]})
                return
            } else {
                durability.damage = 0
                event.source.playSound("aria_ah.battery_reload")
                event.source.runCommand("clear @s aria_ah:battery 0 1")
            }
        }
        item.getComponent(Minecraft.ItemComponentTypes.Durability).damage = durability.damage
        playerInventory.setItem(event.source.selectedSlotIndex, item)
        event.source.playSound("aria_ah.light_onoff", { pitch: 1.0 })
    } else if (event.itemStack.typeId == "aria_ah:flashlight_on") {
        const item = new Minecraft.ItemStack("aria_ah:flashlight_off")
        item.getComponent(Minecraft.ItemComponentTypes.Durability).damage = event.itemStack.getComponent(Minecraft.ItemComponentTypes.Durability).damage
        playerInventory.setItem(event.source.selectedSlotIndex, item)
        event.source.playSound("aria_ah.light_onoff", { pitch: 0.9 })
    } else if (event.itemStack.typeId == "aria_ah:light_ray_off") {
        if (event.itemStack.getComponent("cooldown").getCooldownTicksRemaining(event.source) == 0) {
            if (event.source.runCommand("clear @s aria_ah:battery 0 1").successCount == 1) {
                playerInventory.setItem(event.source.selectedSlotIndex, new Minecraft.ItemStack("aria_ah:light_ray"))
                event.source.playSound("aria_ah.battery_reload")
            } else {
                event.source.playSound("aria_ah.wrong")
                event.source.sendMessage({ rawtext: [{ translate: "aria_ah.requires_battery" }]})
            }
        }
    } else if (event.itemStack.typeId == "aria_ah:light_ray") {
        const selectedSlot = event.source.selectedSlotIndex
        const lockedItem = playerInventory.getItem(selectedSlot)
        event.source.startItemCooldown("ender_pearl", 0)
        if (lockedItem.lockMode == Minecraft.ItemLockMode.slot) {
            return
        }
        lockedItem.lockMode = Minecraft.ItemLockMode.slot
        playerInventory.setItem(selectedSlot, lockedItem)

        Minecraft.system.runTimeout(() => {
            for (let i = 1; i < 30; i++) {
                const loc = event.source.getViewDirection()
                let x = event.source.location.x + loc.x * i
                let y = event.source.location.y + loc.y * i + 2
                let z = event.source.location.z + loc.z * i

                if (!event.source.runCommand(`testforblock ${x} ${y} ${z} minecraft:air`).successCount && !event.source.runCommand(`testforblock ${x} ${y} ${z} minecraft:light_block`).successCount) {
                    break
                }

                Minecraft.system.runTimeout(() => {
                    event.source.runCommand(`fill ${x} ${y} ${z} ${x} ${y} ${z} minecraft:light_block["block_light_level"=15] replace air`)
                    event.source.dimension.spawnParticle("aria_ah:white_explosion_small", { x, y, z })

                    const nearbyEntities = event.source.dimension.getEntities({ maxDistance: 5, location: { x, y, z }, families: ["monster"] })
                    for (const entity of nearbyEntities) {
                        entity.setOnFire(2)
                    }

                    const removeDate = Date.now() + 2000

                    Minecraft.world.setDynamicProperty(`aria_ah.light_ray.location.${event.source.id}.${i}.${removeDate}`, { x, y, z })
                    Minecraft.world.setDynamicProperty(`aria_ah.light_ray.dimension.${event.source.id}.${i}.${removeDate}`, event.source.dimension.id)

                    event.source.dimension.runCommand(`execute positioned ${x} ${y} ${z} as @e[type=aria_ah:dark_fog,r=5] run event entity @s aria:despawn`)
                    event.source.dimension.runCommand(`execute positioned ${x} ${y} ${z} as @e[family=aria_ah_creature,r=5] run event entity @s aria:set_transform_back_1`)
                }, i / 2)
            }

            Minecraft.system.runTimeout(() => {
                const inventory = event.source.getComponent("inventory").container
                if (inventory.getItem(selectedSlot)?.typeId == "aria_ah:light_ray") {
                    inventory.setItem(selectedSlot, new Minecraft.ItemStack("aria_ah:light_ray_off"))
                }
                event.source.startItemCooldown("ender_pearl", 200)
            }, 20)
        }, 65)
    }
})

Minecraft.system.runInterval(() => {
    for (const player of Minecraft.world.getPlayers()) {player.playAnimation("", {players:[] })
        const gameMode = player.getGameMode()
        const playerInventory = player.getComponent("inventory").container
        const playerEquipment = player.getComponent("equippable")
        let handItem = playerInventory.getItem(player.selectedSlotIndex)
        let mainhand = true
        if (handItem?.typeId != "aria_ah:flashlight_on") {
            handItem = playerEquipment.getEquipment(Minecraft.EquipmentSlot.Offhand)
            mainhand = false
        }

        if (handItem?.typeId == "aria_ah:flashlight_on" && !(gameMode == Minecraft.GameMode.Creative || gameMode == Minecraft.GameMode.Spectator)) {
            if (!handItem.getDynamicProperty("aria_ah.flashlight.id")) {
                handItem.setDynamicProperty("aria_ah.flashlight.id", Date.now().toString(36))
            }

            const durability = handItem.getComponent(Minecraft.ItemComponentTypes.Durability)
            const flashlightId = handItem.getDynamicProperty("aria_ah.flashlight.id")
            let flashlightDuri = (flashlightDurability.get(flashlightId) + 1) || durability.damage + 1
            flashlightDurability.set(flashlightId, flashlightDuri)
            if (flashlightDuri % 20 == 0) {
                durability.damage = flashlightDuri
                player.playSound("aria_ah.light_flicker")

                if (durability.damage == durability.maxDurability) {
                    player.playSound("random.break", { pitch: 0.9 })

                    let foundBattery = false

                    for (let i = 0; i < playerInventory.size; i++) {
                        const item = playerInventory.getItem(i)
                        if (item?.typeId == "aria_ah:battery") {
                            foundBattery = true
                        }
                    }
                    if (!foundBattery) {
                        const newItem = new Minecraft.ItemStack("aria_ah:flashlight_off")
                        newItem.getComponent(Minecraft.ItemComponentTypes.Durability).damage = durability.damage
                        player.sendMessage({
                            rawtext: [{ translate: "aria_ah.no_battery" }]
                        })
                        player.playSound("aria_ah.wrong")

                        handItem = newItem
                    } else {
                        player.playSound("aria_ah.battery_reload")
                        flashlightDurability.delete(flashlightId)
                    }
                }
            }
            if (mainhand) {
                player.getComponent("inventory").container.setItem(player.selectedSlotIndex, handItem)
            } else {
                playerEquipment.setEquipment(Minecraft.EquipmentSlot.Offhand, handItem)
            }
        }


        if (
            playerEquipment.getEquipment(Minecraft.EquipmentSlot.Head)?.typeId == "aria_ah:shadow_boss_helmet"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Chest)?.typeId == "aria_ah:shadow_boss_chestplate"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Legs)?.typeId == "aria_ah:shadow_boss_leggings"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Feet)?.typeId == "aria_ah:shadow_boss_boots"
        ) {
            if (!player.hasTag("aria_ah_armor_invisible")) {
                player.addTag("aria_ah_armor_invisible")
            }
        } else if (player.hasTag("aria_ah_armor_invisible")) {
            player.removeTag("aria_ah_armor_invisible")
        }

        if (
            playerEquipment.getEquipment(Minecraft.EquipmentSlot.Head)?.typeId == "aria_ah:fox_helmet"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Chest)?.typeId == "aria_ah:fox_chestplate"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Legs)?.typeId == "aria_ah:fox_leggings"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Feet)?.typeId == "aria_ah:fox_boots"
        ) {
            player.addEffect("speed", 230, { showParticles: false })
            player.addEffect("jump_boost", 230, { showParticles: false })
        }

        if (
            playerEquipment.getEquipment(Minecraft.EquipmentSlot.Head)?.typeId == "aria_ah:bone_helmet"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Chest)?.typeId == "aria_ah:bone_chestplate"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Legs)?.typeId == "aria_ah:bone_leggings"
            && playerEquipment.getEquipment(Minecraft.EquipmentSlot.Feet)?.typeId == "aria_ah:bone_boots"
        ) {
            player.addEffect("strength", 230, { showParticles: false })
        }
    }
}, 20)

Minecraft.system.runInterval(() => {
    let player0 = Minecraft.world.getPlayers().find((player) => player.id == hostId) || Minecraft.world.getPlayers()[0]
    if (!player0) {
        return
    }
    hostId = player0.id
    const vvActive = Minecraft.world.scoreboard.getObjective("aria_ah.general").getScore("vv_active") || 0
    const vvForce = Minecraft.world.scoreboard.getObjective("aria_ah.general").getScore("vv_force") || 0

    if (vvForce == 1 && player0) {
        if (player0.graphicsMode == Minecraft.GraphicsMode.Deferred && vvActive == 0) {
            Minecraft.world.scoreboard.getObjective("aria_ah.general").setScore("vv_active", 1)
            Minecraft.world.sendMessage({
                rawtext: [{ translate: "aria_ah.vv_detected" }]
            })
        } else if (player0.graphicsMode != Minecraft.GraphicsMode.Deferred && vvActive == 1) {
            Minecraft.world.scoreboard.getObjective("aria_ah.general").setScore("vv_active", 0)
            Minecraft.world.sendMessage({
                rawtext: [{ translate: "aria_ah.vv_not_detected" }]
            })
        }
    }
}, 100)

Minecraft.system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent("aria_ah:item_dont_break", {
        onBeforeDurabilityDamage: (damageEvent) => {
            Minecraft.system.run(() => {
                const player = Minecraft.world.getPlayers().find((player) => player.id == damageEvent.attackingEntity.id)
                const item = new Minecraft.ItemStack(damageEvent.itemStack.typeId)
                const durability = item.getComponent(Minecraft.ItemComponentTypes.Durability)
                if (durability.damage < durability.maxDurability) {
                    return
                }
                player.runCommand(`stopsound @s random.break`)
                durability.damage = durability.maxDurability
                damageEvent.attackingEntity.getComponent("inventory").container.setItem(player.selectedSlotIndex, item)
            })
        }
    })
})

Minecraft.world.afterEvents.entityDie.subscribe((event) => {
    if (event.deadEntity.typeId == "minecraft:player") {
        for (const itemEntity of event.deadEntity.dimension.getEntities({ type: "minecraft:item" })) {
            const item = itemEntity.getComponent("item").itemStack

            if (item.typeId == "aria_ah:light_ray" && item.lockMode == Minecraft.ItemLockMode.slot) {
                const newItem = new Minecraft.ItemStack("aria_ah:light_ray_off")
                itemEntity.dimension.spawnItem(newItem, itemEntity.location)
                itemEntity.remove()
            }
        }
    }

    if (event.damageSource.damagingEntity?.typeId == "minecraft:player") {
        const player = event.damageSource.damagingEntity
        const difficulty = Minecraft.world.scoreboard.getObjective("aria_ah.options").getScore("difficulty")

        if (event.deadEntity.hasTag("aria_ah_red_buff")) {
            if (difficulty == 1) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_easy").addScore("killed_red_buff", 1)
            } else if (difficulty == 2) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_normal").addScore("killed_red_buff", 1)
            } else if (difficulty == 3) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_hard").addScore("killed_red_buff", 1)
            }
        } else if (event.deadEntity.hasTag("aria_ah_invisible_buff")) {
            if (difficulty == 1) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_easy").addScore("killed_invisible_buff", 1)
            } else if (difficulty == 2) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_normal").addScore("killed_invisible_buff", 1)
            } else if (difficulty == 3) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_hard").addScore("killed_invisible_buff", 1)
            }
        }

        if (event.deadEntity.getComponent(Minecraft.EntityComponentTypes.TypeFamily)?.getTypeFamilies().includes("aria_ah_creature")) {
            if (difficulty == 1) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_easy").addScore("killed_creature", 1)
            } else if (difficulty == 2) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_normal").addScore("killed_creature", 1)
            } else if (difficulty == 3) {
                Minecraft.world.scoreboard.getObjective("aria_ah.stats_hard").addScore("killed_creature", 1)
            }
        }
    }
})
