Export schematics
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# 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](https://msgpack.org), see
|
||||
`common/schematic.ts` for their format.
|
||||
|
||||
Only exporting is built so far. Placing schematics, and mods shipping their own, are 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:
|
||||
|
||||
```ts
|
||||
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](../blocks/tiledata.md), see [Block Data](#block-data), and its
|
||||
[client data](../blocks/tiledata.md#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.
|
||||
|
||||
## 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](../inventories/containers.md), needs an `export_data`
|
||||
handler that writes what it points to in:
|
||||
|
||||
```ts
|
||||
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`.
|
||||
Reference in New Issue
Block a user