import { world, system, EffectTypes, EquipmentSlot, BlockTypes, ItemStack } from "@minecraft/server";
const hddg = ["kick:wendigo_antler_left",
        "kick:wendigo_antler_right"]
function randoh() {
        const raga = Math.floor(Math.random() * hddg.length)
        return hddg[raga]
}
function removeWendigoDoor(player) {
        const dim = player.dimension;
        const center = player.location;

        for (let dx = -15; dx <= 15; dx++) {
                for (let dy = 0; dy <= 14; dy++) {
                        for (let dz = -15; dz <= 15; dz++) {
                                const pos = {
                                        x: Math.floor(center.x + dx),
                                        y: Math.floor(center.y + dy),
                                        z: Math.floor(center.z + dz)
                                };

                                const block = dim.getBlock(pos);
                                if (!block) continue;
                                if (block.typeId === "kick:door_wendigo") {
                                        block.setType("minecraft:air");
                                }
                        }
                }
        }
}
system.runInterval(() => {
        for (const player of world.getAllPlayers) {
                if (player.hasTag("cursed")) {
                        player.runCommand("effect @s hunger infinite 20 true")
                }
        }
}, 20)
const baiaj = randoh()
class Utilities {
        static getClosestEntityFromViewDirection(entity, distance) {
                const eye = entity.getHeadLocation()
                const entityRaycastHit_list = entity.getEntitiesFromViewDirection({ maxDistance: distance, location: { x: eye.x, y: eye.y + 1, z: eye.z } });
                if (entityRaycastHit_list.length === 0)
                        return undefined;
                let entityClosest = undefined;
                let maxDistance = distance;
                entityRaycastHit_list.forEach((entityRaycastHit) => {
                        if (entityRaycastHit.distance < maxDistance) {
                                maxDistance = entityRaycastHit.distance;
                                entityClosest = entityRaycastHit.entity;
                        };
                });
                return entityClosest;
        };
};

system.runInterval(() => {
        world.getAllPlayers().forEach((player) => {
                const entities = Utilities.getClosestEntityFromViewDirection(player, 20);
                if (entities == undefined || entities.typeId == undefined) return;
                if (entities.typeId == "kick:wendigo_stalker") {
                        if (player.hasTag("wendigo_cooldown")) return
                        player.addTag("wendigo_cooldown")
                        system.runTimeout(() => {
                                player.removeTag("wendigo_cooldown")
                        }, 40)
                        player.addTag("randy")
                        if (Math.random() < 0.5) {
                                player.removeTag("randy")
                                player.addTag("randy1")
                        }
                        if (player.hasTag("randy")) {
                                for (const entity of world.getDimension("overworld").getEntities({ type: "kick:wendigo_stalker" })) {
                                        if (Math.random() < 0.25) {
                                                player.runCommand(`execute at @e[type=kick:wendigo_stalker] run setblock ~ ~ ~ ${baiaj} replace`)
                                        }
                                        entity.remove()
                                }
                        }
                        if (player.hasTag("randy1")) {
                                player.runCommand('event entity @e[type=kick:wendigo_stalker] kick:variant6')
                        }
                }
                if (entities.typeId === "kick:wendigo_player") {
                        for (const entity of world.getDimension("overworld").getEntities({ type: "kick:wendigo_player" })) {
                                player.runCommand('effect @s darkness 5 30 true')
                                entity.remove()
                        }
                }
                else {
                        player.removeTag("randy")
                }
        });
}, 5);
system.runInterval(() => {
        for (const player of world.getAllPlayers()) {
                for (const ent of world.getDimension("overworld").getEntities({ type: "kick:wendigo_player_real" })) {
                        const heG = ent.getComponent("minecraft:health").currentValue
                        if (heG <= 7) {
                                ent.triggerEvent("kick:death")
                        }
                }
        }
}, 10)

const chestplate_body = "kick:wendigo_chestplate";

system.runInterval(() => {
        for (const player of world.getAllPlayers()) {
                const equip = player.getComponent("minecraft:equippable");
                const chestItem = equip.getEquipment(EquipmentSlot.Chest);

                const wearing = chestItem && chestItem.typeId === chestplate_body;

                if (wearing) {
                        player.addTag("wearing_custom_chestplate");
                        player.addEffect(EffectTypes.get("strength"), 25, { amplifier: 1, showParticles: false });
                        player.addEffect(EffectTypes.get("hunger"), 25, { amplifier: 2, showParticles: false });
                        player.runCommand('title @s title jumpscare1')
                } else {
                        player.removeTag("wearing_custom_chestplate");
                }
        }
}, 20);
world.beforeEvents.playerPlaceBlock.subscribe((navi) => {
        const plags = navi.player
        if (navi.block.typeId == "minecraft:cake") {
                navi.cancel = true
        }
})
world.afterEvents.playerInteractWithEntity.subscribe((nagu) => {
        const entitu = nagu.target
        const play = nagu.player
        const ite = new ItemStack("kick:wendigo_heart")
        if (entitu.typeId === "kick:wendigo_player_real") {
                play.getComponent("minecraft:inventory").container.addItem(ite)
                play.runCommand('playsound random.levelup @s')
        }
})
const REQUIRED_ITEM = "kick:key_wendigo";

world.beforeEvents.playerInteractWithBlock.subscribe(ev => {
        const { itemStack, player, block } = ev;

        if (!block) return;
        if (block.typeId !== "kick:door_wendigo") return;

        if (itemStack.typeId !== REQUIRED_ITEM) {
                system.run(() => {
                        player.sendMessage("The door seems to be locked...");
                        return;
                })
        }

        if (itemStack.typeId === REQUIRED_ITEM)
                system.run(() => {
                        removeWendigoDoor(player)
                        player.onScreenDisplay.setActionBar("The door disappeared...")
                        itemStack.amount -= 1;
                })
});
system.afterEvents.scriptEventReceive.subscribe((tr) => {
        const my = tr.message
        const player = tr.initiator
        if (my === "sai:tick") {
                player.removeTag("no_cooked_meat")
        }
})
