I published a YouTube video today showing how you can use Python’s mcpi library to control Minecraft Java Edition under macOS.

For those who don’t know, the mcpi library was written to allow Raspberry Pi users to control the special version of Minecraft that used to ship with the Raspberry Pi.

Without getting into too much history, the Raspberry Pi edition of Minecraft is no longer actively maintained, and Microsoft’s acquisition of Mojang (the company that created Minecraft) means that this is unlikely to change.

You can learn more about all that here.

Luckily, by running your own Minecraft server (with some plugins), you can now get mcpi to talk to Java Edition on macOS or a PC (probably Linux as well). The YouTube video (link above) shows you how!

This simple Python program will allow you to test things out (it connects to a local Minecraft server and polls the player’s position, constantly placing stone blocks under his or her feet):

from mcpi.minecraft import Minecraft
from time import sleep

mc = Minecraft.create(address="localhost", port=4711)

# Constantly grab the player's position and create
# a new stone block underneath him/her
while True:
    x,y,z = mc.player.getPos()

    # Debug
    print("x: {}, y: {}, z: {}".format(x,y,z))
    
    mc.setBlock(x,y-1,z,1)
    sleep

How do you get all this working? You can go watch the YouTube video, or read through the instructions below:

  1. Make sure your Mac has an up-to-date Java environment installed (run java --version from the Terminal, to check). I’m running version 18:
$ java --version

java version "18" 2022-03-22
Java(TM) SE Runtime Environment (build 18+36-2087)
Java HotSpot(TM) 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)
  1. Install Minecraft Java Edition
  2. Install Python 3
  3. Use pip to install the “mcpi” library, with:
python3 -m pip install mcpi
  1. Download a copy of Minecraft server: https://www.minecraft.net/en-us/download/server

  2. Download a copy of Spigot: https://getbukkit.org/download/spigot

  3. Download a copy of RaspberryJuice: https://dev.bukkit.org/projects/raspberryjuice

  4. Make a new folder (“minecraft-server”) and move the .jar files downloaded in steps 5, 6, and 7 into that folder

  5. Create a text file in your “minecraft-server” directory called eula.txt, which contains the text “eula=true”. You can do this from the macOS Terminal with:

echo "eula=true" > eula.txt
  1. Run your minecraft server once, by executing the following command from inside the “minecraft-server” directory:
java -jar spigot-1.18.2.jar

(if you are using a different version of spigot, your .jar file may be named different)

  1. Shut down spigot, and then move the RaspberryJuice .jar file (in my case raspberryjuice-1.11.jar) into the plugins directory that spigot has created

  2. Re-run spigot, connect to your server from the “Multiplayer” menu in Minecraft and try executing a Python program (like the one above) while you’re logged in. You should see stone blocks appearing under your feet.

That’s it!