Programming Minecraft Pi with Wolfram Language

By Lucy Hattersley. Posted

Combining Wolfram Language and Minecraft provides a fun playground for learning coding

If you are a gamer, you can use the richness of the Wolfram Language to programmatically generate all kinds of interesting structures in the game world of Minecraft, or to add new capabilities to the game. If you are a coder, then you can consider Minecraft just as a fun 3D rendering engine for the output of your code.

First, make sure that your Raspberry Pi is up to date and then open Mathematica. Install the MinecraftLink Library by entering the following code, followed by SHIFT+RETURN to evaluate:

PacletInstall["MinecraftLink"]

The MinecraftLink library adds a set of new commands to the Wolfram Language for connecting to a running Raspberry Pi Minecraft game. You will find basic documentation on them by searching for ‘Minecraft’ in the Wolfram Help system. Start by launching Minecraft on the Raspberry Pi, and then start a fresh game or open an existing one. In Mathematica, load the library by evaluating:

<< MinecraftLink`

Don't overlook the grave accent (`) at the end.You can find the new commands in Figure 1. If you are connecting from a different computer, you must tell Mathematica the IP address of the Raspberry Pi:

MinecraftConnect["10.10.163.22"]

You don’t need to do this if both programs are on the same machine, but if you need to reset the connection, you can use MinecraftConnect or MinecraftConnect["localhost"].

Let’s test to see if that worked by evaluating:

MinecraftChat["Hello from the Wolfram Language"]

You should see the message ‘Hello from the Wolfram Language’ appear briefly in the game.

 Figure 1 Adding the MinecraftLink library to the Wolfram Language extends it with these new commands

Controlling Minecraft using Wolfram Language

Minecraft uses a simple {x, y, z} co-ordinate system, where x and z are the horizontal directions and y is the vertical direction. You can see the co‑ordinates in the top-left corner of the screen, but to get them programmatically you can use:

MinecraftGetPosition[]

We can teleport the character to a new location (in this case, up in the air):

MinecraftSetPosition[{0, 50, 0}]

If we have just started a game, then 10 blocks in front of us is {0,10,0}. But depending on how mountainous the terrain is, that block might be above or below ground. We can find the surface level with:

y = MinecraftGetHeight[{0, 8}]
2

We can test that by looking at the block at that position. It should be Air:

pos = {0, y, 8}
MinecraftGetBlock[pos]

Now we can start building. We can place blocks of any type – for example, ‘Wood’:

MinecraftSetBlock[pos, "Wood"]

You can specify the block material by name, BlockID number, or using an Entity. We remove them by just overwriting the block with something else, such as ‘Air’:

MinecraftSetBlock[pos, "Air"]

One reason to use the Wolfram Language for this is that it handles all kinds of interesting 2D and 3D objects, and we have set up the SetBlock command to handle these fairly automatically. For example, let’s paint a letter X in the sky in gold:

MinecraftSetBlock[pos, "GoldBlock", "X"]

By default, rasterized content will be 12 blocks wide, so if you need more detail, you can increase that with an option.

MinecraftSetBlock[pos, "GoldBlock", "", RasterSize → 50]
MinecraftSetBlock[pos, "Air", "", RasterSize → 50]

Use "[WolframLanguageLogo]" to type:
Anything you make can become blocks. Here is a plot of the function sinx:

MinecraftSetBlock[pos, "Dirt", Plot[Sin[x], {x, 0, 12}, Axes → False], RasterSize → 18]

If the content is a 3D geometry, then it will be rasterized in 3D:

MinecraftSetBlock[pos, "Wood", Sphere[], RasterSize → 50]

 Figure 2 Anything you can create in the Wolfram Language can be made into blocks, like this plot of the function sin[x]

Controlling geometry primitives in Minecraft with Wolfram Language

There are lots of 3D geometry primitives and they can be combined in many ways, including cuboids, a pyramid, and a sphere to make a basic house. We can look at our creation from the air by controlling the camera:

MinecraftSetCamera["Fixed"];
MinecraftSetCamera[{0, 25, 6}];

We can interact with blocks that you hit using the right mouse button while holding a sword. The left mouse button just places and smashes blocks, but the right mouse button creates events that wait for us to read and act on them. You can read these with:

MinecraftHitHistory[]

This shows that since the game started, we have done two of these special hits, each time on the same block at {-1,2,2}, on face number 1 (the top of the block). We are player 1, but there could be multiple players. We can fetch these pieces of information by position and name. For example, MinecraftHitHistory[-1] is the last hit, and we can extract its ‘Position’ information and use that co-ordinate in MinecraftGetBlock to discover the type of block that was most recently hit.

This shows that since the game started, we have done two of these special hits, each time on the block at {-1,2,2}, on Face 1 (the top of the block). We can fetch these pieces of information by position and name.

As well as giving you a simple programming interface to Minecraft, similar to other languages, the Wolfram Language contains hundreds of high-level functions that let you develop much more exciting projects quickly. You might want to check out some of the 3D geometry, 3D image processing, and built-in data sources as a starting point.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.