import { world, system } from "@minecraft/server";

const BLOCKS = [
    "glass", "glass_pane", "stained_glass_pane", "stained_glass", "waterlily", "pumpkin",
    "mangrove_leaves", "azalea_leaves", "azalea_leaves_flowered", "leaves", "bee_nest",
    "beehive", "melon", "flower_pot", "candle", "white_candle", "orange_candle",
    "magenta_candle", "light_blue_candle", "yellow_candle", "lime_candle", "pink_candle",
    "blue_candle", "red_candle", "green_candle", "brown_candle", "bamboo", "cactus",
    "cake", "cave_vines", "cave_vines_body_with_berries", "cave_vines_head_with_berries",
    "vine", "twisting_vines", "weeping_vines", "acacia_sapling", "bamboo_sapling",
    "birch_sapling", "cherry_sapling", "dark_oak_sapling", "jungle_sapling", "oak_sapling",
    "spruce_sapling", "dead_bush", "carved_pumpkin", "crimson_fungus", "warped_fungus",
    "red_mushroom", "brown_mushroom", "red_mushroom_block", "brown_mushroom_block",
    "chain", "lantern", "soul_lantern", "pitcher_plant", "short_grass", "tall_grass",
    "fern", "large_fern", "turtle_egg", "fence", "fence_gate", "acacia_fence",
    "birch_fence", "dark_oak_fence", "jungle_fence", "spruce_fence", "warped_fence",
    "crimson_fence", "acacia_door", "birch_door", "dark_oak_door", "jungle_door",
    "spruce_door", "warped_door", "crimson_door", "wooden_door", "scaffolding",
    "ladder", "cobblestone_wall", "lever", "torch", "soul_torch", "dried_kelp_block"
];

world.afterEvents.itemUse.subscribe((event) => {
    const player = event.source;
    const item = event.itemStack;

    if (item?.typeId === "minecraft:stick") {
        let currentIndex = 0;

        const placeNextBlock = () => {
            if (currentIndex >= BLOCKS.length) {
                player.onScreenDisplay.setActionBar("§a已完成所有块的放置!");
                return;
            }

            const block = BLOCKS[currentIndex];
            const x = Math.floor(player.location.x);
            const y = Math.floor(player.location.y) + 2;
            const z = Math.floor(player.location.z);

            try {
                player.runCommand(`setblock ${x} ${y} ${z} minecraft:${block}`);
            } catch (error) {
                console.warn(`检测到无效块: ${block}`);
                player.onScreenDisplay.setActionBar(`§c无效方块: ${block}`);
            }

            currentIndex++;
            system.runTimeout(placeNextBlock, 40); 
        };

        player.onScreenDisplay.setActionBar("§e加载中...");
        placeNextBlock();
    }
});
