import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { HeartAbilityManager } from "../systems/index";
class TreeStructure {
    constructor() {
        this.height = 7;
        this.name = 'tree';
        this.xOffset = 2;
        this.zOffset = 2;
    }
}
const treeStuctures = [
    {
        height: 8,
        name: "tree_acacia",
        xOffset: 2,
        zOffset: 2,
    },
    {
        height: 8,
        name: "tree_birch",
        xOffset: 2,
        zOffset: 2,
    },
    {
        height: 11,
        name: "tree_cherry",
        xOffset: 2,
        zOffset: 2,
    },
    {
        height: 11,
        name: "tree_jungle",
        xOffset: 2,
        zOffset: 2,
    },
    {
        height: 7,
        name: "tree_oak",
        xOffset: 2,
        zOffset: 2,
    },
    {
        height: 9,
        name: "tree_spruce",
        xOffset: 2,
        zOffset: 2,
    },
];
const down = { x: 0, y: -1, z: 0 };
export async function saplingAbility(player) {
    utils.mclog(`sapling`);
    HeartAbilityManager.addInvulnerability(player, 'noFallDamage');
    const direction = new utils.Vector3({ x: player.getViewDirection().x, y: 0, z: player.getViewDirection().z });
    const origin = new utils.Vector3(player.getHeadLocation()).add(direction.multiply(3));
    const block = player.dimension.getBlockFromRay(origin, down, { includeLiquidBlocks: false, includePassableBlocks: false, maxDistance: 5 });
    let target = new utils.Vector3(player.location).add(direction.multiply(3));
    if (block) {
        target = new utils.Vector3(block.block.location).add(new utils.Vector3(block.faceLocation));
    }
    const tree = treeStuctures[utils.getRandomInteger(0, treeStuctures.length - 1)];
    if (player.hasTag('InSpawnHouse') || player.location.y + tree.height >= 319) {
        utils.runCommand(`function abilities/sapling/activate`, player);
    }
    else {
        utils.runCommand(`execute unless entity @s[tag=InSpawnHouse] run structure load ${tree.name} ~${-tree.xOffset} ~-1 ~${-tree.zOffset} 0_degrees none layer_by_layer 0.5`, player);
        utils.waitForEvent(0, () => {
            player.applyKnockback(0, 0, 0, 0.5 + tree.height * 0.1);
        });
    }
    mc.world.playSound('ability.sapling.grow', player.location, { volume: 1 });
    await utils.waitForTicks(1);
    await utils.waitForLoopEvent(0, () => {
        return player.hasTag('onGround');
    });
    HeartAbilityManager.removeInvulnerability(player, 'noFallDamage');
}
