Export schematics

This commit is contained in:
2026-11-15 18:41:22 -03:00
parent 2fa109026c
commit 59bfed58bf
18 changed files with 511 additions and 26 deletions
+51 -1
View File
@@ -47,13 +47,16 @@ import { collision_shape_at, type Shape } from "@/common/block_shapes.ts";
import { physics } from "@/common/physics.ts";
import { EXHAUSTION } from "./vitals.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import type { LootContext, UseTarget } from "@/common/mod_api/server.ts";
import type { LootContext, Position, UseTarget } from "@/common/mod_api/server.ts";
import type { EntityJson } from "@/common/mod_data.ts";
import { encode_schematic } from "@/common/schematic.ts";
import { capture_schematic } from "./schematics.ts";
import {
argument,
choice,
type CommandContext,
CommandError,
type CommandFunction,
type CommandNode,
fluid,
integer,
@@ -132,6 +135,8 @@ export interface GameHost {
close(conn: number): void;
// fetches a url, failing with a message for players when it can't or it's more than max_bytes
download(url: string, max_bytes: number): Promise<Uint8Array>;
// keeps an exported schematic in a new file, for whoever runs the server, resolving to where it is
write_schematic(bytes: Uint8Array<ArrayBuffer>): Promise<string>;
}
// level.json, everything saved but the blocks, which are in region files (see saves.ts)
@@ -911,6 +916,14 @@ export class GameServer {
}
}
// schematics
// the box between two corners as a schematic in a file the host writes, see ctx.schematics.export. throws right
// away for a box that's too big
export_schematic(from: Position, to: Position): Promise<string> {
return this.#host.write_schematic(encode_schematic(capture_schematic(this, from, to)));
}
// menus
// the id a new menu for this player gets, 1 to 100 and around, like minecraft's containerCounter
@@ -1772,6 +1785,22 @@ 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
const coordinates = (
names: string[],
run: CommandFunction<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);
};
register(
literal<ServerCommandSource>("exportschematic")
.describe("Saves the blocks between two corners as a schematic file on the server")
.then(coordinates(["x1", "y1", "z1", "x2", "y2", "z2"], (context) => this.#export_schematic(context))),
);
register(
literal<ServerCommandSource>("locatebiome").describe("Finds the nearest biome of a kind").then(
argument<string, ServerCommandSource>("biome", word())
@@ -1906,6 +1935,27 @@ export class GameServer {
this.#broadcast({ type: "player_skin", id: player.id, skin, slim: slim || undefined });
}
// /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}`),
}));
let writing: Promise<string>;
try {
writing = this.export_schematic(from, to);
} catch (e) {
throw new CommandError(`Couldn't export it: ${(e as Error).message}`);
}
const online = () => this.#players.get(player.conn) === player;
writing.then(
(path) => online() && this.send_chat(player, `Saved the schematic to ${path}`),
(e) => online() && this.send_chat(player, `Couldn't save the schematic: ${(e as Error).message}`),
);
}
// the host downloads it, which takes a while, so the player hears back later
#download_skin(player: ServerPlayer, url: string, slim: boolean) {
if (player.downloading_skin) {