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
+14
View File
@@ -49,6 +49,7 @@ export interface ServerContext {
world: ServerWorld;
players: PlayerList;
containers: ContainerApi;
schematics: SchematicApi;
items: ItemApi;
blocks: BlockApi;
// every fluid, named and colored by its rules, and fluid handlers, see docs/modding/resources/fluids.md
@@ -118,6 +119,11 @@ export interface BlockComponent<P = any> {
// particular when it's undefined, like neoforge's Capabilities.FluidHandler.BLOCK. undefined when it has none
// there. see ServerFluidApi.block_handler
fluid_handler?(block: BlockRef, params: P, face: Face | undefined): FluidHandler | undefined;
// its tile data the way it's kept outside the world, in a schematic, like neoforge's
// BlockEntity.saveWithoutMetadata: what lives elsewhere written in, like its container's items in place of the
// container's id. any json value. the first of its components that returns something other than undefined gives
// it, and a block without any keeps its data as it is
export_data?(block: BlockRef, params: P): unknown;
// placing and neighbor rules are in shared phases, see BlockRules in cubi/shared
}
@@ -565,6 +571,14 @@ export interface ContainerApi {
delete(id: string): void;
}
// see docs/modding/misc/schematics.md
export interface SchematicApi {
// the blocks between two corners, both of them in it, with their tile data (see BlockComponent.export_data) and
// the entities whose feet are in that box, written to a .cbsm file the host keeps in a temporary directory, like
// create's schematic and quill. resolves to where the file is. throws for a box of more than 1048576 blocks (2^20)
export(from: Position, to: Position): Promise<string>;
}
export interface ItemApi {
exists(id: Id): boolean;
// every item, in load order
+64
View File
@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// schematics: blocks and entities from a box of the world, kept to be placed again somewhere else. a .cbsm file is
// one encoded with msgpack. the short keys (i, s, p) are there to keep files small, see STRUCTURES.md
import { decode, encode } from "@std/msgpack";
export const SCHEMATIC_EXTENSION = ".cbsm";
export interface SchematicFile {
format_version: 1;
schematic: Schematic;
}
export interface Schematic {
// set once it's registered. an exported one doesn't have one yet
id?: string;
// blocks along x, y and z
size: [number, number, number];
palette: SchematicPaletteEntry[];
// every position in the box, air too, so placing it clears what was there
blocks: SchematicBlock[];
entities: SchematicEntity[];
}
export interface SchematicPaletteEntry {
id: string;
// its states by name, left out for a block without any
s?: Record<string, number>;
}
export interface SchematicBlock {
// its entry in the palette
i: number;
// from the box's lowest corner
p: [number, number, number];
// its tile data, the way its components' export_data give it
data?: unknown;
// the part of its tile data clients see, like the item a cutting board holds
client_data?: Record<string, unknown>;
}
export interface SchematicEntity {
// where its feet are, from the box's lowest corner
p: [number, number, number];
id: string;
// everything else the entity saves, without its uuid, so each one placed gets a new one
data: Record<string, unknown>;
}
export function encode_schematic(schematic: Schematic): Uint8Array<ArrayBuffer> {
const file: SchematicFile = { format_version: 1, schematic };
// msgpack can't encode undefined, which tile data and entities can have in them. as json they're left out
return encode(JSON.parse(JSON.stringify(file))) as Uint8Array<ArrayBuffer>;
}
// throws for anything that isn't a schematic file of a format this knows
export function decode_schematic(bytes: Uint8Array): Schematic {
const file = decode(bytes) as Partial<SchematicFile> | null;
if (file?.format_version !== 1 || typeof file.schematic !== "object" || file.schematic === null) {
throw new Error("isn't a schematic, or is one of a format this game doesn't know");
}
return file.schematic;
}