import { system, world } from "@minecraft/server";
class AntiJuke {
    constructor(families = ["ss_hyd_hydra"]) {
        this.families = families;
    }
    run(player) {
        const entity = this.getEntity(player);
        if (!entity)
            return;
        if (entity.hasTag('ss:juked'))
            return;
        this.playAnimation(entity);
        this.applyKnockback(player);
        system.runTimeout(() => {
            try {
                entity.removeTag('ss:juked');
            }
            catch (error) {
                player.runCommand('/tag @e[family=hydra,r=30] remove ss:juked');
            }
        }, 40);
        entity.addTag('ss:juked');
    }
    getEntity(player) {
        const zone = world.getDimension(player.dimension.id);
        const options = {
            "closest": 1,
            "families": this.families,
            "maxDistance": 4,
            "minDistance": 0,
            "location": player.location
        };
        const entity = zone.getEntities(options)[0];
        return entity;
    }
    playAnimation(entity) {
        entity.playAnimation('animation.ss_hyd.hydra.attack_stomp');
    }
    applyKnockback(player) {
        system.runTimeout(() => {
            const facingDirection = player.getViewDirection();
            const magnitude = Math.sqrt(facingDirection.x * facingDirection.x + facingDirection.z * facingDirection.z);
            const normalizedX = facingDirection.x / magnitude;
            const normalizedZ = facingDirection.z / magnitude;
            player.applyKnockback(-normalizedX, -normalizedZ, 3, 1);
        }, 30);
    }
}
export { AntiJuke };
