Controlling Minecraft: Pi Edition

The GPIO pins on the Raspberry Pi are one of the reasons for its popularity. In this guide we've used the GPIO to connect to capacitive touch sensors. This has a range of applications and allows you to create some really creative projects.

The Raspberry Pi has a special version of Minecraft that can be programmed with Python. Combining Minecraft on the Raspberry Pi with our capacitive touch sensors allows us to create programs with limitless potential for creativity and fun.

Install Minecraft: Pi Edition

Before we begin you'll need to have Minecraft: Pi Edition installed on your Raspberry Pi. To install Minecraft Pi you can use this guide on the Adafruit Learning System.

Copy the API

In order to connect to a Minecraft game you will need to copy the API.

Open a terminal and create a new directory named Minecraft within the Documents directory with the following command:
mkdir ~/Documents/Minecraft
Next, we will copy the API into this folder with the following command:
cp -r ~/mcpi/api/python/ ~/Documents/Minecraft

Super Jump

This code uses the capacitive touch pad with Minecraft: Pi Edition to make the player jump 10 blocks into the air when the pad is pressed.

This code works best with the momentary board, though can be used with the toggle board and can be adapted for the 5-pad board.
import RPi.GPIO as GPIO
import time
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

GPIO.setmode(GPIO.BCM)

padPin = 23

GPIO.setup(padPin, GPIO.IN)

alreadyPressed = False

while True:
    padPressed = GPIO.input(padPin)

    if padPressed and not alreadyPressed:
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y + 10
        z = pos.z
        mc.player.setPos(x, y, z)
    alreadyPressed = padPressed
    time.sleep(0.1)

Melons

This code will place a melon block at the current position of the player. If you hold down the touch pad of the capacitive touch sensor or use toggle sensor, a trail of melons will be created wherever the player walks. This example works particularly well with the toggle sensor board.
import RPi.GPIO as GPIO
import time
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

GPIO.setmode(GPIO.BCM)

padPin = 23

GPIO.setup(padPin, GPIO.IN)

# melon block
blockType = 103

while True:
    padPressed = GPIO.input(padPin)

    if padPressed:
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, blockType)

    time.sleep(0.1)

Control Panel (5-Pad Board)

In the last example we use the 5-pad capacitive touch sensor board to do 5 different things in Minecraft Pi.

This is what each pad does:
0 - Teleports the player to co-ordinates (0, 0, 0)
1 - Places a flower at the player's position
2 - Places explosive TNT at the player's position
3 - Checks if the player is in water
4 - Toggles whether or not blocks can be smashed
import RPi.GPIO as GPIO
import time
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

GPIO.setmode(GPIO.BCM)

#set the GPIO input pins
pad0 = 22
pad1 = 27
pad2 = 17
pad3 = 24
pad4 = 23

GPIO.setup(pad0, GPIO.IN)
GPIO.setup(pad1, GPIO.IN)
GPIO.setup(pad2, GPIO.IN)
GPIO.setup(pad3, GPIO.IN)
GPIO.setup(pad4, GPIO.IN)

pad0alreadyPressed = False
pad3alreadyPressed = False
pad4alreadyPressed = False

immutable = False
tnt = 46
water = 9
flowers = 38

while True:
    pad0pressed = not GPIO.input(pad0)
    pad1pressed = not GPIO.input(pad1)
    pad2pressed = not GPIO.input(pad2)
    pad3pressed = not GPIO.input(pad3)
    pad4pressed = not GPIO.input(pad4)

    if pad0pressed and not pad0alreadyPressed:
        #teleport
        x = 0
        y = 0
        z = 0
        mc.player.setPos(x, y, z)
    pad0alreadyPressed = pad0pressed

    if pad1pressed:
        #Flowers
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, flowers)

    if pad2pressed:
        #TNT
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, tnt, 1)

    if pad3pressed and not pad3alreadyPressed:
        #Chat message: Are in water?
        pos = mc.player.getPos()
        block = mc.getBlock(pos.x, pos.y, pos.z)
        inWater = block == water
        mc.postToChat("In water: " + str(inWater))
    pad3alreadyPressed = pad3pressed

    if pad4pressed and not pad4alreadyPressed:
        #Immutable
        immutable = not immutable
        mc.setting("world_immutable", immutable)
        mc.postToChat("Immutable: " + str(immutable))
    pad4alreadyPressed = pad4pressed

    time.sleep(0.1)

Running the Code

To run any of the examples on this page you will need to be in Minecraft game world on your Raspberry Pi. You will get an error if you don't have Minecraft open or if you're on the title screen.

All of these examples use the same wiring as the rest of this guide.

The code runs like any other Python program that uses the GPIO. You can run it from the terminal on your Raspberry Pi with the following command (change the name of the program):
sudo python superJump.py
This guide was first published on Apr 07, 2014. It was last updated on Sep 19, 2018. This page (Controlling Minecraft: Pi Edition) was last updated on May 04, 2015.