Posted on Leave a comment

Scripting Minecraft with Python under Mac OSX

My kids really love Minecraft. They begged me for the game until I relented. Then they played it whenever they could.

I just didn’t get it. It kind of looked a bit crap. Even adults seemed to be completely enamoured by it, which I just didn’t get.

Then, one Good Friday we were sitting around the kitchen table playing games and I picked up my son’s iPad and gave it a try.

Damn. Okay. I can see where the interest is. I was digging underground tunnels, and even in the fairly limited mobile version we had a lot of fun playing together.

Fast forward a little bit and I ended up running up a server for the kids to play on because I didn’t rally like the idea of them out there joining random servers with strangers, and also, TNT. The boy likes to blow things up.

So I set them up with a server on my little Mac mini server at home, and they’d invite their friends on and everything was peachy.

And then one day I was on playing with them and was diligently mining out some tunnels and thought, “Damn this is tiresome, surely there’s a better way to dig these mile long tunnels?”

Turns out there is!

Small disclaimer. Minecraft is so unbelievably hackable  I’m sure there are MANY ways to script, modify and extend it. I’m also sure there’s better code than mine. But it works for me, and might be useful for others too. No hate, bro.

Running the Server

Okay. So this will definitely work under pretty much any version of Mac OSX, and probably also Linux. I don’t know about Windows as I’m using the screen utility to make it possible. There might be a way but I don’t know.

Using this method we’ll be able to add blocks through python code from the server end without anything on the client end. You can log in and watch the blocks as they’re placed, which is kind of mesmerising and cool 🙂

Now then, we need to run the Minecraft application on the server in a screen instance. This will allow us to pass commands into the screen session from our Python script.

Here’s something similar to my Minecraft startup script. It changes into the Minecraft directory and runs screen with a session name of “minecraft” which then runs your typical Minecraft command line to invoke java and load the application.

Create a text file called something like mcstart.command and launch Minecraft similarly to below. You’ll need to substitute for your Minecraft directory – and be careful of the line wrapping (there’s only 3 lines).

#!/bin/bash
cd ~/mcserver
screen -S minecraft java -Xmx1024M -Xms1024M -jar minecraft_server.1.10.jar nogui

You might need to make the file executable with:

chmod +x mcstart.command

 

Cool! So now launch Minecraft from the script which will run inside a screen session called “minecraft”. In OSX you should be able to double click mcstart.command to make it run too. I’ll leave it up to you to add it to your startup items so it launches every time your computer restarts.

Now on the server end also you can create a python script to send commands into the screen session.

Try something like this. Create a file called mcscript.py and edit it. Put something like this in:

#!/usr/bin/python

# Setup our co-ordinates
Start_X = 10
Start_Y = 70
Start_Z = 20

# Define the block to use
Our_Block = "double_stone_slab 4"

cmd = "screen -x minecraft -X stuff '/setblock " + str(Start_X) + " " + str(Start_Y) + " " + str(Start_Z) + " " + Our_Block + " replace\x0D'"

os.system(cmd)

Run the script from the command line by using:

./mcscript.py

You may also need to make it executable.

In the above code we define some co-ordinates. The X co-ordinate in Minecraft increases to the east, Z increases to the north, and Y is the vertical co-ordinate. Note that Y values less than about 65 is probably underground, and cannot go above 255.

The variable Our_Block in the code defines the block and variation. If you search online for something like “Minecraft ID List” you should find plenty of references to the various blocks. Things can be named strangely. Powered rails for example are “Golden_Rail” or something.

Obviously you can use loops to output more than one block. On my modest Mac mini scripts can place about 30-50 blocks per second.

  • Note also that you don’t seem to be able to place blocks that are far away in the world from an active player. An error along the lines of “can’t place block outside of the world” is shown in the screen session.
  • Remember that you can see your current co-ordinate by pressing F3. You can also get close to a particular block and mouse over it to see the co-ordinates while the F3 info is shown
  • The Y co-ordinate seems to be where your head is. For the block under your feet subtract 2.
  • Existing blocks can be replaced with “Air 0” too!

 

I hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *