import { system, world } from '@minecraft/server';
system.runInterval(() => {
    
    const players = world.getAllPlayers();

    for (const player of players) {
        
        if (player.isSneaking && !player.hasTag('agachado')) {
            defesa(player);
        }
        
        if (player.isSneaking && player.hasTag('anmd')) {
            anmd(player);
        }
        
        else if (!player.isSneaking && player.hasTag('agachado') && player.hasTag('anmd')) {
            removerDefesa(player);
        }
    }
}, 1);

function defesa(player) {
    player.runCommandAsync("tag @s add anmd");
    player.runCommandAsync("particle nb:defesa ^ ^0.4 ^0.6");
    player.runCommandAsync("effect @s slowness 5 3 true");
    player.runCommandAsync("effect @s resistance 5 10 true");
    player.addTag('agachado');
}

function removerDefesa(player) {
    player.removeTag('agachado');
    player.removeTag('anmd');
    player.runCommandAsync("effect @s clear");
}

function anmd(player) {
    player.runCommandAsync("playanimation @s animation.defesa");
}