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() or load(), or from an array of your own. rotate() and mirror() 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 the nbt of 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 Offset to save in a file.

  • metadata (dict[str, Any] | None) -- The Sponge Metadata compound. 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 BlockState indexed [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), Id and Data. They are kept when saving and moved by rotate() and mirror(), but are not pasted into the world.

offset#

The Sponge Offset of the file, kept when saving, but not used when pasting.

metadata#

The Sponge Metadata compound (Name, Author and so on), kept when saving.

data_version#

The Minecraft data version that the blocks are from.

property size: Vec3#

The size of the schematic in blocks, as (width x, height y, length z).

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]: a Cuboid from grab or load_items, or any 3D list or numpy array of Item, block state strings or BlockState.

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:

Schematic

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 Item indexed [x][y][z], for use with Blocks or save_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:

list

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, type or axis. 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:
  • volume (Volume) -- The box of the world to read.

  • states (bool) -- Whether to read the block states of stairs, slabs and log-like blocks. If False, every block takes its default state, which is quicker.

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:

Schematic

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. The block_entities are not placed: if there are any, a warning is logged.

Parameters:
  • position (Vec3) -- Where to put the schematic’s bottom north west corner (blocks[0][0][0]). Rounded down to a whole block.

  • air (bool) -- If True, also place the air blocks, clearing whatever is in the box. If False, leave the world unchanged where the schematic has air.

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:

Volume

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.rotate for the properties that are updated. The Pos of each of the block_entities moves with its block, but their data is not changed. The offset is 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:

Schematic

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.mirror for the properties that are updated. The Pos of each of the block_entities moves with its block, but their data is not changed. The offset is 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:

Schematic

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 .schem file. 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:

Schematic

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 in block_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

Byte

Short

Short

Int

Int

int, bool

Long

Long

Float

Float

Double

Double

float

Byte Array

ByteArray

bytes

String

str

List

TagList

list

Compound

dict

Int Array

IntArray

Long Array

LongArray

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.ByteArray[source]#

An NBT Byte Array, as bytes (tag type 7).

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.Int[source]#

An NBT Int: a 32 bit signed whole number (tag type 3).

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.Long[source]#

An NBT Long: a 64 bit signed whole number (tag type 4).

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 list is 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 from read_nbt().

Raises:
  • ValueError -- If the file is not NBT with a root compound tag.

  • OSError -- If the file cannot be read.

Return type:

tuple[str, dict]

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:

tuple[str, dict]

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:
  • TypeError -- If a value has a type that cannot be written.

  • error -- If a number does not fit in its tag.

  • OSError -- If the file cannot be written.

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: int and bool (written as Int), float (Double), bytes (Byte Array) and list (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:
  • TypeError -- If a value has a type that cannot be written, such as None or a tuple.

  • error -- If a number does not fit in its tag, or an item of a list does not suit the tag type of the first item.

Return type:

bytes

Example

>>> data = write_nbt({"Width": Short(5), "Tags": ["a", "b"]}, compress=False)
>>> read_nbt(data)
('', {'Width': 5, 'Tags': ['a', 'b']})