Fill command
This commit is contained in:
@@ -117,6 +117,7 @@ Commands can only be registered while the server phase runs.
|
|||||||
| `/time add <ticks>` | Moves time forward |
|
| `/time add <ticks>` | Moves time forward |
|
||||||
| `/locatebiome <biome>` | Says where the nearest biome of that kind is, up to 6400 blocks away |
|
| `/locatebiome <biome>` | Says where the nearest biome of that kind is, up to 6400 blocks away |
|
||||||
| `/tp <x> <y> <z>` | Teleports you there |
|
| `/tp <x> <y> <z>` | Teleports you there |
|
||||||
|
| `/fill <x1> <y1> <z1> <x2> <y2> <z2> <block>` | 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 |
|
| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster |
|
||||||
| `/tps` | Shows how the game loop keeps up |
|
| `/tps` | Shows how the game loop keeps up |
|
||||||
| `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts |
|
| `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts |
|
||||||
|
|||||||
@@ -53,11 +53,13 @@ import { encode_schematic } from "@/common/schematic.ts";
|
|||||||
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
|
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
|
||||||
import {
|
import {
|
||||||
argument,
|
argument,
|
||||||
|
type ArgumentType,
|
||||||
|
block,
|
||||||
choice,
|
choice,
|
||||||
type CommandContext,
|
type CommandContext,
|
||||||
CommandError,
|
CommandError,
|
||||||
type CommandFunction,
|
type CommandFunction,
|
||||||
type CommandNode,
|
CommandNode,
|
||||||
fluid,
|
fluid,
|
||||||
integer,
|
integer,
|
||||||
item,
|
item,
|
||||||
@@ -102,6 +104,8 @@ const PICKUP_REACH_Y = 0.5;
|
|||||||
// tiles further than this many chunks from every player don't tick
|
// tiles further than this many chunks from every player don't tick
|
||||||
const SIMULATION_DISTANCE = 6;
|
const SIMULATION_DISTANCE = 6;
|
||||||
const MAX_GIVE = 64 * 36;
|
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
|
// /locatebiome looks at columns this far apart, out to this far, like minecraft's
|
||||||
const LOCATE_STEP = 32;
|
const LOCATE_STEP = 32;
|
||||||
const LOCATE_RADIUS = 6400;
|
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
|
// schematics
|
||||||
|
|
||||||
// the box between two corners as a schematic in a file the host writes, see ctx.schematics.export. throws right
|
// 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
|
// 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 = (
|
const coordinates = (
|
||||||
names: string[],
|
names: string[],
|
||||||
run: CommandFunction<ServerCommandSource>,
|
next: CommandFunction<ServerCommandSource> | CommandNode<ServerCommandSource>,
|
||||||
): CommandNode<ServerCommandSource> => {
|
): CommandNode<ServerCommandSource> => {
|
||||||
const [name, ...rest] = names;
|
const [name, ...rest] = names;
|
||||||
const node = argument<number, ServerCommandSource>(name, integer());
|
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(
|
register(
|
||||||
literal<ServerCommandSource>("exportschematic")
|
literal<ServerCommandSource>("exportschematic")
|
||||||
@@ -1842,6 +1872,26 @@ export class GameServer {
|
|||||||
.then(coordinates(["x1", "y1", "z1", "x2", "y2", "z2"], (context) => this.#export_schematic(context))),
|
.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(
|
register(
|
||||||
literal<ServerCommandSource>("locatebiome").describe("Finds the nearest biome of a kind").then(
|
literal<ServerCommandSource>("locatebiome").describe("Finds the nearest biome of a kind").then(
|
||||||
argument<string, ServerCommandSource>("biome", word())
|
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
|
// /exportschematic. the host writes the file, so the player hears back where it is a little later
|
||||||
#export_schematic(context: CommandContext<ServerCommandSource>) {
|
#export_schematic(context: CommandContext<ServerCommandSource>) {
|
||||||
const player = context.source.server_player;
|
const player = context.source.server_player;
|
||||||
const [from, to] = ["1", "2"].map((corner) => ({
|
const [from, to] = corners(context);
|
||||||
x: context.get<number>(`x${corner}`),
|
|
||||||
y: context.get<number>(`y${corner}`),
|
|
||||||
z: context.get<number>(`z${corner}`),
|
|
||||||
}));
|
|
||||||
let writing: Promise<string>;
|
let writing: Promise<string>;
|
||||||
try {
|
try {
|
||||||
writing = this.export_schematic(from, to);
|
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 {
|
function is_number(value: unknown): value is number {
|
||||||
return typeof value === "number" && Number.isFinite(value);
|
return typeof value === "number" && Number.isFinite(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
assertEquals(game.world.locate_biome("bworld:plains", x, z), y);
|
||||||
assert(say("/locatebiome bworld:nowhere").includes("There's no biome bworld:nowhere"));
|
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<string>();
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user