Minecraft Python Programming Guide
Erscheinungsbild
Setup
Server
Docker Compose Setup
I use docker with the itzg/minecraft-server Docker image.
services:
minecraft:
image: itzg/minecraft-server
ports:
- "25565:25565" # Minecraft Java Port
- "19132:19132/udp" # Bedrock/Geyser Port
- "25575:25575" # RCON port
- "4711:4711"
environment:
EULA: "TRUE"
TZ: "Europe/Berlin"
TYPE: PAPER
VERSION: "1.21.8"
MEMORY: "1G"
SPAWN_PROTECTION: 0
ENABLE_RCON: "true"
RCON_PASSWORD: "somepassword"
RCON_PORT: 25575 # default Minecraft RCON port, can be changed
volumes:
- ./data:/data
- ./plugins:/plugins
memswap_limit: "2G"
deploy:
resources:
limits:
memory: "2G"
cpus: "2"
reservations:
memory: "2G"
cpus: "0.75"
Open Firewall Ports
Assure that the Raspberry Juice port on the Minecraft server is opened up on your firewalls for incoming traffic.
Setup Raspberry Juice Plugin
- Download plugin from https://www.spigotmc.org/resources/raspberryjuice.22724/
- Copy it into ./plugins directory on Docker host.
Setup Development Workstation
Install the Raspberry Juice Python module. Better in a venv, but to keep it easy globally like below:
pip install mcpi
Now write some python script that connects to your remote minecraft server and execute it:
from mcpi.minecraft import Minecraft
from mcpi import block
# connect to the server
mc = Minecraft.create(address="192.168.2.151", port=4799)
# get all players
player_ids = mc.getPlayerEntityIds()
players = {}
print("Connected players:")
for pid in player_ids:
name = mc.entity.getName(pid)
players[pid] = name
print(f" {pid}: {name}")
# ask which player to give blocks
choice = int(input("Enter the ID of the player to receive 10 diamond blocks: "))
if choice not in players:
print("Invalid player ID!")
else:
# get player position
pos = mc.entity.getPos(choice)
# place 10 diamond blocks in a line in front of the player
for i in range(10):
mc.setBlock(pos.x + i + 1, pos.y, pos.z, block.DIAMOND_BLOCK)
print(f"Placed 10 diamond blocks in front of {players[choice]}!")