Operate Minecraft Pi Edition from your PC

Minecraft Pi Edition is installed on Raspbian, the OS of Raspberry Pi. Minecraft Pi Edition has an API that can be operated from the outside, and you can add blocks to the world of Minecraft using Python programs.

This time, I will introduce how to operate Minecraft Pi Edition from a PC.

Software used

Raspberry Pi side

PC side

Mechanism to operate remotely

Minecraft Pi Edition exposes the API on port 4711. In Raspbian, the Python library for clients (hereafter mcpi) is located in / usr / lib / python2.7 / dist-packages / mcpi /, and you can write and operate Python programs.

Raspberry Pi side


$ python 
>>> import mcpi
>>> print mcpi.__file__
/usr/lib/python2.7/dist-packages/mcpi/__init__.pyc

The implementation of the minecraft.create method is as follows, and you can operate Minecraft Pi Edition from a PC on the same local network by entering the IP address of Raspberry Pi in the first argument.

mcpi/minecraft.py


    def create(address = "localhost", port = 4711):
        return Minecraft(Connection(address, port))

mcpi can be ported to other than Python if socket communication can be implemented, so it seems that some people are rewriting it with Ruby. (eaglestone / minecraft-pi-ruby)

Development environment preparation

First, copy mcpi on the Raspberry Pi to the PC side. (Change the IP address part of [email protected] by executing ```ifconfig`` etc. on Raspberry Pi.)

PC side


mkdir minecraft-pi-samples
cd minecraft-pi-samples
rsync -av --copy-links [email protected]:/usr/lib/python2.7/dist-packages/mcpi/ ./mcpi/ --exclude=*.pyc

If you don't understand this work, or if you have trouble, download the sample program here and unzip it. Please use the `src`` directory.

Run the program

Create the following program. Replace the first argument "192.168.1.49" of Minecraft.create with the IP address of your Raspberry Pi.

hello.py


import platform
from mcpi.minecraft import Minecraft
mc = Minecraft.create("192.168.1.49")
mc.postToChat("Hello " + platform.platform())

The directory structure looks like this.

.
├── hello.py
└── mcpi
    ├── __init__.py
    ├── block.py
    ├── connection.py
    ├── event.py
    ├── minecraft.py
    ├── util.py
    └── vec3.py

Execute python hello.py, and it is successful when the platform information of the PC that executed the program is displayed in the chat message on the Minecraft Pi Edition side.

raspberry-pi-mcpi.png

Summary

This time, I read the code of mcpi and developed it remotely because it communicates with sockets.

The merit of using a PC is that you can use a development environment that you are familiar with such as an editor, and because you can operate it remotely, one person can operate Minecraft and the other can execute a program to interfere or assist. I think.

As a side benefit, since mcpi is copied to the same directory, you can write code while checking the contents of the method.

I feel that it is necessary for the Raspberry Pi to execute it in the field of education, but I think that the side that creates sample code as a teaching material may use such a highly productive environment.

Recommended Posts

Operate Minecraft Pi Edition from your PC
Block and draw images in Minecraft Pi Edition
Power on / off your PC with raspberry pi
Operate Filemaker from Python
Operate neutron from Python!
Operate LXC from Python