# 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. Exporting, registering, reading and placing are built. Blocks making what their data points to when they're placed (`import_data`) is planned. ## 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. ## Placing A mod's `schematics/.cbsm` files are its schematics `:`, so an exported file can be dropped in as it is. `check-mods` checks that every block and entity in them exists. A missing block is only a warning: it's left out when the schematic loads, so one made with blocks from another mod still works without it. 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: ```ts 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 state or entity that isn't registered stops the mod from loading, instead of failing when it's placed. Blocks that aren't registered are left out, with a warning. 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: - `position` is where its lowest corner goes. - `rotation` is `0`, `90`, `180` or `270` degrees clockwise seen from above. `mirror` is `"x"` or `"z"`, the axis that gets flipped. The mirror is applied first, then the rotation, both around `position_pivot`, a block from the schematic's lowest corner, `0 0 0` by default. Each placement can come out different, so a mod can turn the same room to face each way. - Only a state named `facing` turns with it (`0` north, `1` east, `2` south, `3` west, `4` up and `5` down 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 with `ctx.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. `/placeschematic id x y z` places one unturned, with its lowest corner at `x y z`. ## 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. `ids` gives every registered schematic's id, the files' and the ones scripts registered, in the order they were registered, like for a tool that lets players pick one: ```ts const rooms = ctx.schematics.ids().filter((id) => id.startsWith("copper_tools:room_")); ``` 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](../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`.