import { system, world } from "@minecraft/server";
import { modifyStamina } from "../stamina";
let playerMap = new Map();

system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        let viewDirection = player.getViewDirection();
        let pos = player.location;
        let isSneaking = player.isSneaking;
        let isJumping = player.getVelocity().y > 0.1;

        let blockFront = player.dimension.getBlock({
            x: Math.floor(pos.x + viewDirection.x),
            y: Math.floor(pos.y),
            z: Math.floor(pos.z + viewDirection.z)
        });

        if (blockFront && !blockFront.isAir && isJumping && isSneaking) {

            if (!player.hasTag("isExahust") && !player.hasTag("useSkill")) {
                player.addTag("useSkill");

                if (player.hasTag("isExahust")) {
                    player.playSound("msft:ambient:no-mana");
                    player.removeTag("useSkill");
                    continue;
                }
                if (!playerMap.has(player.id)) {
                    player.playAnimation("animation.wall_climb");
                    playerMap.set(player.id);
                }
                player.applyKnockback(0, 0, 0, 0.2);
                modifyStamina(player, 5, "remove");
                player.removeTag("useSkill");
            }
        }
    };
}, 0.5);

system.runInterval(() => {
    world.getAllPlayers().forEach(player => {
        if (playerMap.has(player.id)) {
            //player.playAnimation("animation.wall_climb");
            playerMap.delete(player.id);
        }
    })
}, 20);