Block class

class PyMCTranslate.py3.api.amulet_objects.block.Block(namespace, base_name, properties=None, extra_blocks=None)[source]

Bases: object

A class to manage the state of a block.

It is an immutable object that contains a namespaced name, properties and extra blocks.

Here’s a few examples on how create a Block object:

>>> # Create a block with the namespace `minecraft` and base name `stone`
>>> stone = Block("minecraft", "stone")
>>> # Create a water block with the level property
>>> water = Block(
>>>     "minecraft",  # the namespace
>>>     "water",  # the base name
>>>     {  # A dictionary of properties.
>>>         # Keys must be strings and values must be a numerical or string NBT type.
>>>         "level": StringTag("0")  # define a property `level` with a string value `1`
>>>     }
>>> )
>>> # The above two examples can also be achieved by creating the block from the Java blockstate.
>>> stone = Block.from_string_blockstate("minecraft:stone")
>>> water = Block.from_string_blockstate("minecraft:water[level=0]")

Java 1.13 added the concept of waterlogging blocks whereby some blocks have a waterlogged property. Bedrock achieved the same behaviour by added a layering system. The layering system allows any block to be the second block. In order to support both formats and future proof the editor Amulet adds the concept of extra blocks. Extra blocks are a sequence of zero or more blocks stored in the extra_blocks property of the Block instance. Amulet places no restrictions on which blocks can be extra blocks but some validation will be done when saving based on the format being saved to.

>>> # Create a waterlogged stone block.
>>> waterlogged_stone = Block("minecraft", "stone",
>>>     # extra_blocks can be a Block instance or iterable of Block instances.
>>>     extra_blocks=Block("minecraft", "water", {"level": StringTag("0")})
>>> )
>>> # The above can also be achieved by adding together a stone and water block.
>>> waterlogged_stone = stone + water
>>> repr(waterlogged_stone)
'Block(minecraft:stone, extra_blocks=(minecraft:water[level="0"]))'
property base_block: Block

Returns an instance of Block containing only the base block without any extra blocks

>>> waterlogged_stone = Block("minecraft", "stone",
>>>     extra_blocks=Block("minecraft", "water", {"level": StringTag("0")})
>>> )
>>> waterlogged_stone.base_block
Block(minecraft:stone)
Returns

A Block object

property base_name: str

The base name of the blockstate represented by the Block object.

>>> water = Block.from_string_blockstate("minecraft:water[level=0]")
>>> water.base_name
'water'
Returns

The base name of the blockstate

property block_tuple: Tuple[Block, ...]

Returns the stack of blocks represented by this object as a tuple. This is a tuple of base_block and extra_blocks

>>> waterlogged_stone = Block("minecraft", "stone",
>>>     extra_blocks=Block("minecraft", "water", {"level": StringTag("0")})
>>> )
>>> waterlogged_stone.block_tuple
(Block(minecraft:stone), Block(minecraft:water[level="0"]))
Returns

A tuple of Block objects

property blockstate: str

The Java blockstate string of this Block object Note if there are extra blocks this will only show the base block. Note this will only contain properties with StringTag values.

>>> stone = Block("minecraft", "stone")
>>> stone.blockstate
'minecraft:stone'
>>> water = Block("minecraft", "water", {"level": StringTag("0")})
>>> water.blockstate
`minecraft:water[level=0]`
Returns

The blockstate string

property extra_blocks: Tuple[Block, ...]

Returns a tuple of the extra blocks contained in the Block instance

>>> waterlogged_stone = Block("minecraft", "stone",
>>>     extra_blocks=Block("minecraft", "water", {"level": StringTag("0")})
>>> )
>>> waterlogged_stone.extra_blocks
(Block(minecraft:water[level="0"]),)
Returns

A tuple of Block objects

classmethod from_snbt_blockstate(blockstate)[source]

Parse a blockstate where values are SNBT of any type and populate a Block class with the data.

classmethod from_string_blockstate(blockstate)[source]

Parse a Java format blockstate where values are all strings and populate a Block class with the data.

>>> stone = Block.from_string_blockstate("minecraft:stone")
>>> water = Block.from_string_blockstate("minecraft:water[level=0]")
Parameters

blockstate (str) – The Java blockstate string to parse.

Returns

A Block instance containing the state.

property full_blockstate: str

The SNBT blockstate string of the base block and extra blocks.

>>> bell = Block("minecraft", "bell", {
>>>     "attachment":StringTag("standing"),
>>>     "direction":IntTag(0),
>>>     "toggle_bit":ByteTag(0)
>>> })
>>> water = Block("minecraft", "water", {"liquid_depth": IntTag(0)})
>>> waterlogged_bell = bell + water
>>> waterlogged_bell.full_blockstate
'minecraft:bell[attachment="standing",direction=0,toggle_bit=0b]{minecraft:water[liquid_depth=0]}'
Returns

The blockstate string

property namespace: str

The namespace of the blockstate represented by the Block object.

>>> water = Block.from_string_blockstate("minecraft:water[level=0]")
>>> water.namespace
'minecraft'
Returns

The namespace of the blockstate

property namespaced_name: str

The namespace:base_name of the blockstate represented by the Block object.

>>> water = Block.from_string_blockstate("minecraft:water[level=0]")
>>> water.namespaced_name
'minecraft:water'
Returns

The namespace:base_name of the blockstate

static parse_blockstate_string(blockstate, snbt=False)[source]

Parse a Java or SNBT blockstate string and return the raw data.

To parse the blockstate and return a Block instance use from_string_blockstate() or from_snbt_blockstate()

Parameters
  • blockstate (str) – The blockstate to parse

  • snbt (bool) – Are the property values in SNBT format. If false all values must be an instance of StringTag

Return type

Tuple[str, str, Dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]]]

Returns

namespace, block_name, properties

property properties: Dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]]

The mapping of properties of the blockstate represented by the Block object.

>>> water = Block.from_string_blockstate("minecraft:water[level=0]")
>>> water.properties
{"level": StringTag("0")}
Returns

A dictionary of the properties of the blockstate

remove_layer(layer)[source]

Removes the block at the given index and returns the resulting new Block object.

>>> stone = Block("minecraft", "stone")
>>> water = Block("minecraft", "water", {"level": StringTag("0")})
>>> waterlogged_stone = stone + water
>>> stone = waterlogged_stone.remove_layer(1)
Parameters

layer (int) – The layer of extra block to remove.

Return type

Block

Returns

A new instance of Block with the same data but with the block at the specified layer removed.

Raises

BlockException – Raised when you remove the base block from a Block with no other extra blocks.

property snbt_blockstate: str

A modified version of the Java blockstate format that supports all NBT types. Converts the property values to the SNBT format to preserve type. Note if there are extra blocks this will only show the base block.

>>> bell = Block("minecraft", "bell", {
>>>     "attachment":StringTag("standing"),
>>>     "direction":IntTag(0),
>>>     "toggle_bit":ByteTag(0)
>>> })
>>> bell.snbt_blockstate
'minecraft:bell[attachment="standing",direction=0,toggle_bit=0b]'
Returns

The SNBT blockstate string