Fill command

This commit is contained in:
2026-11-15 20:56:40 -03:00
parent b4adf85f03
commit 17f39bfa4d
3 changed files with 110 additions and 9 deletions
+77 -9
View File
@@ -53,11 +53,13 @@ import { encode_schematic } from "@/common/schematic.ts";
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
import {
argument,
type ArgumentType,
block,
choice,
type CommandContext,
CommandError,
type CommandFunction,
type CommandNode,
CommandNode,
fluid,
integer,
item,
@@ -102,6 +104,8 @@ const PICKUP_REACH_Y = 0.5;
// tiles further than this many chunks from every player don't tick
const SIMULATION_DISTANCE = 6;
const MAX_GIVE = 64 * 36;
// the most blocks one /fill changes, so it can't hold up the game for long
const MAX_FILL_VOLUME = 1 << 15;
// /locatebiome looks at columns this far apart, out to this far, like minecraft's
const LOCATE_STEP = 32;
const LOCATE_RADIUS = 6400;
@@ -921,6 +925,31 @@ export class GameServer {
}
}
// /fill: every block between two corners, both included, replaced with id. like a schematic, they all change before
// any of them hears of it. throws for a box that's too big, and says how many blocks it changed
fill(from: Position, to: Position, id: string): number {
const [x0, x1] = [Math.min(from.x, to.x), Math.max(from.x, to.x)];
const [y0, y1] = [Math.min(from.y, to.y), Math.max(from.y, to.y)];
const [z0, z1] = [Math.min(from.z, to.z), Math.max(from.z, to.z)];
const volume = (x1 - x0 + 1) * (y1 - y0 + 1) * (z1 - z0 + 1);
if (volume > MAX_FILL_VOLUME) {
throw new Error(`fill changes up to ${MAX_FILL_VOLUME} blocks, and that box has ${volume}`);
}
const placed: Position[] = [];
for (let y = y0; y <= y1; y++) {
if (!this.world.shape.contains(y)) continue;
for (let z = z0; z <= z1; z++) {
for (let x = x0; x <= x1; x++) placed.push({ x, y, z });
}
}
this.mods.rules.change(() => {
for (const { x, y, z } of placed) this.#replace_block(x, y, z, id);
for (const { x, y, z } of placed) this.#create_block(x, y, z, id);
});
for (const { x, y, z } of placed) this.mods.rules.notify_neighbors(x, y, z);
return placed.length;
}
// schematics
// the box between two corners as a schematic in a file the host writes, see ctx.schematics.export. throws right
@@ -1827,14 +1856,15 @@ export class GameServer {
);
// TODO: only let operators do this, it writes files on the server's disk
// whole number arguments one after another, the last one running it
// whole number arguments one after another, the last one running it or going on to next
const coordinates = (
names: string[],
run: CommandFunction<ServerCommandSource>,
next: CommandFunction<ServerCommandSource> | CommandNode<ServerCommandSource>,
): CommandNode<ServerCommandSource> => {
const [name, ...rest] = names;
const node = argument<number, ServerCommandSource>(name, integer());
return rest.length ? node.then(coordinates(rest, run)) : node.executes(run);
if (rest.length) return node.then(coordinates(rest, next));
return next instanceof CommandNode ? node.then(next) : node.executes(next);
};
register(
literal<ServerCommandSource>("exportschematic")
@@ -1842,6 +1872,26 @@ export class GameServer {
.then(coordinates(["x1", "y1", "z1", "x2", "y2", "z2"], (context) => this.#export_schematic(context))),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("fill")
.describe("Fills the blocks between two corners with a block")
.then(coordinates(
["x1", "y1", "z1", "x2", "y2", "z2"],
argument<string, ServerCommandSource>("block", block_or_air()).executes((context) => {
const [from, to] = corners(context);
const id = context.get<string>("block");
let count: number;
try {
count = this.fill(from, to, id);
} catch (e) {
throw new CommandError(`Couldn't fill it: ${(e as Error).message}`);
}
context.source.send_message(`Filled ${count} blocks with ${display_name(id)}`);
}),
)),
);
register(
literal<ServerCommandSource>("locatebiome").describe("Finds the nearest biome of a kind").then(
argument<string, ServerCommandSource>("biome", word())
@@ -1979,11 +2029,7 @@ export class GameServer {
// /exportschematic. the host writes the file, so the player hears back where it is a little later
#export_schematic(context: CommandContext<ServerCommandSource>) {
const player = context.source.server_player;
const [from, to] = ["1", "2"].map((corner) => ({
x: context.get<number>(`x${corner}`),
y: context.get<number>(`y${corner}`),
z: context.get<number>(`z${corner}`),
}));
const [from, to] = corners(context);
let writing: Promise<string>;
try {
writing = this.export_schematic(from, to);
@@ -2542,6 +2588,28 @@ export class GameServer {
}
}
// a block id, or air, which isn't a registered block, for clearing with /fill
function block_or_air(): ArgumentType<string> {
const blocks = block();
return {
parse: (text) => text === "air" || text === AIR_ID ? AIR_ID : blocks.parse(text, undefined),
suggest(partial) {
const air = AIR_ID.startsWith(partial) || "air".startsWith(partial);
return [...(air ? [{ text: AIR_ID }] : []), ...blocks.suggest!(partial, undefined)];
},
};
}
// the two corners from x1 y1 z1 x2 y2 z2 arguments, for /fill and /exportschematic
function corners(context: CommandContext<ServerCommandSource>): [Position, Position] {
const corner = (n: string) => ({
x: context.get<number>(`x${n}`),
y: context.get<number>(`y${n}`),
z: context.get<number>(`z${n}`),
});
return [corner("1"), corner("2")];
}
function is_number(value: unknown): value is number {
return typeof value === "number" && Number.isFinite(value);
}