Placing blocks#
The block placer that every builder is made with.
Placer sends setblock and fill commands,
checks the server’s reply to each one and records the bounding box of the
blocks it was asked to place. Each builder makes a
Placer, places its blocks through it and returns
its volume. A builder of your own can do the
same.
A reply that does not report success raises
BuildError. A command that had nothing to change,
because the blocks were already there, counts as success, so building the
same thing twice is not an error.
- class mciwb.builders.Placer(client=None)[source]#
Place blocks, recording the bounding box of every block placed.
Positions are rounded down to whole blocks. Blocks are given as any
Block, and are sent to the server as the text of aBlockState.- Parameters:
client (Client | None) -- The RCON client to send the commands with. Defaults to the client of the current thread, from
get_client().
Example:
from mciwb.imports import Item, Placer, Vec3, Volume def table(pos: Vec3) -> Volume: placer = Placer() placer.setblock(pos, Item.OAK_FENCE) placer.fill( pos + Vec3(-1, 1, -1), pos + Vec3(1, 1, 1), "oak_slab[type=top]" ) return placer.volume
- client#
The RCON client that the commands are sent with.
- property volume: Volume#
The bounding box of everything placed so far.
This covers every position given to
setblock(),fill(),layer()andinclude(), including blocks that a command left as they were.- Raises:
ValueError -- If nothing has been placed.
- include(start, end=None)[source]#
Extend the bounding box to include a block, or a box of blocks.
Use this for blocks placed by other means, such as
place_door()or a command sent with the client, so thatvolumecovers them too. Nothing is sent to the server.- Parameters:
Example:
from mciwb.imports import Direction, Item, Placer, Vec3, get_world placer = Placer() pos = Vec3(0, 70, 0) # a door is two blocks high get_world().place_door(pos, Item.OAK_DOOR, Direction.SOUTH) placer.include(pos, pos + Direction.UP)
- setblock(pos, block)[source]#
Set one block.
- Parameters:
pos (Vec3) -- The position of the block.
block (Item | str | BlockState) -- The block to place. It may carry block state properties and block entity data, such as the text of a sign.
- Returns:
The server’s reply.
- Raises:
ValueError -- If block is a string that is not a valid block state.
BuildError -- If the server rejects the command.
- Return type:
Example:
placer.setblock(pos, "oak_stairs[half=top]") text = '{front_text:{messages:["Hi","","",""]}}' placer.setblock(pos + Vec3(0, 1, 0), "oak_sign[rotation=4]" + text)
- fill(start, end, block, mode=None, filter=None)[source]#
Fill the box between two opposite corners, inclusive.
The server changes at most 32768 blocks in one
fillcommand, so a bigger box is split in two across its longest side, again and again until every part is small enough, and sent as several commands.- Parameters:
start (Vec3) -- One corner of the box.
end (Vec3) -- The opposite corner of the box.
block (Item | str | BlockState) -- The block to fill with. Block entity data in it, as in
"chest{Items:[...]}", is given to every block.mode (FillMode | None) -- How to treat the blocks already in the box, a
FillMode: for exampleFillMode.KEEPto replace only air, orFillMode.HOLLOWto fill only the outside of the box and set the inside to air. By default every block is replaced.filter (str | None) -- Only replace these blocks: a block such as
"minecraft:stone", or a tag such as"#minecraft:logs". ImpliesFillMode.REPLACE.
- Returns:
The server’s reply. For a box that was split, this is the reply for the last part only.
- Raises:
ValueError -- If filter is given with a mode other than
FillMode.REPLACE, or block is a string that is not a valid block state.BuildError -- If the server rejects a command. Parts of a split box that were already filled stay filled.
- Return type:
Example:
from mciwb.imports import FillMode, Item, Placer, Vec3 placer = Placer() start, end = Vec3(0, 70, 0), Vec3(6, 74, 6) placer.fill(start, end, Item.STONE_BRICKS, FillMode.HOLLOW) placer.fill(start, end, Item.MOSSY_STONE_BRICKS, filter="minecraft:stone_bricks")
- layer(y, cells)[source]#
Place one horizontal layer of blocks, given cell by cell.
The cells of each block are grouped into rectangles, to send as few commands as possible: one
fillfor each rectangle, or onesetblockfor a rectangle that is a single cell. An empty mapping places nothing.- Parameters:
- Raises:
ValueError -- If a block is a string that is not a valid block state.
BuildError -- If the server rejects a command.
Example:
from mciwb.imports import Item, Placer placer = Placer() # a 5 by 5 chequerboard at y = 70 placer.layer(70, { (x, z): Item.QUARTZ_BLOCK if (x + z) % 2 else Item.COAL_BLOCK for x in range(5) for z in range(5) })
- exception mciwb.builders.BuildError[source]#
The server rejected a building command.
Raised by
Placer, and so by every builder, when the reply to asetblockorfillcommand is not one that reports success. The usual causes are an unknown block or block state property, and a position in a chunk that is not loaded or outside the world. The message holds the command and the server’s reply.ground_heights()raises it too, when it cannot read the ground in a column.Blocks placed before the command that failed stay in the world.