There are a wide variety of ways to interact with Minecraft programatically, including an official Javascript "add-on" API to the "bedrock" version of Minecraft, APIs for Minecraft servers, unofficial hacks on the client side to the Java version of Minecraft, and other specialized offerings. They are briefly introduced at Minecraft modding (Wikipedia) and covered in more detail at the Official Minecraft Wiki. (I'd love to see a good comparison of how comprehensive each approach is, and how the APIs compare....)
Here I'll cover just one approach, which explains how to connect with Python to a "Spigot" Minecraft server and manipulate the world and players. It supports a Python 3 API rooted in the Minecraft: Pi Edition for Raspberry Pi described at <Stuff about="code" />: Minecraft.
The full explanation, with videos for a Windows 10 environment, is at Learn Python With Minecraft — Part 1: Setting Up The Environment | by Carlos Argueta | Medium. Here is a quick summary of the steps, as I did them in Debian Linux "Buster" in the standard Crostini virtual environment on a Chromebook (Pixelbook Go):
Download Minecraft (Java version)
If necessary, download and install Python, e.g. version 3.8
Download Carlos' Minecraft server, which seems to be based on Spigot and CraftBukkit, from https://sourceforge.net/projects/learn-python-minecraft-win/files/latest/download
Unzip Carlos' server
Run python -m pip install minecraftPythonAPI.zip
Start the server up via java -Xmx1024M -Xms1024M -jar spigot.jar
(Carlos provides a .bat
file for that for Windows). When you run spigot for the first time, it automatically creates a new Minecraft world for you
Notice which version of Spigot you're running. It is listed towards the top of the output of the java command, and is currently 1.14.4. More specifically in my case: [Server thread/INFO]: This server is running CraftBukkit version git-Spigot-ea7e48b-368f4e9 (MC: 1.14.4) (Implementing API version 1.14.4-R0.1-SNAPSHOT)
You need to launch the same version of Java Minecraft as you're running on the server. That requires using the Minecraft launcher, and downloading a different version (currently 1.14.4) as shown in his video number 5.
Run the matching version in multiplayer, and add your server, by specifying a network address as "localhost".
Note that Minecraft defaults to creative mode
Run some Python code like this, which just shows where the player is, and then moves the player:
from mcpi.minecraft import Minecraft
mc = Minecraft.create()
PlayerTile = mc.player.getTilePos()
print(PlayerTile)
mc.player.setTilePos(0, 140, 0)
See also an early demo of the Pi interface but note you have to change two lines to import via mcpi
rather than minecraft
, e.g.
import mcpi.minecraft as minecraft
.
More code snippets are at the github repo Learn-Python-With-Minecraft
A completely different option out there might also be of interest. It allows Python programming for Minecraft version 1.12.2 from 2017, and is supported with these instructions, which covers installing the older version, and handles Python 2.7 or 3: Python Coding for Minecraft : 18 Steps (with Pictures) - Instructables. There are recent comments from the author so it seems to still be relevant.