Linux Format - UK (2019-12)

(Antfer) #1
90 LXF257 December 2019 http://www.linuxformat.com

CODING ACADEMY Hacking Minecraft


around our friend’s player, and we’ve communicated
with them directly by displaying text on their screen.
Another fun way of messing with our friend’s game is by
teleporting their player around their world.
hackedMC.player.setTilePos(x,y,z)
Set the X, Y, Z coordinates 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.

Programming 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

from LXF255. Initialise the connection as above and
create some new variables:
woolBlockGreen = 35
woolBlockGreenType = 5
woolBlockBlack = 35
woolBlockBlackType = 15
Create our pixel art with alternating 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 initialised variables, assigning 0 to
woolBlockBlack and 1 to woolBlockGreen. 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, woolBlockBlack,
woolBlockBlackType)
elif pixelArt[row][pixel] == 1:
hackedMC.setBlock(pos.x,
(pos.y+7) - row, pos.z + pixel, woolBlockGreen,
woolBlockGreenType)

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).

Teleporting your friend
We’ve controlled the game world by placing blocks

Blowing up a
creeper head
in our friend’s
game can be fun,

Remember we
used ‘hackedMC’
to connect to
our friend’s PC
and just ‘mc’
for connecting
to our own
Minecraft world.
We wouldn’t
want to get
those two
mixed up...

90 LXF257December 2019 888Decmbr 2019dDo21


CODING ACADEMY Hacking Minecraft


around our friend’s player, and we’ve communicated
with them directly by displaying text on their screen.
Another fun way of messing with our friend’s game is by
teleporting their player around their world.
hackedMC.player.setTilePos(x,y,z)
Set the X, Y, Z coordinates 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
withthefollowing:
touch~/.minecraft /mcpipy/script.txt
touch~/.minecraft /mcpipy/NPC.py
Openthe newly created files in your favourite text
editoras before.

Programming your script
Startoffthe 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
textfilein read-only mode ( “r” ) within another variable
(mcScript ).
frommc import *
mc= Minecraft()
mcScript = open(”script.txt”, ”r”)
Ifyou’d like to call from a text file outside of the
mcpipydirectory where your Python file is, you can do
sowith:
mcScript = open(”~/.minecraft/mcpipy/script.txt”,
”r+”)
You’llalso notice we’re using “r+” instead of “r”
thistime. The available modes for editing text files are:
append( ”a” ), read (“r” ) and read+write ( “r+” ). As we

from LXF255. Initialise the connection as above and
create some new variables:
woolBlockGreen = 35
woolBlockGreenType = 5
woolBlockBlack = 35
woolBlockBlackType = 15
Create our pixel art with alternating 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 initialised variables, assigning 0 to
woolBlockBlack and 1 to woolBlockGreen. When we run
this code, we’ll spawn a large pixelart Creeper head in
front of our friend’s player.
pos= hackedMC.player.getTilePos()
forrowinrange(len(pixelArt)):
forpixelinrange(len(pixelArt[row])):
ifpixelArt[row][pixel]==0:
hackedMC.setBlock(pos.x,
(pos.y+7)- row,pos.z+ pixel,woolBlockBlack,
woolBlockBlackType)
elifpixelArt[row][pixel]==1:
hackedMC.setBlock(pos.x,
(pos.y+7) - row, pos.z + pixel, woolBlockGreen,
woolBlockGreenType)

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).

Teleporting your friend
We’ve controlled the game world by placing blocks

Blowing up a
creeper head
in our friend’s
game can be fun,

Rememberwe
used‘hackedMC’
toconnectto
ourfriend’sPC
andjust‘mc’
forconnecting
toourown
Minecraftworld.
Wewouldn’t
wanttoget
thosetwo
mixedup...
Free download pdf