5.4 KiB
Schematics
A schematic is a box of the world saved to a file: its blocks with their states and data, and the entities in it, to be
placed again somewhere else. Schematic files end in .cbsm and are MessagePack, see
common/schematic.ts for their format.
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
/exportschematic x1 y1 z1 x2 y2 z2 saves the blocks between two corners, both of them included, in any order. The
server writes the file to its system's temporary directory and tells the player where it is, so it's for whoever runs
the server to pick up.
Scripts do the same in the server phase:
const path = await ctx.schematics.export({ x: 0, y: 64, z: 0 }, { x: 15, y: 80, z: 15 });
ctx.log(`saved to ${path}`);
A schematic holds up to 1048576 blocks (2^20), a box of 128 by 64 by 128. A bigger box throws right away.
What goes in:
- Every block in the box, air too, so placing it clears what was there. Blocks with the same states share one entry in the palette.
- Each block's tile data, see Block Data, and its client data, like the item a cutting board holds.
- Entities whose feet are in the box, with what they ride and what rides them, without their uuids, so each copy placed gets new ones. Players aren't entities, so they're never in one.
Positions are from the box's lowest corner.
Placing
A mod's schematics/<name>.cbsm files are its schematics <mod id>:<name>, so an exported file can be dropped in as it
is. check-mods checks that every block and entity in them exists. They're registered before any script runs, so a
server phase can place them right away. Players never download them.
A script can also register one it made itself, by id in the server phase, then place it by that id:
ctx.schematics.register("copper_tools:hut", schematic);
ctx.schematics.place("copper_tools:hut", {
position: { x: 100, y: 64, z: 40 },
rotation: 90,
});
register checks it right away, so a schematic with a block, state or entity that isn't registered stops the mod from
loading, instead of failing when it's placed. It takes a Schematic (exported from cubi/server), the same shape as a
file's.
place puts the whole schematic in at once, replacing what was there, air too:
positionis where its lowest corner goes.rotationis0,90,180or270degrees clockwise seen from above.mirroris"x"or"z", the axis that gets flipped. The mirror is applied first, then the rotation, both aroundposition_pivot, a block from the schematic's lowest corner,0 0 0by default. Each placement can come out different, so a mod can turn the same room to face each way.- Only a state named
facingturns with it (0north,1east,2south,3west,4up and5down stay). Other states, like a stair's shape, stay as they are. Entities' positions and yaw turn too. - Replaced blocks get their
on_break, like withctx.world.set_block, so a chest it replaces drops what it holds. - Blocks the schematic has data or client data for get it as it is. Other blocks get their
on_create. Then every block placed tells the blocks beside it, so a fence at its edge joins what's outside. Blocks hear of it once all of them are in, so a door's two halves are both there by then. - Entities come with new uuids each time.
- Parts above or below the world are left out. Ungenerated chunks it reaches are generated first.
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:
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.
Block Data
Tile data goes into a schematic as it is, which is right for plain fields like a furnace's progress. Data that only
means something in this world, like the id of a container, needs an export_data
handler that writes what it points to in:
ctx.components.register_block("copper_tools:crate", {
export_data(block) {
const container = ctx.containers.get(block.data.container);
if (!container) return block.data;
const items = [];
for (let slot = 0; slot < container.size; slot++) items.push(container.get(slot) ?? null);
return { ...block.data, container: items };
},
});
It returns any JSON value, and leaves the block's own data alone. Like NeoForge's BlockEntity.saveWithoutMetadata,
it's what the block is outside the world. The first of a block's components whose export_data returns something other
than undefined decides, and a block without any keeps its data as it is.
The base game's chests, furnaces, crabpots, worm bins and cooking pots do this, with export_container_data in
mods/bworld/scripts/block_containers.ts.