Export schematics
This commit is contained in:
@@ -74,6 +74,7 @@ the block and the component.
|
||||
| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | |
|
||||
| `on_entity_stand(block, params, entity)` | An entity standing on it moves, every tick it does. See [Entities on It](#entities-on-it). | |
|
||||
| `fluid_handler(block, params, face?)` | Something reaches for its tanks from `face`. See [Fluid Handlers](../resources/fluids.md#fluid-handlers). | a `FluidHandler`, or `undefined` |
|
||||
| `export_data(block, params)` | It goes into a schematic. See [Schematics](../misc/schematics.md#block-data). | its data for the schematic |
|
||||
|
||||
`block` is `{ id, x, y, z, biome, season, data, get_client_data, set_client_data, set_ticking }`, where `data` is the
|
||||
block's [tile data](tiledata.md) and the client data functions its [part clients see](tiledata.md#client-data), `biome`
|
||||
|
||||
@@ -56,6 +56,9 @@ ctx.components.register_block("copper_tools:crate", {
|
||||
|
||||
The base game's `bworld:storage` does exactly this, see `mods/bworld/scripts/server.ts`.
|
||||
|
||||
The container's id means nothing outside the world, so a block like this should also have an `export_data` that writes
|
||||
its items in, for when it's saved into a [schematic](../misc/schematics.md#block-data).
|
||||
|
||||
## Client Data
|
||||
|
||||
Client data is the part of a block's data its clients see too, like how fast a shaft turns. It's kept by field, each any
|
||||
|
||||
@@ -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