Minecraft Mashups part two

By Russell Barnes. Posted

Continue learning how to use Minecraft with other coding languages and the real world

Continuing on from Minecraft Mashups part one, we start having Minecraft interact with and effect the real world.

The full article can be found in The MagPi 41

Control an LED with Minecraft

Using GPIO Zero in conjunction with the Minecraft Python API, we can do some amazing things very simply. GPIO Zero is the new Python coding library that allows you to quickly and easily connect and control physical electronics in the real world with Python.

 Wire up your LED to work with Minecraft

In this code, you need to hunt down a specific block; your only guide is an LED light. It blinks as you head in the right direction, blinking faster as you get close, before becoming a solid light when you’re extremely close to it.

This code keeps track of the player’s position, with a set block as the target. As the player gets nearer and the distance to the block shortens, the distance is used to alter the time delay in the LED flashing. There are four levels to the flashing: the slowest indicates you’re generally heading in the right direction, the next step up is close, the following is near, and the final one is within ten blocks. The solid light occurs within four blocks of the target, and the game lets you know when you’ve found it by displaying a message on the screen.

 Find the block in the LED game!

The code for this game is available on GitHub

Control Minecraft with a button

We have an example of an output from Minecraft that controls an LED using GPIO Zero; now to talk about how an input can be used. Using a project designed by Richard Hayler, you can create the effect of having ice powers, freezing blocks in your path at the touch of a physical push button you’ve added to a breadboard. You can also change the range of the power’s effect using a potentiometer, also known as a variable resistor.

 Wire up the Frozen circuit with this diagram

The code uses the input reading of the GPIO pin and converts it to a number usable by the code. This then generates the range of blocks that will be affected by the player when the button is pressed. When it's pressed, the selection of blocks in the range that aren't made of air have their coordinates saved so they can all be set to ice blocks.

The code lets you know the power of your ice blast, so you can tweak the resistor and figure out how to get it configured perfectly.

 [Insert Frozen reference here]Grab the code from GitHub and have a play.

Node-RED and controlling Minecraft with JavaScript

This article should give you a taste of a workshop taught at a recent CamJam by Boris Adryan. Here, we'll show you just how easy Node-RED is to use. You can refer to the original tutorial for a lot more detail and theory.

 Find Steve's position is only the start of using Minecraft with Node-RED

Preparation

Start Minecraft and open a new world. Fire up Node-RED from the Programming submenu on the Raspbian desktop. After a few moments, Node-RED is going to run as a service in the background. Open a web browser (Iceweasel works best with Node-RED) and direct it to 127.0.01:1880 to see the programming environment. On the left, there’s a panel with nodes. These are visual components that you can use to build your flow. In the middle is the flow editor where you can simply drag and drop your flow together. On the right, you can switch between the help panel and the debug panel. Note that while ‘debug’ often has some negative connotation, it’s your default text output in Node-RED.

Hello World in Minecraft

Drag and drop an inject node from the nodes panel into the flow editor. Once you’ve selected that inject node, you should see a general explanation about its functionality in the help panel – this is especially useful info when you select complex nodes with many different options.

Double-click your inject node in the flow editor. Once the associated dialog opens, change the Payload to type ‘string’ and write ‘Hello World’ in the empty text field below.

The flow of information is modelled through the exchange of messages in Node-RED, which happens by passing along a variable called msg. It has two main properties: topic and payload. In simple terms, these could be interpreted like the subject and body of an email. Drag and drop a debug node from the nodes library into the flow editor.

Drag and drop a function node into the editor. This is the node type that allows you to directly interact with the msg object in JavaScript. In your function node, write msg.payload = "chat.post("+msg.payload+")\n"; before the line with ‘return msg;’. This line is going to take the incoming payload ‘Hello World’, and assign the new content chat.post(Hello World)\n to the variable. chat.post is a command from the Minecraft API, which you can read about in a file called mcpiprotocolspec.txt in the Minecraft API directory. The end of our command is indicated by a line break, the Unix character \n.

 Hello World, Node-RED style

Drag and drop a TCP request node from the function section of the node panel. The server is 127.0.0.1, and the port is 4711. As we’re not expecting a return value, we'll Return ‘after a fixed timeout’ of 0ms. This connects Node‑RED to the Minecraft TCP socket – if you're keen to understand what a socket is, have a look at the original workshop material.

Connect the nodes by drawing connections, as in the screenshots here. Start your flow by pressing the red Deploy button in the upper-right corner. Trigger your flow by pressing the rounded rectangle to the immediate left of your inject button. Do you see your message in Minecraft?

Retrieve values from Minecraft

So far, our interaction with Minecraft has been rather one-directional. We just sent a command string that had an effect on the Minecraft world. Now we’re going to modify our flow so we can query values like our own position via the API.

In the function node, set msg.payload = "player.getPos()\n"; before the line with ‘return msg;’. Instead of leaving the TCP request node after some time, we expect our Return ‘when character is received’, namely \n.

By default, the TCP request node returns a buffer, and we need to convert the information from Node-RED using msg.payload = msg.payload.toString(); in a new function node. The result of this new function node goes straight to our debug node.

Deploy your Node-RED flow. Now have a walk around Minecraft and trigger your flow with the inject node. Do you see what you expected in the debug panel?

You can get the flows for the Hello World from the Node-RED website, which also contains the flow for the Player Position code.

Continue on to Minecraft Mashups part three.

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.