Coding Minecraft

As mentioned earlier, Minecraft has its own API that can be used to control and manipulate the Minecraft world. The API is called mcpi, which stands for Minecraft Pi. The mcpi API is a set of functions to query and control the player and the environment in the Minecraft world.

Note: There is an mcpi API downloadable online, which is slightly different from the one available on the CREATOR Kit. The CREATOR Kit has an enhanced version that includes functions for controlling the movement of the Minecraft player, determining the direction the player is looking, and showing/hiding the Minecraft window.

A typical Minecraft program will consist of four parts:

  1. Initialization Code

  2. Starting Minecraft

  3. Connecting to the Minecraft Server

  4. The Game Loop

Initialization Code

The first thing we typically do in any program (including our Minecraft programs) is to import the modules and functions we'll need for our code. The set of functions that the Ready Set STEM uses for Minecraft programs is called rstem.mcpi, so in addition to any other functions we will need to import, we'll typically either import the entire rstem.mcpi module or we'll import specific functions within the rstem.mcpi module.

Starting Minecraft

In order to start Minecraft, we use a function within the rstem.mcpi module called control.show(). This function will be used in pretty much every Minecraft program to start the game prior to executing our game loop.

Connecting to the Minecraft Server

Once the Minecraft game has been started and we have our Minecraft window open, we next need to connect our code to the Minecraft server so that we can interact with the existing game. This is accomplished using a function from the rstem.mcpi module called minecraft.Minecraft.create().

Note that not every Minecraft function will require that this step was completed, but many do, so it's good to get in the habit of including it even if you don't use it.

The Game Loop

Once we have our initialization complete and we've started the Minecraft game, we then create the game loop that will house the logic for our code. In many cases, the game loop will be a while loop that will run forever (until we manually stop it).

Putting It All Together

Putting all three steps together, the basic framework of our Minecraft programs will look something like this:

All of our subsequent Minecraft projects will take the form above.

For more detailed information about these — and all — Minecraft APIs, check out the API documentation linked from the RDE above.