import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
const itemDropPool = [
    { item: 'minecraft:stone_axe', stack: () => 1 },
    { item: 'minecraft:stone_sword', stack: () => 1 },
    { item: 'minecraft:stone_pickaxe', stack: () => 1 },
    { item: 'minecraft:stone_shovel', stack: () => 1 },
    { item: 'minecraft:stone_hoe', stack: () => 1 },
    { item: 'minecraft:torch', stack: () => utils.getRandomInteger(1, 64) },
    { item: 'minecraft:leather_helmet', stack: () => 1 },
    { item: 'minecraft:leather_chestplate', stack: () => 1 },
    { item: 'minecraft:leather_leggings', stack: () => 1 },
    { item: 'minecraft:leather_boots', stack: () => 1 },
];
const down = { x: 0, y: -1, z: 0 };
export function crafting_tableAbility(player) {
    utils.mclog(`crafting_table`);
    mc.world.playSound('ability.crafting_table.activate', player.location);
    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));
    }
    utils.runCommand(`particle ldz:ability_crafting_table ${target.toCommandString()}`);
    utils.waitForEvent(15, () => {
        const itemData = itemDropPool[utils.getRandomInteger(0, itemDropPool.length - 1)];
        const stack = player.dimension.spawnItem(new mc.ItemStack(itemData.item, itemData.stack()), target);
        const impulse = new utils.Vector3({ x: 0, y: 0, z: 1 }).rotateByAngle(utils.getRandomInteger(-180, 180)).normalize().multiply(0.1);
        mc.world.playSound('random.pop', target);
        utils.runCommand(`particle ldz:spawn_effect_small ${target.toCommandString()}`);
        stack.applyImpulse(impulse);
    });
}
