Block states#

Block states: a block id with its properties and block entity data.

A block state is written as Minecraft’s setblock command takes it: the block id, then any properties in square brackets, then any block entity data in braces. For example minecraft:oak_stairs[facing=east,half=bottom,shape=straight], or minecraft:oak_sign[rotation=4]{front_text:{messages:["Hi","","",""]}}.

Every builder takes its blocks as a Block: an Item, a string in this form (minecraft: may be left out), or a BlockState. BlockState parses and formats these strings, changes their properties, and rotates and mirrors them about the y axis, which is how a Schematic is turned.

The functions here help with stairs. facing_name() gives the property value for a direction, slab_of() and full_block_of() find the slab and the full block of the same material, and stair_shapes() works out the shape that Minecraft gives each block in a layer of stairs, so that a builder can send the states that the server would settle on anyway.

class mciwb.builders.BlockState(name, props=<factory>, nbt='')[source]#

A block id, its block state properties and its block entity data.

Make one with parse() or of(), which add minecraft: to a bare block id and check the block entity data. The constructor itself stores what it is given, unchecked. A state cannot be changed: with_props(), with_nbt(), rotate() and mirror() return a new one.

States are equal when their id, properties and block entity data are equal, whatever the order of the properties, and they can be used as dictionary keys.

str(state) gives the form that setblock and fill accept, and that Sponge schematic palettes use, with the properties in alphabetical order:

>>> state = BlockState.of(Item.OAK_STAIRS, facing="east", half="bottom")
>>> str(state)
'minecraft:oak_stairs[facing=east,half=bottom]'
>>> str(state.rotate(1))
'minecraft:oak_stairs[facing=south,half=bottom]'
>>> BlockState.parse("stone").name
'minecraft:stone'
>>> sign = BlockState.parse("oak_sign[rotation=4]{front_text:{}}")
>>> sign.nbt
'{front_text:{}}'
name: str#

The namespaced block id, for example "minecraft:oak_stairs".

props: Mapping[str, str]#

The block state properties, for example {"facing": "east"}. Properties that are not given take the block’s default value when the block is placed.

nbt: str = ''#

The block entity data (the contents of a chest, the text of a sign) as SNBT text, including its braces, for example '{Items:[{Slot:0b,id:"minecraft:apple",count:3}]}', or "" for none. setblock and fill give it to every block they place. It is compared as text, so states that differ only in the spacing or order of their NBT are not equal.

classmethod parse(block)[source]#

Make a block state from any kind of block.

Parameters:

block (Item | str | BlockState) -- An Item, a block state string, or a BlockState, which is returned as it is. A string may leave out minecraft:, and may end with block entity data in SNBT, as setblock takes it: "chest[facing=west]{Items:[...]}".

Returns:

The block state. Whether the block and its properties exist is not checked: the server does that when the block is placed.

Raises:

ValueError -- If a string is not in the form of a block state: no block id, a property without =, brackets or braces that do not match, or text after the block entity data.

Return type:

BlockState

Example

>>> BlockState.parse("oak_stairs[facing=east]").props
{'facing': 'east'}
classmethod of(block, **props)[source]#

Make a block state from a block and extra properties.

Parameters:
  • block (Item | str | BlockState) -- The block, as for parse(). Its block entity data is kept.

  • **props (str | bool | int | Vec3) -- The properties to set, replacing any that the block already has. A value may be a string, a Direction (converted to its name, as by facing_name()), a bool (true or false) or an int (its digits).

Returns:

The block state.

Raises:

ValueError -- If block is a string that is not a valid block state, or a value is a Vec3 that is not a direction.

Return type:

BlockState

Example

>>> str(BlockState.of(Item.OAK_STAIRS, facing=Direction.EAST))
'minecraft:oak_stairs[facing=east]'
>>> str(BlockState.of(Item.SNOW, layers=3))
'minecraft:snow[layers=3]'
with_props(**props)[source]#

Make a copy of this state with some properties added or replaced.

Parameters:

**props (str | bool | int | Vec3) -- The properties to set. A value may be a string, a Direction (converted to its name), a bool (true or false) or an int (its digits).

Returns:

The new block state. The other properties and the block entity data are kept.

Raises:

ValueError -- If a value is a Vec3 that is not a direction.

Return type:

BlockState

Example

>>> slab = BlockState.parse("oak_slab[type=bottom]")
>>> str(slab.with_props(type="top", waterlogged=True))
'minecraft:oak_slab[type=top,waterlogged=true]'
with_nbt(nbt)[source]#

Make a copy of this state with its block entity data replaced.

Parameters:

nbt (str | None) -- The block entity data as SNBT text, one compound in braces. None or "" removes the data.

Returns:

The new block state.

Raises:

ValueError -- If nbt is not one SNBT compound {...}.

Return type:

BlockState

Example

>>> apple = '{Items:[{Slot:0b,id:"minecraft:apple",count:3}]}'
>>> chest = BlockState.parse("chest[facing=west]").with_nbt(apple)
>>> str(chest.with_nbt(None))
'minecraft:chest[facing=west]'
property item: Item#

The Item for this block, without its properties.

Raises:

ValueError -- If the block is not in Item, such as a block from a mod or from a newer Minecraft release.

property is_air: bool#

Whether this is air, cave_air or void_air.

rotate(steps=1)[source]#

Make a copy of this state turned about the y axis.

Updates facing, axis, rotation (signs, banners, heads), the connections named after directions (fences, walls, panes, redstone) and the shape of rails. Block entity data is kept unchanged.

Parameters:

steps (int) -- The number of quarter turns clockwise, viewed from above, so that north becomes east. Negative numbers turn anticlockwise.

Returns:

The turned block state -- this one itself after a whole number of full turns.

Return type:

BlockState

Example

>>> str(BlockState.parse("oak_fence[east=true]").rotate(1))
'minecraft:oak_fence[south=true]'
mirror(axis)[source]#

Make a mirror image copy of this state.

Updates the same properties as rotate(), and also swaps left and right: the corner shape of stairs (outer_left becomes outer_right), the hinge of a door and the type of a double chest. Block entity data is kept unchanged.

Parameters:

axis (str) -- The axis to mirror along: "x" swaps east and west, "z" swaps north and south.

Returns:

The mirrored block state.

Raises:

ValueError -- If axis is not "x" or "z".

Return type:

BlockState

Example

>>> str(BlockState.parse("oak_door[facing=east,hinge=left]").mirror("x"))
'minecraft:oak_door[facing=west,hinge=right]'
mciwb.builders.Block: Item | str | BlockState#

The type of an argument that takes a block: an Item, a block state string such as "minecraft:stone" or "oak_stairs[facing=east]", or a BlockState.

mciwb.builders.facing_name(direction)[source]#

Give the block state name of a direction.

This is the value that properties such as facing take.

Parameters:

direction (Vec3) -- One of the six values of Direction, or a Vec3 equal to one.

Returns:

"north", "east", "south", "west", "up" or "down".

Raises:

ValueError -- If direction is not one of the six directions.

Return type:

str

Example

>>> facing_name(Direction.EAST)
'east'
mciwb.builders.stair_shapes(stairs)[source]#

Work out the shape that Minecraft gives each block in a layer of stairs.

The layer is one horizontal layer of stairs that all have the same half. Nothing is sent to the server.

A stairs block’s facing is the side of its full height step, so people walking up it travel in that direction. A corner forms when the stairs in front of (or behind) it face across it: outer_left has its high corner on the facing side and the side anticlockwise from it, outer_right on the clockwise side. This follows StairBlock.getStairsShape in Minecraft.

Parameters:

stairs (Mapping[tuple[int, int], str]) -- The facing of each stairs block ("north", "east", "south" or "west"), keyed by (x, z).

Returns:

The shape of each stairs block, keyed by (x, z) -- "straight", "inner_left", "inner_right", "outer_left" or "outer_right".

Return type:

dict[tuple[int, int], str]

Example

The north west corner of a ring of stairs that face inwards, as in a hip roof:

>>> stair_shapes({(0, 0): "south", (1, 0): "south", (0, 1): "east"})
{(0, 0): 'outer_left', (1, 0): 'straight', (0, 1): 'straight'}
mciwb.builders.slab_of(stairs)[source]#

Find the slab made of the same material as a stairs block.

Parameters:

stairs (Item | str | BlockState) -- A stairs block. Its properties are ignored.

Returns:

The slab, for example Item.OAK_SLAB for Item.OAK_STAIRS.

Raises:

ValueError -- If stairs is not a stairs block, or there is no matching slab in Item.

Return type:

Item

Example

>>> slab_of(Item.OAK_STAIRS)
<Item.OAK_SLAB: 'minecraft:oak_slab'>
mciwb.builders.full_block_of(stairs)[source]#

Find the full block made of the same material as a stairs block.

The block is found by its name: for <name>_stairs it is the first of <name>_planks, <name>s, <name>_block and <name> that is in Item.

Parameters:

stairs (Item | str | BlockState) -- A stairs block. Its properties are ignored.

Returns:

The full block, for example Item.OAK_PLANKS for Item.OAK_STAIRS.

Raises:

ValueError -- If stairs is not a stairs block, or no block matches.

Return type:

Item

Example

>>> full_block_of(Item.OAK_STAIRS)
<Item.OAK_PLANKS: 'minecraft:oak_planks'>
>>> full_block_of("stone_brick_stairs")
<Item.STONE_BRICKS: 'minecraft:stone_bricks'>