Пошаговое изменение мира
Этот пример переносит построение пирамиды на несколько игровых тиков. Generator возвращает управление после каждого блока, а system.runJob продолжает его до завершения.
Создайте config/questscript/scripts/pyramid.js:
// @ts-check
world.sendMessage("Build pyramid script");
const overworld = world.getDimension("overworld");
function* buildPyramid() {
const center = [204, 64, -332];
const size = 7;
world.sendMessage(`Building pyramid at ${center} with size ${size}`);
const blocks = getPyramidBlockCoordinates(center, size);
for (const [x, y, z] of blocks) {
yield system.waitTicks(1);
overworld.setBlockType({ x, y, z }, "minecraft:gold_block");
}
world.sendMessage("Pyramid built");
}
/**
* @param {number[]} center
* @param {number} size
* @returns {[number, number, number][]}
*/
function getPyramidBlockCoordinates(center, size) {
/** @type {[number, number, number][]} */
const coordinates = [];
const [cx, cy, cz] = center;
let currentSize = size;
let layerY = 0;
while (currentSize > 0) {
const halfSize = Math.floor(currentSize / 2);
for (let x = -halfSize; x < halfSize + (currentSize % 2); x++) {
for (let z = -halfSize; z < halfSize + (currentSize % 2); z++) {
coordinates.push([cx + x, cy + layerY, cz + z]);
}
}
currentSize -= 2;
layerY += 1;
}
return coordinates;
}
system.runJob(buildPyramid());
Загрузите его командой /qs load pyramid.js. Координаты в вызове setBlockType используют публичную форму { x, y, z }; tuple остаётся только локальной структурой JavaScript.
Для больших объёмов всё равно учитывайте нагрузку на сервер: пошаговое выполнение распределяет работу по тикам, но не отменяет стоимость самих изменений мира.