Meeting 2020-02-11

Editing Blocks

We now know how to get and set the player's location in the Minecraft world using Python. Another useful ability for our Python environment is to see and change the block material at any location in the Minecraft world. This week we will learn how to change block types.

Changing the Block Type

Once we have given ourselves access to the Minecraft programming code library and world with:

from mcpi.minecraft import Minecraft

mc = Minecraft.create()

From last week, we know how to save the current XYZ location of the player into a variable:

playerPos = mc.player.getTilePos()

playerX = playerPos.x

playerY = playerPos.y

playerZ = playerPos.z

And we know how to change the player location (teleport):

mc.player.setTilePos(playerX, playerY, playerZ)

We can change the type of block in the Minecraft world using the setBlock command like this:

mc.setBlock(xPos, yPos, zPos, blockType)

where xPos,yPos,zPos are integer coordinates of the block to change, and blockType is an integer value or variable chosen from this list of block IDs. For example, the block ID for dirt is 3 and lava is 11.

So we can put all three operations together in our next project.

Project: Zombie Tower

Sometimes in Minecraft (especially early in the game) things get too intense and you just need to build a tower and hang out on top of it, out of reach of zombies, until daylight. This week's project is to write a Python program that builds a tower of blocks at the player's current location, and teleports the player on top of it.

Save the program to %appdata%/.minecraft/mcpipy/zombietower.py (or pick your own name) and in Minecraft enter "/py zombietower" to run it.

If you need help, you can ask me, or download my version of the program from the bottom of this web page.

Programming Tip: String Formatting

The first program we wrote sent a "Hello World!" text string message to the Minecraft console. It would be useful to be able to put information about the game (such as player location or block type) into our console messages. But to do this, we have to convert all our numeric coordinates and information into a single string that we can send to the mc.postToChat function. This requires two different Python operations:

String concatenation means putting multiple strings together one after the other into a single string. In Python you can concatenate strings with the "+" operator (which does addition on numbers):

>>> 'Hello' + 'World'

'HelloWorld'

The + operator joins the two strings into a single string. No extra spaces or other characters are added in between.

The next thing we need to know is how to convert a number to a string. This is done with the str( ) function:

>>> 1234

1234

>>> str(1234)

'1234'

>>> '1234' + 5678

Traceback (most recent call last):

File "<pyshell#7>", line 1, in <module>

'1234' + 5678

TypeError: can only concatenate str (not "int") to str

>>> '1234' + str( 5678 )

'12345678'

If you try and combine a string and a number using string concatenation, you will get an error - you have to convert the number to a string first, then concatenate.

Project: Print current location to console

The next project is to write a short Python program that prints our current location in the Minecraft world to the console when we run it. Have the Python program send a message like "Your location: X=123 Y=64 Z=-456". You will need to build up the message using "+" string concatenation operators and str() to convert the integer values returned by mc.player.getTilePos() to strings.