Schematics#
Schematics: boxes of blocks to copy, turn, paste, save and load.
A Schematic holds a box of block states in memory,
indexed blocks[x][y][z] from its bottom north west corner, like the
cuboids of Item that grab returns and
Blocks renders. It can be read from the world, pasted into
it, converted to and from those cuboids, rotated and mirrored, and loaded
from and saved to files. Unlike a cuboid of Item, it keeps
block states, so stairs, slabs and logs still face the right way after a
copy or a turn.
The files are Sponge schematics (.schem): the format that WorldEdit,
Litematica and most other Minecraft tools exchange structures in. The
specification of the format is at
SpongePowered/Schematic-Specification. Files are written
in version 3, and versions 2 and 3 can be read. They are NBT files, read
and written with mciwb.builders.nbtfile.
- class mciwb.builders.Schematic(blocks, *, block_entities=None, offset=ORIGIN, metadata=None, data_version=DATA_VERSION)[source]#
A box of blocks with their block states.
Make one with
from_world(),from_cuboid()orload(), or from an array of your own.rotate()andmirror()return a new schematic and leave this one as it was.Block entity data (the contents of a chest, the text of a sign) can be held in two ways, which behave differently. Data in
block_entities, where a file keeps it, is saved but not pasted. Data in thenbtof a block state is pasted, but such a schematic cannot be saved.- Parameters:
blocks (ndarray) -- A 3D numpy array of
BlockState, indexed[x, y, z]. It is used as it is, not copied.block_entities (list[dict[str, Any]] | None) -- The block entities, as Sponge BlockEntity compounds. Defaults to none.
offset (Vec3) -- The Sponge
Offsetto save in a file.metadata (dict[str, Any] | None) -- The Sponge
Metadatacompound. Defaults to empty.data_version (int) -- The Minecraft data version that the blocks are from. Defaults to
DATA_VERSION.
- Raises:
ValueError -- If blocks is not 3 dimensional, or one of its sides has no blocks.
Example:
from mciwb.imports import Schematic, Vec3, Volume # copy a building with its stairs and slabs facing the right way, # and paste it turned a quarter turn clockwise corner1, corner2 = Vec3(0, 64, 0), Vec3(8, 72, 6) house = Schematic.from_world(Volume.from_corners(corner1, corner2)) house.save("house.schem") Schematic.load("house.schem").rotate(1).paste(Vec3(100, 64, 0))
- blocks#
The blocks, as a 3D numpy array of
BlockStateindexed[x, y, z].
- block_entities#
The block entities (chests, signs and so on) from a file, as Sponge BlockEntity compounds: dictionaries with
Pos(the position in the schematic),IdandData. They are kept when saving and moved byrotate()andmirror(), but are not pasted into the world.
- offset#
The Sponge
Offsetof the file, kept when saving, but not used when pasting.
- metadata#
The Sponge
Metadatacompound (Name,Authorand so on), kept when saving.
- data_version#
The Minecraft data version that the blocks are from.
- property palette: list[BlockState]#
The different block states in the schematic.
A new list each time, with each state once, in the order that the states first appear in
blocks(x slowest, z fastest).
- classmethod from_cuboid(cuboid)[source]#
Make a schematic from a 3D list or array of blocks.
- Parameters:
cuboid (Sequence[Any] | ndarray) -- The blocks, indexed
[x][y][z]: aCuboidfromgraborload_items, or any 3D list or numpy array ofItem, block state strings orBlockState.- Returns:
The schematic.
- Raises:
ValueError -- If cuboid is not 3 dimensional, one of its sides has no blocks, or a string in it is not a valid block state.
- Return type:
Example:
from mciwb.imports import Item, Schematic, Vec3 # 2 blocks along x, 1 high, 2 along z pad = Schematic.from_cuboid( [[[Item.STONE, Item.GOLD_BLOCK]], [["oak_slab[type=top]", "air"]]] ) pad.paste(Vec3(0, 70, 0))
- to_cuboid()[source]#
Convert the blocks to a 3D list of items, losing their block states.
- Returns:
The blocks as a 3D list of
Itemindexed[x][y][z], for use withBlocksorsave_items.- Raises:
ValueError -- If a block is not in
Item, such as a block from a mod or from a newer Minecraft release.- Return type:
- classmethod from_world(volume, states=True)[source]#
Read the blocks in a box of the world.
Reading is slow: one RCON command for each block, and when states is True a few more for each stairs, slab and log-like block, to find its
facing,half,shape,typeoraxis. Other block states, such as the facing of a door or a furnace, cannot be read back, and those blocks take their default state. Block entity data, such as the contents of a chest, is not read either. Some blocks read back as a related block: a wall torch as a torch, and a wall sign as a sign.Uses the RCON client of the current thread, from
get_client(). The box must be in loaded chunks.- Parameters:
- Returns:
The schematic, with
blocks[0][0][0]at the bottom north west corner of volume.- Raises:
ValueError -- If a position is not in a loaded chunk, or holds a block that is not in
Item.- Return type:
Example:
from mciwb.imports import Schematic, Vec3, Volume box = Volume.from_corners(Vec3(0, 64, 0), Vec3(8, 72, 6)) house = Schematic.from_world(box)
- paste(position, air=False)[source]#
Place the blocks in the world.
The blocks are placed a layer at a time, bottom layer first, with
Placer.layer: one command for each rectangle of the same block state. A block state that carries block entity data (nbt) is placed with it. Theblock_entitiesare not placed: if there are any, a warning is logged.- Parameters:
- Returns:
The box that the schematic covers in the world, whether or not every block in it was placed.
- Raises:
BuildError -- If the server rejects a command, for example for an unknown block or an unloaded chunk. The layers below the one that failed stay in the world.
- Return type:
Example:
from mciwb.imports import Schematic, Vec3 house = Schematic.load("house.schem") box = house.paste(Vec3(100, 64, 0), air=True)
- rotate(steps=1)[source]#
Make a copy of the schematic turned about the y axis.
The blocks move, and each block state turns too: see
BlockState.rotatefor the properties that are updated. ThePosof each of theblock_entitiesmoves with its block, but their data is not changed. Theoffsetis kept as it is.- Parameters:
steps (int) -- The number of quarter turns clockwise, viewed from above, so that a stairs block facing north faces east after one step. Negative numbers turn anticlockwise.
- Returns:
The turned schematic. After an odd number of steps its x and z sizes are swapped. After a whole number of full turns it is this schematic itself, not a copy.
- Return type:
Example:
from mciwb.imports import Schematic, Vec3 house = Schematic.load("house.schem") house.rotate(2).paste(Vec3(100, 64, 0)) # facing the other way
- mirror(axis)[source]#
Make a mirror image copy of the schematic.
The blocks move, and each block state is mirrored too: see
BlockState.mirrorfor the properties that are updated. ThePosof each of theblock_entitiesmoves with its block, but their data is not changed. Theoffsetis kept as it is.- Parameters:
axis (str) -- The axis to mirror along:
"x"swaps east and west,"z"swaps north and south.- Returns:
The mirrored schematic.
- Raises:
ValueError -- If axis is not
"x"or"z".- Return type:
Example:
from mciwb.imports import Schematic, Vec3 wing = Schematic.load("wing.schem") wing.paste(Vec3(100, 64, 0)) wing.mirror("x").paste(Vec3(120, 64, 0))
- classmethod load(filename)[source]#
Load a Sponge schematic file, version 2 or 3.
- Parameters:
filename (Path | str) -- The file to read, usually a
.schemfile. It may be gzip compressed (as they normally are) or not.- Returns:
The schematic, with the file’s block entities, offset, metadata and data version.
- Raises:
ValueError -- If the file is not NBT, is not a Sponge schematic of version 2 or 3, or does not hold as many blocks as its size says.
OSError -- If the file cannot be read.
- Return type:
Example:
from mciwb.imports import Schematic house = Schematic.load("house.schem") print(house.size, house.palette)
- save(filename)[source]#
Save as a Sponge schematic version 3 file.
The file is gzip compressed NBT. An existing file is replaced.
- Parameters:
filename (Path | str) -- The file to write, usually named
.schem.- Raises:
ValueError -- If a block state carries block entity data (
nbt): a Sponge palette holds block states only, and its block entities are NBT compounds inblock_entities. Also if a side of the schematic is longer than 65535 blocks.OSError -- If the file cannot be written.
Example:
from mciwb.imports import Schematic, Vec3, Volume box = Volume.from_corners(Vec3(0, 64, 0), Vec3(8, 72, 6)) Schematic.from_world(box).save("house.schem")
- mciwb.builders.DATA_VERSION: int#
The data version of the Minecraft release that mciwb supports (
mciwb.server.MINECRAFT_VERSION), written in saved schematics.
NBT files#
Read and write binary NBT files, the format of Sponge schematics.
NBT is the format of Minecraft’s data files, described at
https://minecraft.wiki/w/NBT_format. This module reads and writes whole
files in Java Edition’s big-endian form, gzip compressed or not, and is
what Schematic loads and saves with. Its names are
not in mciwb.imports: import them from mciwb.builders.nbtfile.
Tags map to Python types as follows. Reading always gives the typed wrappers, so that a file that is read and written again keeps its tag types. Writing also accepts the plain Python types in the last column.
NBT tag |
Read as |
Also written |
|---|---|---|
Byte |
||
Short |
||
Int |
int, bool |
|
Long |
||
Float |
||
Double |
float |
|
Byte Array |
bytes |
|
String |
str |
|
List |
list |
|
Compound |
dict |
|
Int Array |
||
Long Array |
mciwb.anvil.read_nbt() is a different function with the same name: it
decodes uncompressed NBT from a world’s region files into a plain
dictionary, with arrays as numpy arrays, for reading only.
read_nbt() here also returns the name of the
root tag, and keeps the tag types so that the data can be written back.
- class mciwb.builders.nbtfile.Byte[source]#
An NBT Byte: a whole number from -128 to 127 (tag type 1).
- class mciwb.builders.nbtfile.Double(x=0, /)[source]#
An NBT Double: a 64 bit floating point number (tag type 6).
- class mciwb.builders.nbtfile.Float(x=0, /)[source]#
An NBT Float: a 32 bit floating point number (tag type 5).
- class mciwb.builders.nbtfile.IntArray(iterable=(), /)[source]#
An NBT Int Array: a list of 32 bit signed whole numbers (tag type 11).
- class mciwb.builders.nbtfile.LongArray(iterable=(), /)[source]#
An NBT Long Array: a list of 64 bit signed whole numbers (tag type 12).
- class mciwb.builders.nbtfile.Short[source]#
An NBT Short: a whole number from -32768 to 32767 (tag type 2).
- class mciwb.builders.nbtfile.TagList(items=(), tag_type=None)[source]#
An NBT List: a list whose items all have the same tag type (tag type 9).
When a list is written, its items are all written with the tag type of its first item. A plain
listis written in the same way.- Parameters:
items -- The items of the list.
tag_type (int | None) -- The tag type number of the items. It only matters for an empty list, which is written with this type (or with type 0, End, if it is
None), so that an empty list that was read is written back with the type it had.
- tag_type#
The tag type number of the items as read from a file, or
None. Only used when writing an empty list.
- mciwb.builders.nbtfile.load_nbt(filename)[source]#
Read an NBT file.
- Parameters:
filename (Path | str) -- The file to read, gzip compressed or not.
- Returns:
A tuple
(name, value)-- the name of the root tag and its value, as fromread_nbt().- Raises:
ValueError -- If the file is not NBT with a root compound tag.
OSError -- If the file cannot be read.
- Return type:
Example:
from mciwb.builders.nbtfile import load_nbt name, root = load_nbt("house.schem") print(root["Schematic"]["Version"])
- mciwb.builders.nbtfile.read_nbt(data)[source]#
Decode NBT data that holds a root compound tag.
This is not
mciwb.anvil.read_nbt(), which returns only a dictionary (with numpy arrays) and does not accept compressed data.- Parameters:
data (bytes) -- The whole contents of an NBT file, gzip compressed or not.
- Returns:
A tuple
(name, value)-- the name of the root tag (usually""), and its value as a dictionary of the typed wrappers of this module. Anything after the root tag is ignored.- Raises:
ValueError -- If the data does not start with a compound tag, ends too soon, or holds an unknown tag type.
- Return type:
Example
>>> data = write_nbt({"Version": Int(3), "Name": "house"}) >>> read_nbt(data) ('', {'Version': 3, 'Name': 'house'})
- mciwb.builders.nbtfile.save_nbt(filename, value, name='')[source]#
Write a gzip compressed NBT file.
- Parameters:
filename (Path | str) -- The file to write. An existing file is replaced.
value (dict) -- The contents of the root tag, as for
write_nbt().name (str) -- The name of the root tag.
- Raises:
Example:
from mciwb.builders.nbtfile import Byte, save_nbt save_nbt("data.nbt", {"Count": Byte(3), "Name": "apple"})
- mciwb.builders.nbtfile.write_nbt(value, name='', compress=True)[source]#
Encode a root compound tag as NBT data.
- Parameters:
value (dict) -- The contents of the root tag. The values may be the typed wrappers of this module,
str,dict, or the plain types that have a default tag:intandbool(written as Int),float(Double),bytes(Byte Array) andlist(List).name (str) -- The name of the root tag.
compress (bool) -- Whether to gzip compress the data, as Minecraft and schematic files are.
- Returns:
The NBT data. The same value always gives the same bytes.
- Raises:
- Return type:
Example
>>> data = write_nbt({"Width": Short(5), "Tags": ["a", "b"]}, compress=False) >>> read_nbt(data) ('', {'Width': 5, 'Tags': ['a', 'b']})