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



function chitin_effect_s(player) {
    try {
            if (player.hasTag("chitin_1") && player.hasTag("chitin_2") && player.hasTag("chitin_3") && player.hasTag("chitin_4") && !player.hasTag("chitin_effect")) {
                    player.runCommandAsync("title @s actionbar §l§5Anti-pest: Spider slow/weaken (14 range)");
                    player.runCommandAsync("playsound minecraft:entity.player.levelup player @s");

                system.runTimeout(() => {
                    player.runCommandAsync("tag @s add chitin_effect");
                }, 50);
            }
    } catch (error) {
        console.error("Error en chitin_effect_s(player):", error);
    }
}

function oculto_ghillie(player) {
    try {
            if (player.hasTag("camuflaje_armor_efecto") && !player.hasTag("no_oculto")) {
                    player.runCommandAsync("title @s actionbar §l§5Perfect Camouflage: Dino detect -75%, temp invisibility");
                    player.runCommandAsync("playsound minecraft:entity.player.levelup player @s");

                system.runTimeout(() => {
                    player.runCommandAsync("tag @s add no_oculto");
                }, 50);
            } 
    } catch (error) {
        console.error("Error en oculto_ghillie(player):", error);
    } 
}

function camuflaje_armor_efecto(player) {
    try {
            if (player.hasTag("camuflaje_armor_efecto") && !player.hasTag("camuflaje_armor_efecto2")) {
                    player.runCommandAsync("title @s actionbar §l§5Enhanced Camouflage - Dinosaur vision -50%");
                    player.runCommandAsync("playsound minecraft:entity.player.levelup player @s");

                system.runTimeout(() => {
                    player.runCommandAsync("tag @s add camuflaje_armor_efecto2");
                }, 50);
            }
    } catch (error) {
        console.error("Error en camuflaje_armor_efecto(player):", error);
    }
}
function gas_mask_effect(player) {
    try {
            if (player.hasTag("gas_mask_effect") && !player.hasTag("gas_mask_effect2")) {
                    player.runCommandAsync("title @s actionbar §l§5Passive Anti-Toxin Buff: Minor poison defense");
                    player.runCommandAsync("playsound minecraft:entity.player.levelup player @s");

                system.runTimeout(() => {
                    player.runCommandAsync("tag @s add gas_mask_effect2");
                }, 50);
            }
    } catch (error) {
        console.error("Error en gas_mask_effect(player):", error);
    }
}

world.afterEvents.itemUse.subscribe((event) => { 
    try {
        const player = event.source;
    camuflaje_armor_efecto(player);
    chitin_effect_s(player);
    gas_mask_effect(player);
    } catch (error) {
        console.error("Error en itemUse event (Armor_effect):", error);
    }
});

world.afterEvents.playerSpawn.subscribe((event) => {
    try {
        const player = event.player;
    camuflaje_armor_efecto(player);
    chitin_effect_s(player);
    gas_mask_effect(player);
    } catch (error) {
        console.error("Error en playerSpawn event (Armor_effect):", error);
    }
});

system.runInterval(() => {
    try {
        const players = world.getPlayers();
        for (const player of players) {
    camuflaje_armor_efecto(player);
    chitin_effect_s(player);
    gas_mask_effect(player);
        }
    } catch (error) {
        console.error("Error en runInterval (Armor_effect):", error);
    }
}, 20);
 