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

world.beforeEvents.playerBreakBlock.subscribe(e => {
    var block = e.block;
    var veinminer = e.itemStack?.typeId.includes("picareta_ruby") && (block.hasTag("iron_pick_diggable"));

    if (veinminer) {
        let dimension = block.dimension;
        let toBreak = [block.location];
        let checked = new Set();

        while (toBreak.length > 0) {
            let location = toBreak.shift();
            let key = `${location.x},${location.y},${location.z}`;
            if (checked.has(key)) continue;
            checked.add(key);

            let currentBlock = dimension.getBlock(location);
            if (currentBlock && currentBlock.hasTag("iron_pick_diggable")) {
                system.run(() => {
                    dimension.runCommand(`setblock ${location.x} ${location.y} ${location.z} air destroy`);
                });
                let adjacent = [
                    {x: location.x + 1, y: location.y, z: location.z},
                    {x: location.x - 1, y: location.y, z: location.z},
                    {x: location.x, y: location.y + 1, z: location.z},
                    {x: location.x, y: location.y - 1, z: location.z},
                    {x: location.x, y: location.y, z: location.z + 1},
                    {x: location.x, y: location.y, z: location.z - 1}
                ];
                for (let loc of adjacent) {
                    toBreak.push(loc);
                }
            }
        }
        e.cancel = true;
    }
});