2013-05-09

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit servers. Next, I found this handy guide to the Minecraft Pi Protocol. Finally, I found a Python library called mcpipy that implements the Minecraft Pi Protocol in Python.

Between looking at the Minecraft Pi Protocol documentation and peeking at the mcpipy source code, it's pretty easy to figure out how to control Minecraft from Python. My next step was to figure out how to draw something cool. Naturally, the first thing to come to mind was fractals, but it's not so easy to draw most fractals with fixed sized blocks. However, Sierpinski's triangle is not so hard to draw using blocks, so that's what I did.

Here's the psuedocode for drawing Sierpinski's triangle:

draw_the_three_vertices()
current_point =
random_point_in_triangle()
while True:
vertex = random_vertex()
current_point = midpoint_between(
current_point,
vertex)
draw(current_point)

Here's the actual source code:

The Gory Details
If you're interested, here are all the gory details.

I got all of this running on OS X 10.7.

First, you'll need Java to run Bukkit. I installed Java SE Development Kit 7u21.

Next, you need to install Bukkit. Note, you must use a version of Bukkit that is compatible with your version of the Minecraft client. For me, that meant installing the latest, development version of Bukkit. See here for more information:

To start the server, execute the start.command file. To stop the server, type "stop" into the console.

Next, I installed RasberryJuice. Download the jar file and move it to ~/Local/bukkit_server/plugins. Then type "reload" in the console where Bukkit is running.

Next, you'll need to install Minecraft itself. That costs roughly $30. I put it in ~/Local. When I first ran it, it insisted on installing "Java 6". To connect to your local server, choose Multiplayer, then Direct Connect, and then enter "localhost".

To install mcpipy, you can either use Git (if you have it installed), or you can download and uncompress the zip file. I put it in ~/Local/mcpipy. To test that it's working, try:

This will draw a rainbow in your running copy of Minecraft. After running the command, assuming there were no errors, it takes a little while for the rainbow to show up.

To try out my code, create a file called ~/Local/mcpipy/jjinux_sierpinski_triangle.py with the code from above. Then run:

It'll print out messages as it draws all the blocks. This will draw Sierpinski's triangle in the sky. It takes a minute or two to show up. You can tweak the script to draw the triangle using more blocks or with a different type of block. However, I've found that making certain changes causes Bukkit to crash, so keep an eye on the console where you started Bukkit to make sure nothing bad happened. If anything bad does happen, you might want to use control-c to stop the server. Then you can delete the world, world_nether, and world_the_end directories in ~/Local/bukkit_server and restart the server.

Show more