Linux Format

Hacking Minecraft

Calvin Robinson demonstrat­es how to use Python to hack into a friend’s Minecraft game and create a chatbot.

-

Calvin Robinson demonstrat­es how to use Python to hack into a friend’s Minecraft game and create a chatbot.

This issue sees the last in our series of Minecraft Python coding tutorials. Over the last six issues, we’ve looked at integratin­g Python code into Minecraft, giving us the creative freedom to produce all kinds of wonderful monstrosit­ies.

We’ve looked at how to move our character around the world (see LXF252) using code, building blocks with code and automatica­lly building prefabrica­ted creations. We experiment­ed with Turtle graphics

(see LXF255), pixel art and vector graphics (see LXF256)

in Minecraft. We even designed our own graphical user interface, with buttons to spawn buildings (see LXF254)

in our Minecraft worlds.

Minecraft could be considered one of the most creative video games of our time, with virtual environmen­ts spanning acres of procedural­ly generated random biomes. The world and its contents are left to the player’s imaginatio­n. However, in Minecraft one’s creativity is usually limited to the in-game tools provided. Now, thanks to the release of the Raspberry Pi version of Minecraft, we’re finally able to hook directly into the API with Python and program away to our heart’s content.

Combining Raspberry Pi Jam, developed by Alexander Pruss, with the community project Forge we’re able to use the Raspberry Pi Minecraft API on Linux. We’ve put together a package called Mcpifomo

(Minecraft Pi Forge Mod) that does all of this for you at http://rogerthat.co.uk/mcpifomo.rar. Mcpifomo includes MCPIPY by fleap and bluepillra­bbit.

You will need to set up and prepare your Minecraft installati­on with Mcpifomo. However, if you’re using the

Minecraft Pi edition on a Raspberry Pi, no additional software is necessary.

Prep and install

Download and install the appropriat­e version of

Minecraft for your distro of choice. Developers Mojang offers pre-packaged solutions for Debian/ubuntu and Arch (DEB and AUR respective­ly), and a generic java version for everyone else at www.minecraft.net/ en-us/download/alternativ­e. Next, you’ll want to make sure you’ve got Python installed: the command sudo apt-get install python should suffice for any Debian-based distro (after doing a sudo apt-get update, of course).

It’s always best to backup your Minecraft folder first before making any changes. It can be found hidden in your /home as .minecraft. The keyboard shortcut Ctrl+h can be used in most file explorers to show hidden directorie­s, or use ls -la if you’re not a fan of the GUI. In a terminal window mv ~/.minecraft ~/minecraftb­ackup should suffice to save a hidden backup (remember to remove this later if you don’t need it, as it’ll only take up extra space).

Extract the new .minecraft directory from Mcpifomo directly into your Home directory. You may need to sudo apt-get install unrar if your system doesn’t recognise the RAR file. unrar xe Mcpifomo.rar ~/.minecraft should then do the trick. If you have

worlds you’d like to carry over, copy the Saves directory from your backup .minecraft directory into the new one using cp –r ~/.minecraft-backup/saves ~/. minecraft/ . Launch Minecraft as you normally would, but after logging in select Forge as the profile.

This should load Minecraft 1.8 with Forge 11.14. You can play around with the latest version of Minecraft and download and install an updated Forge if you wish, but these are the versions we’ve found most compatible with Raspberry Jam – your results may vary. In any case you’ll know you’re running the correct profile when you see the version numbers in the bottom-left corner of the window.

Create a new Superflat world in single-player Creative mode and you’re ready to begin coding. We’ve included a single ‘Flat’ world pre-installed with the Mcpifomo package. It might be worthwhile making a few copies of this to experiment on: just copy the ~/.minecraft/saves/world directory a few times.

Hacking Minecraft

We’re going to create a Python script that connects directly to a Minecraft game running on another computer and then allows us to have some fun with their game world. We’ll have the ability to manipulate the character, the environmen­t, and place blocks, as we have been doing in the previous Minecraft Python tutorials, but this time we’re working on someone else’s game. We’ll be able to have pranks galore, but we should note that this shouldn’t be done without prior consent from the other person.

Getting your friend’s IP

Before we do anything, we’ll need to know the IP address of our friend’s computer. Make sure your PC is connected to the same network as theirs and run Angry

IP Scanner. This lists all the computers connected to the same network as you, within your IP range by default. Look for a hostname that suggests it could be your friend’s computer. For instance, if your friend is running the Raspbian distro, the default hostname will be ‘raspberryp­i’. Be sure to identify your own IP for exclusion by opening a terminal and running ifconfig /all .

Initiate a Python Script

Create a new Python script in the IDLE or your favourite text-based editor. All Python scripts should be saved in ~/home/.minecraft/mcpipy/, regardless of whether you’re running Minecraft Pi edition or retail Linux

Minecraft. We’re initialisi­ng a connection to our friend’s

IP with: import mcpi.minecraft as minecraft mc = minecraft.minecraft.create() friendsip = “192.168.1.2” hackedmc = minecraft.minecraft.create(friendsip)

Hello World!

As with any programmin­g tutorial, we start off with a quick “Hello World!”: hackedmc.posttochat(“hello world!”)

Save this script in your ~/home/.minecraft/ mcpipy/ directory with a name like hax.py and run the script in Minecraft Pi by typing /python hax in chat and pressing Return. You’ll notice your friend’s game has now displayed “Hello World!”. May the fun commence!

Placing blocks around our friend

Now that we’ve connected to and communicat­ed with our friend’s game, it’s time to start building something around them. We’ll need to gather their player position and place blocks relative to them: hackedpos = hackedmc.player.gettilepos() hackedmc. setblock(hackedpos.x,hackedpos.y,hackedpos.z,block. DIAMOND_ORE)

Now that we have their position, we can build around them by altering the X, Y and Z coordinate­s and block type accordingl­y.

Building in their world

To take creations from previous Python Minecraft tutorials and convert them to work across the network we’ll replace the mc variable which points to our game world with hackedmc , referring to our friend’s. Thus, mc.setblock(blockx, blocky, blockz, woolblockb­lack, woolblockb­lacktype) becomes the following: hackedmc.setblock(blockx, blocky, blockz, woolblockb­lack, woolblockb­lacktype)

Let’s start a new script to convert our Creeper head

from LXF255. Initialise the connection as above and create some new variables:

woolblockg­reen = 35 woolblockg­reentype = 5 woolblockb­lack = 35 woolblockb­lacktype = 15

Create our pixel art with alternatin­g block types.

pixelart = [[1, 1, 1, 1, 1, 1, 1, 1],[1, 0, 0, 1, 1, 0, 0, 1],[1, 0, 0, 1, 1, 0, 0, 1],[1, 1, 1, 0, 0, 1, 1, 1],[1, 1, 0, 0, 0, 0, 1, 1],[1, 1, 0, 0, 0, 0, 1, 1],[1, 1, 0, 1, 1, 0, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1]]

The next step is to link these 1s and 0s to our previously initialise­d variables, assigning 0 to woolblockb­lack and 1 to woolblockg­reen. When we run this code, we’ll spawn a large pixelart Creeper head in front of our friend’s player.

pos = hackedmc.player.gettilepos() for row in range(len(pixelart)): for pixel in range(len(pixelart[row])): if pixelart[row][pixel] == 0:

hackedmc.setblock(pos.x, (pos.y+7) - row, pos.z + pixel, woolblockb­lack, woolblockb­lacktype) elif pixelart[row][pixel] == 1:

hackedmc.setblock(pos.x, (pos.y+7) - row, pos.z + pixel, woolblockg­reen, woolblockg­reentype)

Spawn it, blow it

Save your new script in ~/home/.minecraft/ mcpipy/ and run it directly in Minecraft with /python scriptname . Now sit back and watch your friend jump out of their skin when a giant Creeper head appears in front of them. If you want to take things to the next level, you could duplicate the for loop to spawn rows of TNT behind the Creeper head. An explosive Creeper head would be quite something.

The easiest way to spawn primed TNT is to place it next to an enabled redstone torch (blockid 76).

Teleportin­g your friend

We’ve controlled the game world by placing blocks around our friend’s player, and we’ve communicat­ed with them directly by displaying text on their screen. Another fun way of messing with our friend’s game is by teleportin­g their player around their world. hackedmc.player.settilepos(x,y,z)

Set the X, Y, Z coordinate­s to wherever you want your friend’s player to be teleported, though note that it might end up with them inside a cliff.

Chatbot

In previous tutorials, we’ve looked at posting the occasional message from Python to Minecraft’s in-game chat and saving text to files. Now we’re going to combine the two and go one step further in creating our own chatbots.

We’ll want to create two empty documents for this tutorial, one being a regular text file and the other an empty Python script. You can do this in the terminal with the following: touch ~/.minecraft /mcpipy/script.txt touch ~/.minecraft /mcpipy/npc.py

Open the newly created files in your favourite text editor as before.

Programmin­g your script

Start off the code in your NPC.PY file by importing the Minecraft libraries into Python, initiating an instance of Minecraft within a local variable ( mc ) and opening the text file in read-only mode ( “r” ) within another variable ( mcscript ).

from mc import * mc = Minecraft() mcscript = open(”script.txt”, ”r”)

If you’d like to call from a text file outside of the mcpipy directory where your Python file is, you can do so with:

mcscript = open(”~/.minecraft/mcpipy/script.txt”,

”r+”)

You’ll also notice we’re using “r+” instead of “r” this time. The available modes for editing text files are: append ( ”a” ), read (“r” ) and read+write ( “r+” ). As we

did earlier, we’ll start off with a “Hello World!” message: mcscript = open(”~/.minecraft/mcpipy/script.txt”,

”r+”) mcscript.write(”hello World”) mcscript.close()

Here we’re opening the file in read+write mode, saving a string to the file and closing it. It’s important to note that text files will only save if closed. Now that we’ve got something written in our text file we can

print our scripts in-game:

mcscript = open(”~/.minecraft/mcpipy/script.txt”,

”r+”) mc.posttochat(mcscript) mcscript.close()

Note we’re using the posttochat action from our mc instance, rather than print , and we’re posting the contents of a variable instead of a string.

Reading directly into your world

You can use the traditiona­l print command to read text into your game world. The following code will read the first line of your text file. mcscript = open(”~/.minecraft/mcpipy/”script.txt”,

”r+”) print mcscript .readline()

Add a few more lines to script.txt after “Hello World” – perhaps create some dialogue for an NPC.

Rehearsing multiple lines at a time

You’re probably going want to post several dialogue lines of text into your game at a time, especially if you’re creating a bot of some kind. This will post line-by-line: mcscript = open(”~/.minecraft/mcpipy/script.txt”,

”r+”) print mcscript .readline() print mcscript .readline() print mcscript .readline()

Using a loop

Of course, the previous step will only read the first three lines. That might be useful if you only want your NPC to say so many things at once, but you can create a for loop to read the entire document line-by-line, too: for line in mcscript.readline():

print line

Writing and autosaving

Depending on what kind of bot you’re creating, you may

want to write text back to the script file: with open(“script.txt”, ”w”) as mcscript: mcscript .write(”strings” + Variables)

Here we can save strings and/or variables directly into the text file. If you’re using with you don’t need to remember to add mcscript.close() , as it will save automatica­lly, which is useful.

Combining chatbots with other projects

Where this gets really interestin­g is when you use text scripts for commands instead of dialogue. Much like our hacking script, we can take our pixel art code from LXF255, and with it we could call a text file from the

pixelart variable instead of hard-writing numbers into the code:

pixelart = mcscript .readline()

We can now change the pixel art every time by adjusting a line in the text file, without altering the Python code. This means we could keep our program running and load different things into our Minecraft world by simply changing the text file. Drag-and-drop at its best!

 ??  ??
 ??  ?? Scanning a network for other computers running Minecraft, ready for us to take over with our hacks.
Scanning a network for other computers running Minecraft, ready for us to take over with our hacks.
 ??  ?? Spawning diamond ore in our friend’s Minecraft game, using a remote Python script.
Spawning diamond ore in our friend’s Minecraft game, using a remote Python script.
 ??  ?? Blowing up a creeper head in our friend’s game can be fun,
Blowing up a creeper head in our friend’s game can be fun,
 ??  ?? And here we are putting words into Steve’s mouth!
And here we are putting words into Steve’s mouth!
 ??  ?? As with all programmin­g scripts, the best place to start is with a basic ‘Hello World’.
As with all programmin­g scripts, the best place to start is with a basic ‘Hello World’.

Newspapers in English

Newspapers from Australia