Minecraft pi hacking

Following on from my previous post,  I noticed that minecraft now runs far better with the new GUI on the pi.  So I decided to have another go at coding in python with minecraft.

There is a really good beginner tutorial on this at the site below


So I am using this in the first instance.


I tried to code on the Pi at the same time as running minecraft,  as I only have a small 15″ monitor this was proving rather tedious.   As I have ssh installed it is easy to simply ssh in and edit code on the Pi within a ssh session.

#!/usr/bin/env python
import sys
from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("hello, world")
x, y, z = mc.player.getPos()
tnt = 46
mc.setBlock(x+5, y, z,  tnt, 1)

I have modified the code a little,  the sys.exit() is not really needed it just allows the code to exit to the shell if run directly from the shell rather than a terminal window.


When run the above code posts a hello, world message to the chat box and also places a tnt block where you are standing,  the tnt, 1 indicates the data attributes have been changed so if you hit the tnt block with a sword, it activates and explodes.

However this is not exactly useful

mc.setBlock(x+5, y, z,  tnt, 1)

If we modify the code we can place the tnt block in front of your character.

I have modified the example code from the above site to place tnt blocks when ever you walk over grass.

#!/usr/bin/env python
import sys
from mcpi import minecraft
from time import sleep
#ntn block 2
mc = minecraft.Minecraft.create()
pos = mc.player.getPos()
grass = 2
tnt = 46
while True:
    x, y, z = mc.player.getPos()  # player position (x, y, z)
    block_beneath = mc.getBlock(x, y-1, z)  # block ID
    if block_beneath == grass:
        mc.setBlock(x, y, z, tnt, 1)

So if you walk around you leave a trail of TNT behind you,   standing on something else such as stone stops this, so if you get to another area,   turn round hit the tnt  to activate and run away, and sit back and wach all the blocks of TNT explode.

Something interesting with all this

if you run code that places a modified tnt block,  then it stays modified, if you place the same block but normally within the game it has the same attributes as it would normally, HOWEVER it will be set off by other blocks exploding.  I am sure with the right code you could make a minesweeper type game within minecraft,  that places both normal and modified blocks in the game.

Once I had spent a while doing this it was getting late.  I am at the Paignton Pi jam tomorrow so may be able to demonstrate this.  Depends who turns up.

I will keep coding with this, see what else I can come up with.