APC Australia

Create a Pi-powered Minecraft server

Jonni Bidwell invites you into the amazing and creative world of Minecraft. Then reveals how you can host your own server to keep out the riff raff.

-

Besides empowering a new generation of makers, the Raspberry Pi has also establishe­d its merits as a gaming platform. Projects like RetroPie, combined with the Pi’s cheapness, mean that it’s great for playing old classics. And even some new ones. Like Minecraft.

In case you’ve been living under a rock since 2011, Minecraft is the bestsellin­g computer game of all time. It’s an open-world sandbox affair in which our hero, Steve, roams around a landscape made of voxels (blocks). Resources can be mined and crafted into other resources, so that Steve can build houses, farm crops, cook food and fight enemies (all rendered in low-res splendour).

A special version, Minecraft: Pi Edition, was released in 2013 and has been bundled with Raspbian since 2014. This version is in some ways cut down, in the sense that there isn’t any crafting or baddies, and nor is there any limit on the resources available for building. But it does have some features that aren’t present in the full game, most notably a Python API for manipulati­ng the world while a game is being played. Not having to worry about creatures trying to kill you or where your next meal is coming from makes it great for younger players, emphasisin­g the creative aspects of the game. The Python API makes it an ideal platform for aspiring coders, young and old.

To get started, first head over to the Menu, then go to ‘Games > Minecraft Pi’. Choose ‘Single Player’ and then ‘New Game’. You’ll see that the game window is slightly offset from the applicatio­n window behind it. Make sure your mouse arrow is parallel with the top of the app window, and hold down to reposition it.

Click ‘Start Game’, followed by ‘Create New’. Your new world will load. Feel free to explore and build. The controls for the game are pretty simple, and are as follows:

You can use the mouse to look around you, too. It can also be used to select items from the inventory. By default, you’re holding a sword. Click blocks to destroy them. The sword can also be used to dig. If you select a block from your inventory, you can begin building. Use the right mouse button to place down the block, or the left to destroy it.

CODING BLOCKS

To start coding in Minecraft: Pi Edition, press the Tab key while it’s running. Go to ‘Menu > Programmin­g’. Click ‘Python 3’. Try to place the text boxes so they’re next to one another.

You can type commands here to change the in-game world. Over time, you can also write scripts to automate tasks for you — for example, a script that places a stone block wherever you walk. So without further ado, let’s reprogram the game to display a simple message.

Type in the following commands, pressing Return after each one:

from mcpi.minecraft import Minecraft mc = Minecraft.create() mc.postToChat(“Hello world, I’m playing Minecraft Pi!!!”)

Press Return to display your first in-game message. Next, look at the

top-left of the Minecraft Pi screen. You should see your X, Y and Z coordinate­s. This is an excellent way to work out your location, as well as place blocks precisely.

You can use Python to summon any number of blocks of various materials. First, let’s try placing a single gold block immediatel­y behind you. Type in the following command: x, y, z = mc.player.getPos()

You’ll need to run the above command each time you move, to give the game your updated coordinate­s. Press Return, then type: mc.setBlock(x+1, y, z, 41)

The number 41 at the end of the last command represents the ID number for gold. Each block or item in the game has a unique ID. If you know the ID of a particular type of block, assign it a name to make it easier to remember.

For example: gold=41

Press Return, then run these commands to create a giant 10 x 10 x 10 cube of solid gold: x, y, z = mc.player.getPos() mc.setBlocks(x+1, y+1, z+1, x+11, y+11, z+11, gold)

This is just a small selection of commands that can be run on

Minecraft Pi. It’s also possible to form complex shapes, teleport the player and even drop blocks as you move around. For a full list of commands that you can run in Minecraft Pi, as well as the ID numbers for each type of block, head to www.stuffabout­code.com/p/minecrafta­pi-reference.html.

AVOIDING THE MINEFIELDS

As we’ve discussed, Minecraft Pi is very similar to the Pocket edition of Minecraft, which can be played on handheld devices such as Android phones and iPhones. It can’t interact with people using the official Minecraft client. According to reports on the Mojang website, it appears the client hasn’t been updated in a while, but if you simply want to make simple

structures and learn to code, without the extra features of the full Minecraft client, then it’s ideal.

Although we’ve seen it’s possible to get the full version of Minecraft 1.8.9 running on a Raspberry Pi 3 (see the boxout back over the page), the method isn’t officially supported by Mojang. This means that you’re likely to see reduced performanc­e over a regular desktop machine, such as glitches and crashes some of the time.

The experiment­al graphics driver that has to be enabled on the Raspberry Pi to use the full version of Minecraft stops the official Minecraft Pi client from working, but you can disable it again by running sudo raspiconfi­g and then rebooting the machine.

Neither version of Minecraft plays particular­ly well if you’re accessing the Pi over VNC, so it’s best to play it directly.

If you wish to play the full version of Minecraft, you do need to purchase an account with Mojang. If you now have the Minecraft bug and want to take it further, read on for details of how to set up your own Minecraft server on the Pi, and build your own digital world block by block.

MINECRAFT SERVER

On the previous pages, we discussed how to play Minecraft directly on the Pi, partly for fun and partly as an introducti­on to programmin­g with Python. This part of the tutorial concerns installing the Minecraft Server software on the Raspberry Pi 3. This enables others running the Minecraft client on their computers to connect to your own online world and play free from thieves and ‘griefers’.

Minecraft runs in Java, which is pre-installed in the latest version of Raspbian, meaning it’s easier than ever to set up a server. The server has fairly low requiremen­ts, but for best performanc­e, we recommend using a Raspberry Pi 2 or 3. With your Pi in hand and a fresh install of Raspbian, connect to it via SSH. Note that the default username and password is ‘raspberry’ and ‘pi’, respective­ly. Make a note of your Pi’s IP address, because you’ll need it later.

Next, create a directory for Minecraft and open it with the following command:

$ mkdir minecraft && cd minecraft

Then download the latest version of SpigotMC, a highly customisab­le and lightweigh­t version of Minecraft Server:

$ wget https://hub.spigotmc. org/jenkins/job/BuildTools/ lastSucces­sfulBuild/artifact/ target/BuildTools.jar

Next, tell the Pi to start building the server tools: $ java -jar BuildTools.jar Once the process is complete, you’ll see a message stating “Saved as spigot-1.x.x.jar”, where 1.x.x is the current version number of Spigot that was downloaded (1.11.2 at the time of writing). Next, start the Minecraft Server with:

$ java -jar -Xms512M -Xmx1008M spigot-1.x.x.jar nogui

Again, substitute ‘1.x.x’ with the version number of Spigot. The program tells you that you need to agree to the EULA in order to run the server. To do this, type: $ nano eula.txt Scroll down with your arrow keys and delete the word false . Replace it with TRUE . Press ‘Ctrl-X’, then ‘ Y’ and Return to save your changes.

Start the server software again with the same command as beforehand:

$ java -jar -Xms512M -Xmx1008M spigot-1.x.x.jar nogui

You’ll see a message stating “Loading Libraries, please wait...” and then some basic informatio­n about the game displays. Note that the default game mode is Survival, and you then see the spawn area loading slowly as a percentage. This may restart a couple of times as various levels are spawned. You’ll see a message stating ‘Done!’ once the process is complete.

Now it’s time to test your server. Go to a computer with the Minecraft client installed and start the program. On the main screen, choose Multiplaye­r. Next, click ‘Add Server’. Click the box marked ‘Server Name’ and type a name of your choice — for example, “Pi Minecraft Server”. Click the box marked ‘Server Address’ and enter the IP address of your Pi. Click ‘Done’ when complete. You should now see the Minecraft server listed.

Hover your mouse over it and click the blue Play button. When the game loads, feel free to wander around a little and even work through your anger issues by smashing a few blocks to make sure the game responds well. Once you’re satisfied, close the Minecraft window and return to your SSH client, which is still connected to the Pi. Type stop for the time being to stop the server.

You can type stop at any time to halt the server if, for instance, you need to make any changes. In future, if you wish to run the server software, connect to the Pi via SSH, then use the following command:

$ cd ~/minecraft && java -jar -Xms512M -Xmx1008M spigot-1.x.x.jar nogui

Again, replace ‘1.x.x’ with the actual version number of Spigot you currently have installed. If you’re unsure of this, type the command cd ~/minecraft && ls to view the contents of the minecraft folder — you can then see the version of ‘spigot-1.x.x.jar’ in there.

BASIC SETTINGS

Once the server is up and running, it’s time to look at how you can tweak some of the settings. The basic settings are contained in a file named ‘server. properties’. You can view and edit this file by running the command:

$ cd ~/minecraft && nano server.properties

If you’re intent on fine-tuning your Minecraft world, visit http://minecraft. gamepedia.com/server.properties to view all values for all potential settings. For now, we’re just making some basic changes. Scroll to the very bottom of the file to the value motd= using your cursor keys. This is simply the message that appears when you first connect to the Minecraft server. By default, this simply says ‘A Minecraft server’.

Delete the line of text and replace with the following cheery message:

motd=\u00A74 \u00A7l Welcome to my Pi Minecraft Server\!

Another change you may want to make to the server — especially for people new to Minecraft — is to change the game from Survival Mode to Creative instead. Creative Mode allows for an infinite amount of building materials. Players also don’t become hungry or lose energy, so are free to start making their constructs right away.

Scroll up to gamemode= 0 and change the 0 to 1 to do this. Make sure also to change force

gamemode=false to true to be certain that everyone who logs on in the future will be in Creative Mode.

If you’re determined to stay in Survival Mode but want to start off slowly, consider changing spawn

monsters=true to false . You can also change the game’s difficulty by changing difficulty=1 to a value between 0 and 3, with zero being the easiest difficulty level.

When you are satisfied with the changes that you’ve made, hit ‘Ctrl-X’, then ‘Y’, then Return to save your changes and boot the server.

ADVANCED TWEAKS

As you become more comfortabl­e with playing Minecraft, or if you’re already an experience­d player, you may find the basic configurat­ion options in ‘server.properties’ limiting.

This is where SpigotMC really comes into its own, with an advanced configurat­ion file. There’s a number of tweaks to improve your Minecraft experience. Simply stop the server, then run the following command:

$ cd ~/minecraft && nano spigot.yml

By way of an example of how finely tuned Spigot is, scroll down to where

it says zombie-aggressive

towards-villager: true and change it to false . This setting determines whether or not zombies will attempt to kill Minecraft villagers. The Pi requires fewer AI resources for ‘friendly’ zombies, so this may speed up your server.

A complete list of the various settings that can be changed in Spigot, along with full descriptio­ns of what they do, can be found at www.spigotmc. org/wiki/spigot-configurat­ion.

SERVER SLOWDOWN

Your configurat­ion may now be tweaked to your heart’s content, but as more players join, and more complex structures are created, you may find Minecraft slowing down. As we’ve previously discussed, one way to make sure there is plenty of free space is to move the Minecraft folder to an external drive. The syntax of the command is:

$ sudo mv ~/minecraft /media/ pi/MYUSB

Where MYUSB is the name of your USB stick.

Bear in mind that the above commands for running the software include the cd command, which refers to the minecraft directory in the home folder, so make sure to modify the commands accordingl­y — for example, cd ~/minecraft becomes cd /media/pi/MYUSB/ minecraft .

If you’re still having trouble, try starting the server with the following command which, without going into technical details, instructs Java to make better use of your CPU:

$ cd ~/minecraft && java -jar -Xms512M -Xmx1008M -XX:+UseConcMar­kSweepGC -XX:+UseParNewG­C -XX:+CMSIncreme­ntalPacing -XX:ParallelGC­Threads=2 -XX:+Aggressive­Opts -jar spigot-1.x.x.jar nogui

For even better performanc­e, consider editing the ‘server.properties’ file once again, this time reducing the view-distance from 10 to 5 . This determines how much of the map has to load as you move around. You can also alter max-players to change the number of people who can be logged in at the same time.

 ??  ?? With time, patience and some coding to automate block placement, you can have your very own castle.
With time, patience and some coding to automate block placement, you can have your very own castle.
 ??  ?? When you get tired of your Midas touch, try repeating the cube command with a different block ID to change the building material.
When you get tired of your Midas touch, try repeating the cube command with a different block ID to change the building material.
 ??  ?? Minecraft multiplaye­r screen running on Debian Linux. The welcome message has been updated (see the main text).
Minecraft multiplaye­r screen running on Debian Linux. The welcome message has been updated (see the main text).
 ??  ?? The basic settings for the game can all be configured here. Creative Mode is best for starting out, but there’s also Survival, Adventure and even Hardcore.
The basic settings for the game can all be configured here. Creative Mode is best for starting out, but there’s also Survival, Adventure and even Hardcore.
 ??  ?? The Port Forward website provides instructio­ns for setting up port forwarding for most models of router.
The Port Forward website provides instructio­ns for setting up port forwarding for most models of router.

Newspapers in English

Newspapers from Australia