ctx.schematics.get

This commit is contained in:
2026-11-17 22:14:32 -03:00
parent c0c728c066
commit 6c38166325
4 changed files with 37 additions and 1 deletions
+4
View File
@@ -589,6 +589,10 @@ export interface SchematicApi {
// adds a schematic for place, by an id in this mod's namespace, while the server phase runs. it's checked here:
// every block, state and entity in it has to be registered
register(id: Id, schematic: Schematic): void;
// a copy of a registered schematic, a file's or one a script registered, with its id, like neoforge's
// StructureTemplateManager.get. undefined for an id nothing registered. for reading what's in it before placing it,
// like where a room's doorways are
get(id: Id): Schematic | undefined;
// puts a registered schematic in the world, all of it at once, replacing what was there, air too. replaced blocks
// get their on_break. blocks it has data for get that data as it is, the rest their on_create, then everything
// placed tells the blocks beside it. its entities come with new uuids. throws for an id nothing registered, or a
+14 -1
View File
@@ -4,7 +4,7 @@ A schematic is a box of the world saved to a file: its blocks with their states
placed again somewhere else. Schematic files end in `.cbsm` and are [MessagePack](https://msgpack.org), see
`common/schematic.ts` for their format.
Exporting, registering and placing are built. Blocks making what their data points to when they're placed
Exporting, registering, reading and placing are built. Blocks making what their data points to when they're placed
(`import_data`) is planned in `STRUCTURES.md`.
## Exporting
@@ -72,6 +72,19 @@ file's.
It throws for an id nothing registered, or a placement that isn't one, before anything changes.
## Reading
`get` gives a copy of a registered schematic, a file's or one a script registered, with its `id`, or `undefined` for an
id nothing registered. It's for looking at what's in one before placing it, like where a room's doorways are when
fitting rooms together:
```ts
const room = ctx.schematics.get("copper_tools:room")!;
const doorways = room.blocks.filter((block) => room.palette[block.i].id === "copper_tools:doorway");
```
Positions in it are from its lowest corner, before it's turned. Changing the copy doesn't change what `place` puts.
Until `import_data` is built, a block's exported data goes back in as it is, so a chest placed from a schematic has its
items' list in its data and no container.
+4
View File
@@ -1069,6 +1069,10 @@ export class ModRuntime {
register.fail(`schematic ${id}: ${(e as Error).message}`);
}
},
get: (id) => {
const registered = game().schematics.get(id);
return registered && { ...structuredClone(registered.schematic), id };
},
place: (id, placement) => game().place_schematic(id, placement),
},
menus: {
+15
View File
@@ -245,6 +245,21 @@ Deno.test("a schematic is checked when it's registered, and a placement before a
assertEquals(t.game.world.get_block_id(0, 100, 0), "cubi:air");
});
Deno.test("get gives a copy of a registered schematic with its id, a file's or a script's", async () => {
const t = await place_setup();
const hut = t.schematics.get("testmod:hut")!;
assertEquals(hut.id, "testmod:hut");
assertEquals(hut.size, [2, 2, 1]);
assertEquals(hut.palette[hut.blocks.find((block) => block.p.join() === "0,1,0")!.i].id, "bworld:furnace");
assertEquals(t.schematics.get("testmod:post")?.id, "testmod:post");
assertEquals(t.schematics.get("testmod:nothing"), undefined);
// changing the copy doesn't change what's placed
hut.palette[0].id = "bworld:stone";
t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 } });
assertEquals(t.game.world.get_block_id(10, 100, 20), "bworld:oak_planks");
});
Deno.test("a mod's schematics/<name>.cbsm files are registered as <mod id>:<name> before its scripts run", async () => {
const t = await place_setup();
assertStringIncludes(String(t.game.mods.storage.testmod?.post), "registered twice");