class World

Manipulating the world is the heart piece of the entire library. With this you can query blocks and world features and set them in turn, as well as finding and spawning entities in the world. This allows building on the server quickly and precisely with only a few commands. Note that all commands of World will only manipulate that world, for example, getEntities() will only return entities in this world.

Do not instantiate the World class directly but use one of the following methods instead:

worlds = mc.worlds
world = mc.overworld
world = mc.nether
world = mc.end
someotherworld = mc.getWorldByKey("mod_namespace:world_key")

Once you have your world you can use it in a multitude of ways:

world.pvp = False  # disable pvp only in this world
ground_pos = world.getHeighestPos(0, 0)  # get position of heighest ground at origin
block = world.getBlock(ground_pos)  # get the block type there
world.setBlock("diamond_block", ground_pos)  # replace that block at location with diamond
# set every second block in a line along the x axis to emerald
world.setBlockList("emerald_block", [ground_pos.up(2).east(i) for i in range(0,20,2)])
# set a 10 x 10 x 10 oak plank block 50 blocks above ground
world.setBlockCube("oak_planks", ground_pos.up(50), ground_pos.up(50) + 9)
sheep = world.spawnEntity("sheep", ground_pos.up(1))  # spawn a sheep on that block
entities = world.getEntities()  # get all (loaded) entities
# get all  (loaded) entities around origin highest block in 20 block radius
entities = world.getEntitiesAround(ground_pos, 20)
world.removeEntities("sheep")  # remove all (loaded) sheep
# copy all blocks between those two points (inclusive)
blocks = world.copyBlockCube(Vec3(0,0,0), Vec3(5,5,5))
# paste back the copied blocks 20 blocks above origin ground
world.pasteBlockCube(blocks, ground_pos.up(20))
world.setBed(ground_pos.up(1))  # place a bed on top of diamond block

Note

When using the world functions on Minecraft (mc) directly the ‘default’ world is used. The default world is defined by the server as the first world loaded by the server, which is usually the overworld. If you want to affect a specific world, use World classes instead, such as mc.overworld, mc.nether or mc.end.

property key: str

The key/id of this world, e.g., "minecraft:overworld" or "minecraft:the_nether"

property name: str

The name of the folder/namespace the world resides in, e.g., "world" or "world_the_end"

property pvp: bool

True if pvp is enabled in this world. Can be set to enable or disable pvp in only this world.

runCommand(command: str) None

Run the command as if it was typed in chat as /-command and executed in this specific world/dimension. Returns immediately without waiting for the command to finish executing.

world.runCommand("kill @e")  # kill all loaded entities in this world
Parameters:

command (str) – the command without the slash /

getHighestPos(x: int, z: int) Vec3

The position of the highest non-air block with given x and z in the world.

Returns:

The position of the highest non-air block with given x and z

Return type:

Vec3

getHeight(x: int, z: int) int

Equivalent to the y value of getHighestPos() with x and z.

getBlock(pos: Vec3) Block

The block Block type/id at position pos in world.

Note

The function does only query the block type/id, no block component data is not queried. To query block component data use getBlockWithData().

Parameters:

pos (Vec3) – position to query block from

Returns:

block type/id at queried position

Return type:

Block

getBlockWithData(pos: Vec3) Block

The block Block at position pos in world including block component data.

Parameters:

pos (Vec3) – position to query block from

Returns:

block type/id and component data at queried position

Return type:

Block

getBlockList(positions: list[Vec3]) list[Block]

The list of all block Block types/ids at given positions in world in the same order.

Note

The function does only query the block type/ids, no block component data is not queried.

Parameters:

positions (list[Vec3]) – list of positions to query

Returns:

list of block types/ids at given positions (same order)

Return type:

list[Block]

getBlockListWithData(positions: list[Vec3]) list[Block]

The list of all block Block at given positions in world with component data in the same order.

Parameters:

positions (list[Vec3]) – list of positions to query

Returns:

list of block type/ids and component data at given positions (same order)

Return type:

list[Block]

setBlock(blocktype: str | Block, pos: Vec3) None

Change the block at position pos to blocktype in world. This will overwrite any block at that position.

Parameters:
  • blocktype (str | Block) – the valid block type/id or Block to set the block to

  • pos (Vec3) – the position where the block should be set

setBlockList(blocktype: str | Block, positions: list[Vec3]) None

Change all blocks at positions to blocktype in world. This will overwrite all blocks at the given positions. This is more efficient that using setBlock() multiple times with the same blocktype.

Parameters:
  • blocktype (str | Block) – the valid block type/id to set the blocks to

  • positions (list[Vec3]) – the positions where the blocks should be set

setBlockCube(blocktype: str | Block, pos1: Vec3, pos2: Vec3) None

Change all blocks in a cube between the corners pos1 and pos2 in world to blocktype, where both positions are inclusive. meaning that both given positions/corners will be part of the cube. This will overwrite all blocks between the given positions. The positions span a cube if all their coordinates are different, a plane if one of their coordinates is the same or a line if two of their three coordinates are the same.

# xlen, ylen, zlen are the length of the cube in the specified axis
start = Vec3()  # this would be the lowest (most negative coordinates) corner of the cube we want to draw, here origin
world.setBlockCube("diamond_block", start, start.addX(xlen).addY(ylen).addZ(zlen))

# this is equivalent to (but more efficent and faster than)

for x in range(xlen + 1):
    for y in range(ylen + 1):
        for z in range(zlen + 1):
            world.setBlock("diamond_block", start.addX(x).addY(y).addZ(z))
Parameters:
  • blocktype (str | Block) – the valid block type/id to set the blocks to

  • pos1 (Vec3) – the position of one corner of the cube

  • pos2 (Vec3) – the position of the opposite corner of the cube

setBed(pos: Vec3, direction: Literal['east', 'south', 'west', 'north'] = 'east', color: Literal['white', 'orange', 'magenta', 'light_blue', 'yellow', 'lime', 'pink', 'gray', 'light_gray', 'cyan', 'purple', 'blue', 'brown', 'green', 'red', 'black'] = 'red') None

Place a bed at pos in direction with color, which is composed of two placed blocks with specific block data.

Parameters:
  • pos (Vec3) – the position of foot part of bed

  • direction (CARDINAL, optional) – direction in which to place head part of bed, defaults to “east”

  • color (COLOR, optional) – color of bed, defaults to “red”

setSign(pos: Vec3, text: list[str | NbtCompound | dict] | str, *, color: Literal['white', 'orange', 'magenta', 'light_blue', 'yellow', 'lime', 'pink', 'gray', 'light_gray', 'cyan', 'purple', 'blue', 'brown', 'green', 'red', 'black'] = 'black', glowing: bool = False, direction: Literal['east', 'south', 'west', 'north'] | int = 'south', sign_block: str | Block = 'oak_sign') None

Place a sign block at pos with text overwriting any block there. text can be a list of at most 8 elements, where each element is a line of text on the sign; the first 4 on the front and the second 4 on the back. Lists with fewer than 8 elements are allowed and result in empty following lines. text may also be a string in which case the above list is built by splitting the string on newlines. The elements in the list may also be NBT instances of the form: {selector: "@p", color: "red", bold: false, italic: false, underlined: false, strikethrough: false, obfuscated: false, text: "A Line of Text"} The material and type of sign can be set with sign_block - it can be a sign, wall_sign, or hanging_sign of any given wood material.

pos = Vec3(0, 120, 0) # position where to place sign
mc.setSign(pos, "Hello Minecraft") # front line 1
mc.setSign(pos, "Hello\nMinecraft") # front line 1 and 2
mc.setSign(pos, ["Hello", "Minecraft"]) # front line 1 and 2
mc.setSign(pos, ["", "Hello", "Minecraft"]) # front line 2 and 3
# back line 6 and 7
mc.setSign(pos, ["", "", "", "",  "", "Hello", "Minecraft"])
# everything beyond line 8 (back line 4) is not on sign anymore
mc.setSign(pos, ["", "", "", "",  "", "", "", "",  "NOT ON SIGN"])
# line-wise customization with NBT compounds or dicts
mc.setSign(pos, [{"text": "Hello", "color": "red"}, NBT({"text": "Minecraft", "bold": True})])

valid_signs = mc.blocks.endswith("_sign")
mc.setSign(..., sign_block="spruce_sign")
mc.setSign(..., sign_block="jungle_wall_sign")
mc.setSign(..., sign_block="acacia_hanging_sign")
mc.setSign(..., sign_block="acacia_hanging_sign[attached=true]")
mc.setSign(..., sign_block=Block("oak_sign").withData({"waterlogged": True}))

mc.setSign(pos, "\nHello\nMinecraft\n", direction="east", color="green", glowing=True)
Parameters:
  • pos (Vec3) – the position where the sign should be set

  • text (list[str | NBT | dict[str, Any]] | str) – the text to put on sign either as list of at most 8 elements, where the first 4 are text on the front and the second set of 4 are the text on the back of the sign, or as a string, where said list is produced by splitting on newlines. Elements of the list may also be NBT compounds instead of strings.

  • color (COLOR, optional) – the base color of the text on the sign, can be overwritten on a per-line basis by providing NBT({“text”:”…”,”color”:”red”}) NBT compounds, defaults to “black”

  • glowing (bool, optional) – whether or not the text should use the glowing effect, defaults to False

  • direction (CARDINAL | int, optional) – the cardinal direction the sign should be facing, for non-wall signs can also be an integer between 0-15, defaults to “south”

  • sign_block (str | Block, optional) – the block type/id of the sign that should be placed, should end with “_sign”, defaults to “oak_sign”

copyBlockCube(pos1: Vec3, pos2: Vec3, withData: bool = False) list[list[list[Block]]]

Get all block types in a cube between pos1 and pos2 inclusive. Should be used in conjunction with pasteBlockCube().

Parameters:
  • pos1 (Vec3) – the position of one corner of the cube

  • pos2 (Vec3) – the position of the opposite corner of the cube

  • withData (bool, optional) – whether block component data should be queried, defaults to False

Returns:

the block types in the cube given as rows of x with columns of y with slices of depth z respectively

Return type:

list[list[list[Block]]]

pasteBlockCube(blocktypes: list[list[list[str | Block]]], pos: Vec3, rotation: Literal['east', 'south', 'west', 'north', 'up', 'down'] = 'east', flip_x: bool = False, flip_y: bool = False, flip_z: bool = False) None

Paste the block types in the cube blocktypes into the world at position pos where pos is the negative most corner of the cube along all three axes. Additional options can be used to change the rotation of blocks in the copied cube, however, no matter in which way the cube is rotated and/or flipped, pos will also be the most negative corner. Should be used in conjunction with copyBlockCube().

start = Vec3(0, 0, 0)
end = start.addX(5).addY(5).addZ(5)
# copy a 6x6x6 block cube from origin in world
blocks = world.copyBlockCube(start, end)
# replace that space with the same blocks but rotated by 90 degrees
world.pasteBlockCube(blocks, start, "south")
# copy same original block at different point above origin
world.pasteBlockCube(blocks, start.up(200))
Parameters:
  • blocktypes (list[list[list[str | Block]]]) – the cube of block types/ids that should be pasted, given as rows of x with columns of y with slices of depth z respectively

  • pos (Vec3) – the most negative corner along all three axes of the cube where the cube should be pasted

  • rotation (DIRECTION, optional) – the direction of the x axis of the cube to be pasted (“east” means copied x axis aligns with real x axis, i.e., the original orientation), defaults to “east”

  • flip_x (bool, optional) – flip pasted blocks along x axis, defaults to False

  • flip_y (bool, optional) – flip pasted blocks along y axis, defaults to False

  • flip_z (bool, optional) – flip pasted blocks along z axis, defaults to False

spawnEntity(type: str | Block, pos: Vec3) Entity

Spawn and return a new entitiy of given type at position pos in world. The entity has default settings and behavior.

Parameters:
  • type (str | EntityType) – the valid entity type that should be spawned (must be spawnable without additional parameters)

  • pos (Vec3) – the position where to spawn the entitiy in the world

Returns:

the Entity entity spawned

Return type:

entity.Entity

spawnItems(type: str | Block, pos: Vec3, amount: int = 1) None

Spawn amount many collectable items of type at pos.

Parameters:
  • type (str | Block) – the item type as string or Block, e.g., "minecraft:arrow" with potential component data

  • pos (Vec3) – position where to spawn the items

  • amount (int, optional) – number of items to spawn, defaults to 1

getEntities(type: str | Block | None = None, only_spawnable: bool = True) list[Entity]

Get all (loaded) entities in the world. If a type is provided, only entities of that type are returned. By default only entities of types that could be spawned using spawnEntity() are returned. To get all entities (except players) set only_spawnable=False, which will also return non-spawnable entities such as e.g. "falling_block" or "dropped_item".

Parameters:
  • type (str | EntityType | None, optional) – if provided returns only entities of that type, returns all types if None, defaults to None

  • only_spawnable (bool, optional) – if False, will also return non-spawnable entities, defaults to True

Returns:

list of (loaded and filtered) entities in the world

Return type:

list[entity.Entity]

getEntitiesAround(pos: Vec3, distance: float, type: str | Block | None = None, only_spawnable: bool = True) list[Entity]

Equivalent to getEntities(), however, is filtered to only return entities within distance around pos. Is more efficient that filtering the list manually.

Parameters:
  • pos (Vec3) – position around which the entities are returned

  • distance (float) – the maximum distance entities returned have around pos

  • type (str | EntityType | None, optional) – if provided returns only entities of that type, returns all types if None, defaults to None

  • only_spawnable (bool, optional) – if False, will also return non-spawnable entities, defaults to True

Returns:

list of (loaded and filtered) entities in the world closer than distance to pos

Return type:

list[entity.Entity]

removeEntities(type: str | Block | None = None) None

Remove all entities (except players) from the world, they do not drop anything. If type is provided remove only entities of that type.

Parameters:

type (str | EntityType | None, optional) – if provided removes only entities of that type, otherwise remove all entities, defaults to None

runCommandBlocking(command: str) str

Run the command as if it was typed in chat as /-command and executed in this specific world/dimension. Blocks and waits for the command to finish executing returning the command’s result.

response = world.runCommandBlocking("locate biome mushroom_fields")

Caution

The plugin that is built against the spigot-Bukkit API does not fully support the return of command output, specifically the capturing of output of vanilla commands. Instead it only supports the capturing of Bukkit commands, which can be seen with mc.runCommandBlocking("help Bukkit").split("\n")

Parameters:

command (str) – the command without the slash /

Returns:

the console output of the command

Return type:

str