diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index baea326..902a80d 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -117,6 +117,7 @@ Commands can only be registered while the server phase runs. | `/time add ` | Moves time forward | | `/locatebiome ` | Says where the nearest biome of that kind is, up to 6400 blocks away | | `/tp ` | Teleports you there | +| `/fill ` | Fills the box between two corners with a block, `air` to clear it | | `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster | | `/tps` | Shows how the game loop keeps up | | `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts | diff --git a/server/game/game_server.ts b/server/game/game_server.ts index a35f0aa..03d0104 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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, + next: CommandFunction | CommandNode, ): CommandNode => { const [name, ...rest] = names; const node = argument(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("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("fill") + .describe("Fills the blocks between two corners with a block") + .then(coordinates( + ["x1", "y1", "z1", "x2", "y2", "z2"], + argument("block", block_or_air()).executes((context) => { + const [from, to] = corners(context); + const id = context.get("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("locatebiome").describe("Finds the nearest biome of a kind").then( argument("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) { const player = context.source.server_player; - const [from, to] = ["1", "2"].map((corner) => ({ - x: context.get(`x${corner}`), - y: context.get(`y${corner}`), - z: context.get(`z${corner}`), - })); + const [from, to] = corners(context); let writing: Promise; 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 { + 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): [Position, Position] { + const corner = (n: string) => ({ + x: context.get(`x${n}`), + y: context.get(`y${n}`), + z: context.get(`z${n}`), + }); + return [corner("1"), corner("2")]; +} + function is_number(value: unknown): value is number { return typeof value === "number" && Number.isFinite(value); } diff --git a/tests/commands_test.ts b/tests/commands_test.ts index 85bcacf..682eaae 100644 --- a/tests/commands_test.ts +++ b/tests/commands_test.ts @@ -209,3 +209,35 @@ Deno.test("/locatebiome says where the nearest biome of a kind is", async () => assertEquals(game.world.locate_biome("bworld:plains", x, z), y); assert(say("/locatebiome bworld:nowhere").includes("There's no biome bworld:nowhere")); }); + +Deno.test("/fill replaces every block between two corners, both included", async () => { + const { game, join, take, send } = await test_game("mods"); + join(1, "alice"); + take(1); + const say = (text: string) => { + send(1, { type: "chat", text }); + return take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n"); + }; + const ids = (x0: number, y0: number, z0: number, x1: number, y1: number, z1: number) => { + const found = new Set(); + for (let x = x0; x <= x1; x++) { + for (let y = y0; y <= y1; y++) { + for (let z = z0; z <= z1; z++) found.add(game.world.get_block_id(x, y, z)); + } + } + return [...found]; + }; + + // corners in any order + assertEquals(say("/fill 12 101 21 10 100 20 stone"), "Filled 12 blocks with Stone"); + assertEquals(ids(10, 100, 20, 12, 101, 21), ["bworld:stone"]); + assert(!ids(9, 100, 20, 9, 101, 21).includes("bworld:stone")); + assert(!ids(10, 102, 20, 12, 102, 21).includes("bworld:stone")); + + assertEquals(say("/fill 10 100 20 12 101 21 cubi:air"), "Filled 12 blocks with Air"); + assertEquals(ids(10, 100, 20, 12, 101, 21), ["cubi:air"]); + + assert(say("/fill 0 0 0 100 100 100 stone").startsWith("Couldn't fill it")); + assertEquals(game.world.get_block_id(50, 120, 50), "cubi:air"); + assertEquals(say("/fill 0 0 0 1 1 1 bworld:nothing"), "There's no block bworld:nothing"); +});