Coding with Minecraft

Exclusive offer: get 50% off this eBook here
Instant Minecraft: Pi Edition Coding How-to [Instant]

Instant Minecraft: Pi Edition Coding How-to [Instant] — Save 50%

Expand your Minecraft world by learning to code with Minecraft: Pi Edition book and ebook

$9.99    $5.00
by Daniel Bates | September 2013 | Open Source

This article by Daniel Bates, author of Instant Minecraft: Pi Edition Coding How-to, will guide you through to the point where you have Python code interacting with Minecraft: Pi Edition. We will walk through the steps required to connect to the game and post a chat message. Many more features are available once connected, some of which are covered at the end of this article.

(For more resources related to this topic, see here.)

Getting ready

Before you begin, you will need a running copy of Minecraft: Pi Edition. Start a game in a new or existing world, and wait for the game world to load.

How to do it...

Follow these steps to connect to the running Minecraft game:

  1. Open a fresh terminal by double-clicking on the LXTerminal icon on the desktop.

  2. Type cd ~/mcpi/api/python/mcpi into the terminal.

  3. Type python to begin the Python interpreter.

  4. Enter the following Python code:

    import minecraft
    mc = minecraft.Minecraft.create()
    mc.postToChat("Hello, world!")

  5. In the Minecraft window, you should see a message appear!

How it works...

First, we used the cd command, which we have seen previously to move to the location where the Python API is located. The application programming interface (API) consists of code provided by the Minecraft developers that handles some of the more basic functionality you might need. We used the ~ character as a shortcut for your home directory (/home/pi). Typing in cd /home/pi/mcpi/api/python/mcpi would have exactly the same effect, but requires more typing.

We then start the Python interpreter. An interpreter is a program that executes code line by line as it is being typed. This allows us to get instant feedback on the code we are writing. You may like to explore the IDLE interpreter by typing idle into the terminal instead of python. IDLE is more advanced, and is able to color your code based on its meaning (so you can spot errors more easily), and can graphically suggest functions available for use.

Then we started writing real Python code. The first line, import minecraft, gives us access to the necessary parts of the API by loading the minecraft module. There are several Python code files inside the directory we moved to, one of which is called, each containing a different code module. The main module we want access to is called minecraft.

We then create a connection to the game using mc = minecraft.Minecraft.create(). mc is the name we have given to the connection, which allows us to use the same connection in any future code. minecraft. tells Python to look in the minecraft module. Minecraft is the name of a class in the minecraft module that groups together related data and functions. create() is a function of the Minecraft class that creates a connection to the game.

Finally, we use the connection we have created, and its postToChat method to display a message in Minecraft. The way that our code interacts with the game is completely hidden from us to increase fl exibility: we can use almost exactly the same code to interact with any game of Minecraft: Pi Edition, and it is possible to use many different programming languages. If the developers want to change the way the communication works, they can do so, and it won't affect any of the code we have written.

Behind the scenes, some text describing our command is sent across a network connection to the game, where the command is interpreted and performed. By default, the connection is to the very Raspberry Pi that we are running the code on, but it is also possible to send these commands over the Internet from any computer to any network-connected Raspberry Pi running Minecraft. A description of all of these text-based messages can be found in ~/ mcpi/api/spec: the message sent to the game when we wrote mc.postToChat("Hello,world!") was"Hello, world!"). This way of doing things allows any programming language to communicate with the running Minecraft game. As well as Python, a Java API is included that is capable of all the same tasks, and the community has created versions of the API in several other languages.

There's more...

There are many more functions provided in the Python API, some of the main ones are described here. You can explore the available commands using Python's help function: after importing the minecraft module, help(minecraft) will list the contents of that module, along with any text descriptions provided by the writer of the module. You can also use help to provide information on classes and functions.

It is also possible to create your own API by building on top of the existing functions. For example, if you find yourself wanting to create a lot of spheres, you could write your own function that makes use of those provided, and import your module wherever you need it.

The minecraft module

The following code assumes that you have imported the minecraft module and created a connection to the running Minecraft game using mc = minecraft.Minecraft.create().

Whenever x, y, and z coordinates are used, x and z are both different directions that follow the ground, and y is the height, with 0 at sea level.




This gets the type of a block at a particular location as a number. These numbers are all provided in the block module.

mc.setBlock(x,y,z, type)

The sets the block at a particular position to a particular type. There is also a setBlocks function that allows a cuboid to be filled - this will be faster than setting blocks individually.


This gets the height of the world at the given location.


This gets a list of IDs of all connected players.


This saves the current state of the world.


This restores the state of the world from the saved checkpoint.


This posts a message to the game chat.

mc.setting(setting, status)

This changes a game setting (such as "world_immutable" or "nametags_visible") to True or False.,y,z)

This moves the game camera to a particular location. Other options are setNormal(player_id), setFixed(), and setFollow(player_id).


This gets the position of the host player.


This moves the host player.

This gets a list of all blocks that have been hit since the last time the events were requested. Each event describes the position of the block that was hit.

This clears the list of events. At the time of writing, only block hits are recorded, but more event types may be included in the future.

The block module

Another useful module is the block module: use import block to gain access to its contents. The block module has a list of all available blocks, and the numbers used to represent them. For example, a block of dirt is represented by the number 3. You can use 3 directly in your code if you like, or you can use the helpful name block.DIRT, which will help to make your code more readable.

Some blocks, such as wool, have additional information to describe their color. This data can be provided after the block's ID in all functions. For example, to create a block of red wool, where 14 is the data value representing "red":

mc.setBlock(x, y, z, block.WOOL, 14)

Full information on the additional data values can be found online at values(Pocket_Edition).


This article gave us a simple code to interact with the game. It also explained how Python communicates with the game. The other aspects brought forth by the article include overview of other API functions, which can build useful functions on top of existing ones.

Resources for Article:

Further resources on this subject:

Instant Minecraft: Pi Edition Coding How-to [Instant] Expand your Minecraft world by learning to code with Minecraft: Pi Edition book and ebook
Published: June 2013
eBook Price: $9.99
See more
Select your format and quantity:

About the Author :

Daniel Bates

Daniel Bates is a Computer Science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors, and when he goes home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi  Foundation since 2011, and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to, Packt Publishing.

Books From Packt

 Instant Minecraft Designs How-to [Instant]
Instant Minecraft Designs How-to [Instant]

Instant Raspberry Pi Gaming [Instant]
Instant Raspberry Pi Gaming [Instant]

 Raspberry Pi Networking Cookbook
Raspberry Pi Networking Cookbook

 Raspberry Pi Home Automation with Arduino
Raspberry Pi Home Automation with Arduino

Raspberry Pi for Secret Agents
Raspberry Pi for Secret Agents

 Raspberry Pi Media Center
Raspberry Pi Media Center

 Python Data Visualization Cookbook
Python Data Visualization Cookbook

MySQL for Python
MySQL for Python

No votes yet

Post new comment

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software