Kirsle.net logo Kirsle.net

Scripting in Minecraft 1.7

September 23, 2013 by Noah

One of the big features of the Raspberry Pi edition of Minecraft was that they provided a scripting API, so that you could write Python code to manipulate the blocks in the MineCraft world.

The scripting API hasn't officially come to the desktop version of Minecraft, but version 1.7 includes a handful of fun new server commands, like /setblock, /summon, and an extended interface for /give, so you can now summon any type of entity you want, including Ender Dragons, or summoning a Wither Skeleton riding on top of a Skeleton Horse while wearing fully enchanted diamond armor and holding a diamond sword.

But this post is going to focus on the /setblock command, which lets you change a block somewhere in your map by giving the coordinates to it and setting what type of block you want to put there.

When you're running a Minecraft server, you can enter server commands either in-game (if you're the operator), or directly into the terminal that's running the server. Using the latter, you can have a third party program (a Python script for example) write stuff into the terminal running the server, to automatically run server commands!

I made a quick proof-of-concept Python script to demonstrate this. This script will create a cube of stone of any dimensions you want somewhere in your Minecraft 1.7 server.

#!/usr/bin/python

# makecube.py - Proof of concept for scripting Minecraft 1.7
#
# Usage: makecube.py <screen_name> <size> <x> <y> <z>
#
# Works for Unix-like OS's only and depends on the `screen` command.
# Setup:
# [bash]$ screen -S minecraft
# [screen]$ java -jar minecraft_server.jar
# (in another terminal)
# [bash]$ makecube.py minecraft 25 0 60 0

from sys import argv
import os
            
material = "stone"
if len(argv) != 6:
    print "Usage: makecube.py <screen_name> <size> <x> <y> <z>"
    quit()

screen = argv[1]
size, x, y, z = map(lambda arg: int(arg), argv[2:])

def setblock(x, y, z):
    cmd = "screen -x {0} -X stuff '/setblock {1} {2} {3} stone 0 replace\x0D'"\
          .format(screen, x, y, z)
    os.system(cmd)

# Generate the cube's coords.
coords = list()
x1 = int(x - size/2)
y1 = int(y - size/2)
z1 = int(z - size/2)
for x2 in range(x1, x1 + size):
    for y2 in range(y1, y1 + size):
        for z2 in range(z1, z1 + size+1):
            coords.append([ x2, y2, z2 ])

# Send all the /setblock commands.
for coord in coords:
    setblock(*coord)

Tags:

Comments

There are 0 comments on this page. Add yours.

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.