#Loading and placing blocks

1 messages · Page 1 of 1 (latest)

empty wasp
#

I seem to have an issue with ticking areas.
I create one and try to set blocks in the area I set but it just doesn’t want to load the area of the ticking area.

Making the ticking area my self seems to fix the issue and everything then works fine but I need this automated


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

const CHUNK_SIZE = 16;
const MAX_TICKING_AREA_CHUNKS = 100;
const BLOCKS_PER_TICK = 2500;

function calculateTickingAreas(start, end) {
    const areas = [];
    const chunkStartX = Math.floor(start.x / CHUNK_SIZE);
    const chunkStartZ = Math.floor(start.z / CHUNK_SIZE);
    const chunkEndX = Math.floor(end.x / CHUNK_SIZE);
    const chunkEndZ = Math.floor(end.z / CHUNK_SIZE);
    const areaChunkSize = Math.floor(Math.sqrt(MAX_TICKING_AREA_CHUNKS));
    for (let chunkX = chunkStartX; chunkX <= chunkEndX; chunkX += areaChunkSize) {
        for (let chunkZ = chunkStartZ; chunkZ <= chunkEndZ; chunkZ += areaChunkSize) {
            const areaStart = {
                x: chunkX * CHUNK_SIZE,
                y: start.y,
                z: chunkZ * CHUNK_SIZE,
            };
            const areaEnd = {
                x: Math.min((chunkX + areaChunkSize - 1) * CHUNK_SIZE + (CHUNK_SIZE - 1), end.x),
                y: end.y,
                z: Math.min((chunkZ + areaChunkSize - 1) * CHUNK_SIZE + (CHUNK_SIZE - 1), end.z),
            };

            areas.push({ start: areaStart, end: areaEnd });
        }
    }
    return areas;
}

function* fillAreaBlocks(dimension, start, end, blockType) {
    const blocks = [];
    let blockCount = 0;
    const totalBlocks = (end.x - start.x + 1) * (end.y - start.y + 1) * (end.z - start.z + 1);
    for (let x = start.x; x <= end.x; x++) {
        for (let y = start.y; y <= end.y; y++) {
            for (let z = start.z; z <= end.z; z++) {
                blocks.push({ x, y, z });
                blockCount++;
                if (blocks.length >= BLOCKS_PER_TICK || blockCount === totalBlocks) {
                    for (const block of blocks) {
                        dimension.setBlockType(block, blockType);
                    }
                    blocks.length = 0;
                    yield;
                }
            }
        }
    }
}

function fillArea(start, end, blockType = "minecraft:stone") {
    const dimension = world.getDimension("overworld");
    const tickingAreas = calculateTickingAreas(start, end);
    try {
tickingAreas.forEach(({ start, end }, index) => {
console.warn("created ticking");
    const player = Array.from(world.getPlayers())[0];
    if (player) {
        player.runCommandAsync(
            `tickingarea add ${start.x} ${start.y} ${start.z} ${end.x} ${end.y} ${end.z} temp_${index} false`
        ).catch(error => console.error(`Failed to create ticking area: ${error}`));
    }
});
    } catch (error) {
        console.error("Failed to create ticking areas:", error);
        return;
    }
system.runJob(fillAreaBlocks(dimension, start, end, blockType));
    system.runTimeout(() => {
        try {
            tickingAreas.forEach((_, index) => {
                dimension.runCommandAsync(`tickingarea remove temp_${index}`);
            });
        } catch (error) {
            console.error("Failed to remove ticking areas:", error);
        }
    }, ((end.x - start.x + 1) * (end.y - start.y + 1) * (end.z - start.z + 1)) / BLOCKS_PER_TICK);
}


fillArea({ x: 2500, y: 64, z: 2500 }, { x: 2600, y: 94, z: 2600 });```

If anyone has a solution it would be appreciated
empty wasp
#

Made a decent solution. It’s not the best but it’s better than nothing


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

const CHUNK_SIZE = 16;
const TICKING_AREA_RADIUS = 4;
const MAX_TICKING_AREAS = 9;
const BLOCKS_PER_TICK = 2500;
let activeTickingAreas = [];
let tickingAreaInProgress = false;

function addTickingArea(x, y, z) {
    return new Promise((resolve) => {
        if (tickingAreaInProgress) return resolve(false);

        if (activeTickingAreas.length >= MAX_TICKING_AREAS) {
            const oldArea = activeTickingAreas.shift();
            const player = Array.from(world.getPlayers())[0];
            player.runCommandAsync(`tickingarea remove temp${oldArea.index}`)
                .catch(err => console.error(`Failed to remove ticking area: ${err}`));
        }

        const index = activeTickingAreas.length;
        const player = Array.from(world.getPlayers())[0];

        tickingAreaInProgress = true;
        player.runCommandAsync(`tickingarea add circle ${x} ${y} ${z} ${TICKING_AREA_RADIUS} temp${index} true`)
            .then(() => {
                activeTickingAreas.push({ x, y, z, index });
                tickingAreaInProgress = false;
                resolve(true);
            })
            .catch(err => {
                console.error(`Failed to create ticking area: ${err}`);
                tickingAreaInProgress = false;
                resolve(false);
            });
    });
}

function* fillAreaBlocks(dimension, start, end, blockType) {
    const blocks = [];
    let blockCount = 0;
    const totalBlocks = (end.x - start.x + 1) * (end.y - start.y + 1) * (end.z - start.z + 1);

    for (let x = start.x; x <= end.x; x++) {
        for (let y = start.y; y <= end.y; y++) {
            for (let z = start.z; z <= end.z; z++) {
                blocks.push({ x, y, z });
                blockCount++;

                if (blocks.length >= BLOCKS_PER_TICK || blockCount === totalBlocks) {
                    let blockPlaced = false;
                    while (!blockPlaced) {
                        try {
                            for (const block of blocks) {
                                dimension.setBlockType(block, blockType);
                            }
                            blockPlaced = true;
                        } catch (error) {
                            console.warn(`Chunk not loaded at (${x}, ${y}, ${z}), adding ticking area`);
                            const added = yield addTickingArea(x, y, z);
                            if (added) yield system.waitTicks(20);
                        }
                    }
                    blocks.length = 0;
                    yield;
                }
            }
        }
    }
}

function fillArea(start, end, blockType = "minecraft:stone") {
    const dimension = world.getDimension("overworld");
    system.runJob(fillAreaBlocks(dimension, start, end, blockType));
    system.runTimeout(() => {
        try {
            const player = Array.from(world.getPlayers())[0];
            activeTickingAreas.forEach(({ index }) => {
                player.runCommandAsync(`tickingarea remove temp${index}`)
                    .catch(err => console.error(`Failed to remove ticking area: ${err}`));
            });
            activeTickingAreas = [];
        } catch (error) {
            console.error("Failed to remove ticking areas:", error);
        }
    }, ((end.x - start.x + 1) * (end.y - start.y + 1) * (end.z - start.z + 1)) / BLOCKS_PER_TICK);
}

fillArea({ x: 3000, y: 64, z: 3000 }, { x: 3100, y: 94, z: 3100 });```