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()orof(), which addminecraft: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()andmirror()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 thatsetblockandfillaccept, 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:{}}'
- 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.setblockandfillgive 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 aBlockState, which is returned as it is. A string may leave outminecraft:, and may end with block entity data in SNBT, assetblocktakes 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:
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 byfacing_name()), a bool (trueorfalse) 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
Vec3that is not a direction.- Return type:
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 (trueorfalse) 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
Vec3that is not a direction.- Return type:
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.
Noneor""removes the data.- Returns:
The new block state.
- Raises:
ValueError -- If nbt is not one SNBT compound
{...}.- Return type:
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
Itemfor 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.
- 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 theshapeof 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:
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 cornershapeof stairs (outer_leftbecomesouter_right), thehingeof a door and thetypeof 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:
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 aBlockState.
- mciwb.builders.facing_name(direction)[source]#
Give the block state name of a direction.
This is the value that properties such as
facingtake.- Parameters:
direction (Vec3) -- One of the six values of
Direction, or aVec3equal to one.- Returns:
"north","east","south","west","up"or"down".- Raises:
ValueError -- If direction is not one of the six directions.
- Return type:
Example
>>> facing_name(Direction.EAST) 'east'
- mciwb.builders.stair_shapes(stairs)[source]#
Work out the
shapethat 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
facingis 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_lefthas its high corner on thefacingside and the side anticlockwise from it,outer_righton the clockwise side. This followsStairBlock.getStairsShapein Minecraft.- Parameters:
stairs (Mapping[tuple[int, int], str]) -- The
facingof each stairs block ("north","east","south"or"west"), keyed by(x, z).- Returns:
The
shapeof each stairs block, keyed by(x, z)--"straight","inner_left","inner_right","outer_left"or"outer_right".- Return type:
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_SLABforItem.OAK_STAIRS.- Raises:
ValueError -- If stairs is not a stairs block, or there is no matching slab in
Item.- Return type:
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>_stairsit is the first of<name>_planks,<name>s,<name>_blockand<name>that is inItem.- Parameters:
stairs (Item | str | BlockState) -- A stairs block. Its properties are ignored.
- Returns:
The full block, for example
Item.OAK_PLANKSforItem.OAK_STAIRS.- Raises:
ValueError -- If stairs is not a stairs block, or no block matches.
- Return type:
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'>