import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
const bruteCount = 3;
const down = { x: 0, y: -1, z: 0 };
export async function bastionAbility(player) {
    utils.mclog(`bastion`);
    mc.world.playSound('mob.piglin.retreat', player.location, { pitch: 2.0 });
    if (player.hasTag('InSpawnHouse')) { }
    else {
        player.triggerEvent('add_bastion_ability_projectile');
    }
    utils.waitForEvent(2, () => {
        const projectile = player.dimension.getEntities({ type: 'ldz:bastion_ability_projectile' })[0];
        if (projectile && projectile.isValid()) {
            utils.waitForLoopEvent(0, () => {
                if (projectile.isValid() && projectile.getVelocity().x === 0 && projectile.getVelocity().y === 0 && projectile.getVelocity().z === 0) {
                    setGoldBlock(projectile);
                }
                return !projectile.isValid();
            });
        }
    });
    let bruteRemaining = bruteCount;
    await utils.waitForLoopEvent(10, () => {
        var _a;
        const spawnLocation = new utils.Vector3(player.location).add(new utils.Vector3({ x: utils.getRandomInteger(-10, 10), y: 0.5, z: utils.getRandomInteger(-10, 10) }));
        // Find Air Block
        for (let y = spawnLocation.y; y < spawnLocation.y + 5; y++) {
            if ((_a = player.dimension.getBlock({ x: spawnLocation.x, y, z: spawnLocation.z })) === null || _a === void 0 ? void 0 : _a.permutation.matches('minecraft:air')) {
                spawnLocation.y = y;
                break;
            }
        }
        utils.runCommand(`event entity @s add_bastion_ability_brute_spawner`, player);
        utils.waitForEvent(5, () => {
            mc.world.playSound('ability.bastion.ambient', spawnLocation, { pitch: 2.0 });
            mc.world.playSound('ability.bastion.brute_spawn', spawnLocation);
            utils.runCommand(`tp @e[family=bastion_ability_brute_spawner,c=1,tag=!dontTP] ${spawnLocation.toCommandString()}`);
            utils.runCommand(`particle ldz:bastion_ability_spawn ${spawnLocation.toCommandString()}`);
            utils.runCommand(`tag @e[family=bastion_ability_brute_spawner] add dontTP`, player);
        });
        bruteRemaining--;
        return bruteRemaining <= 0;
    });
}
function setGoldBlock(projectile) {
    const block = projectile.dimension.getBlockFromRay(projectile.location, new utils.Vector3(projectile.getViewDirection()).multiply(-1), { includeLiquidBlocks: false, includePassableBlocks: false, maxDistance: 5 });
    projectile.triggerEvent('despawn');
    if (block) {
        const location = new utils.Vector3(block.block.location);
        utils.runCommand(`setblock ${location.toCommandString()} minecraft:gold_block`);
        utils.runCommand(`particle ldz:bastion_ability_sparkle ${location.toCommandString()}`);
        mc.world.playSound('chime.amethyst_block', location);
    }
}
