arghbox

Tactical Computer Action (for beginners and their support team)

Tag: Blocks

Minecraft Pi API: Getting Blocks

2013-08-18 12.13.30
Finding the type of block at a specific location is very useful. With this information you can do a variety of things such as checking to see if the player is flying, finding out whether a door is open or seeing if the player has placed a melon on an alter as an offering to the melon god.

Like the methods that set blocks, the methods that find out what type a block is use co-ordinates to determine which block you are interested in. Each method returns a value or an object for that block. There are three methods that we will look at: getBlock(), getBlockWithData() and getBlocks().

getBlock()

When you want to find the type of any block in the game, the getBlock() method is the one you need to use. It is pretty simple, you provide co-ordinates as arguments and it returns the block type as an integer value.

getBlock(x, y, z)

The following example gets the block type at co-ordinates (12, 0, 16):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 12
y = 0
z = 16
blockType = getBlock(x, y, z)

getBlockWithData()

Each block in the game has a number of different states, 16 in total. This allows a single block ID to have a number of different variations. For example the wool block has 16 different states, each one representing a different colour. The TNT block can be smashed like a regular block in state 0 and is explosive in state 1. To see how to set the states check out the section on optional arguments for setBlock() and setBlocks().

Finding out the state of a block with the getBlockWithData() method is relatively simple, yet a tiny bit more complex than the getBlock() method. The getBlockWithData() method takes co-ordinates as arguments in order to determine which block you are interested in. The method returns an object, which contains two attributes, id and data. The id and data attributes store the block type and its state respectively.

getBlockWithData(x, y, z)

The following example finds out the block type and state of the block at co-ordinates (12, 0 ,16):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 12
y = 0
z = 16
block = getBlockWithData(x, y, z)

blockType = block.id
blockState = block.data

getBlocks()

The getBlocks() method is supposed to return the values of all the blocks within a cuboid that is defined between two co-ordinates. At the moment the getBlocks() method does not work. I have written a function that achieves the same thing, however it is quite slow and may not be interchangeable with the getBlocks() method if it is ever corrected.

Below is my alternative function. It takes two sets of co-ordinates and returns a 3-dimensional list where x is the first list, y the first nested list and z the second nested list:

def getBlocks(x1, y1, z1, x2, y2, z2):
    xhigh = max(x1, x2)
    xlow = min(x1, x2)
    yhigh = max(y1, y2)
    ylow = min(y1, y2)
    zhigh = max(z1, z2)
    zlow = min(z1, z2)

    blocks = []
    for x in range(xhigh - xlow + 1):
        blocks.append([])
        for y in range(yhigh - ylow + 1):
            blocks[x].append([])
            for z in range(zhigh - zlow + 1):
                blocks[x][y].append([])
                block = mc.getBlock(xlow + x, ylow + y, zlow + z)
                blocks[x][y][z] = block
    return blocks

Minecraft Pi API: Setting Blocks

blocks

Controlling blocks is one of the most powerful things you can do with the Minecraft Pi API. You can access blocks that are not usually available to the player or build complex structures with a few lines of code instead of placing each block by hand. In this post we will cover the API methods that allow you to place blocks using you Python programs. If you need to set up Minecraft Pi check out my post here.

All block methods in Minecraft Pi use co-ordinates to determine the location of blocks. In the top left of the game window you can see the current co-ordinates of the player. Three variables represent the coordinates x, y and z; x and z represent the horizontal plane and y represents vertical height. This is shown in the following diagram:

3D

All block methods use block IDs that identify the type of block you want to place at a location or already exists at a location. The block ID is an integer value and represents the blocks like soil, air, melon, lava and so on. You can find the IDs of the blocks on this cheat sheet, however not every block exists in Minecraft Pi Edition and most don’t function (for example you can’t use the chest to store things).

setBlock()

When building anything in Minecraft the setBlock() is the most method you’ll be using. It takes four arguments – x, y and z co-ordinates and a block ID – and places a block at that location. The basic syntax for this method is:

setBlock(x, y, z, blockID)

The following example creates a melon block (block ID 103) at coordinates (10, 11, 12):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 10
y = 11
z = 12
blockID = 103
mc.setBlock(x, y, z, blockID)

setBlocks()

Placing blocks with setBlock() is powerful, yet can be inefficient when you want to cover large areas. When we want to place blocks across a large area we use the setBlocks() method (yes, that is a different method to setBlock(), it has an “s” at the end). The setBlocks() method places a single block type in a cuboid shape between two sets of co-ordinates.

cuboid2

The setBlocks() method takes seven arguments: two sets of co-ordinates and a block ID. The basic syntax of setBlocks() is:


setBlocks(x1, y1, z1, x2, y2, z2, blockID)

For example the following code will place a cuboid of melon blocks (block ID 103) between coordinates (6, 5, 18) and (12, 10, 32):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x1 = 6
y1 = 5
z1 = 18
x2 = 12
y2 = 10
z2 = 32
blockID = 103
mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID)

Optional Arguments for setBlock() and setBlocks()

Both the setBlock() and setBlocks() methods can take an optional extra argument. This argument can be described as the block state. For example the wool block (block id 35) has 16 different states for 16 different colours. The red wool block for example has a state of 14. The TNT block is explosive in state 1. To use thisĀ  we just place the extra argument at the end of the arguments in both setBlock() and setBlocks():

mc.setBlock(x, y, z, blockID, blockState)
mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID, blockState)

The following code uses the optional argument to create an explosive TNT block (block ID 46 and state 1):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 10
y = 11
z = 12
blockID = 46
blockState = 1
mc.setBlock(x, y, z, blockID, blockState)

The next example uses the optional argument with setBlocks() to create a cuboid of red wool (block ID 35 state 14):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x1 = 6
y1 = 5
z1 = 18
x2 = 12
y2 = 10
z2 = 32
blockID = 35
blockState = 14
mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID, blockState)

Have fun using these methods, they will save you loads of time when you’re building in Minecraft Pi. If you want to practice these methods while creating some useful programs check out my free book.