Linux Format - UK (2019-12)

(Antfer) #1
http://www.techradar.com/pro/linux December 2019 LXF257 89

Hacking Minecraft CODING ACADEMY


ADVANCING PYTHON


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 environment, 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 ‘raspberrypi’. 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 initialising 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 programming 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 communicated 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 coordinates and block
type accordingly.

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, woolBlockBlack,
woolBlockBlackType)
becomes the following:
hackedMC.setBlock(blockX, blockY, blockZ,
woolBlockBlack, woolBlockBlackType)
Let’s start a new script to convert our Creeper head

Using Python, we can hook directly into Minecraft’s API to perform
complex calculations, alter the location of our player character, and
spawn blocks into the game world to create all kinds of creations,
both 2D and 3D. We can do pretty much anything from creating
prefabricated pixel-art to communicating directly with the player.
Now, with this issue’s tutorial, we can do all of that over the
network with our friends. By hacking into our friend’s Minecraft, we
can manipulate their game world and their player character to our
heart’s content. We’ll also do some reading and writing directly to
text files in order to create a kind of non-player character script that
can be printed to the chat display in-game.
We’re essentially further advancing our Minecraft API projects from
the tutorials in the previous few issues. With each issue we take a
deeper look into coding Python for Minecraft, with the aims of both
improving our Python programming skills and gaining a better
understanding of what actually goes on underneath the bonnet of the
world’s most popular block-based video game.

Our chatbot can
be useful for a
whole host of
applications.
We could hook it
into the hacking
code to message
our friend as
if it were an
NPC with some
basic AI.

Spawning diamond ore in our friend’s Minecraft game, using a remote Python script.

888Decmbr 2 rDm019dr09onsta December 2019 LXF257 89


Hacking Minecraft CODING ACADEMY


ADVANCINGPYTHON


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 environment, 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 ‘raspberrypi’. 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 initialising 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 programming 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


nowdisplayed“HelloWorld!”.Maythefuncommence!

Placing blocks around our friend
Now that we’ve connected to and communicated 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 coordinates and block
type accordingly.

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, woolBlockBlack,
woolBlockBlackType)
becomes the following:
hackedMC.setBlock(blockX, blockY, blockZ,
woolBlockBlack, woolBlockBlackType)
Let’s start a new script to convert our Creeper head

Using Python, we can hook directly into Minecraft’s API to perform
complex calculations, alter the location of our player character, and
spawn blocks into the game world to create all kinds of creations,
both 2D and 3D. We can do pretty much anything from creating
prefabricated pixel-art to communicating directly with the player.
Now, with this issue’s tutorial, we can do all of that over the
network with our friends. By hacking into our friend’s Minecraft, we
can manipulate their game world and their player character to our
heart’s content. We’ll also do some reading and writing directly to
text files in order to create a kind of non-player character script that
can be printed to the chat display in-game.
We’re essentially further advancing our Minecraft API projects from
the tutorials in the previous few issues. With each issue we take a
deeper look into coding Python for Minecraft, with the aims of both
improving our Python programming skills and gaining a better
understanding of what actually goes on underneath the bonnet of the
world’s most popular block-based video game.

Ourchatbotcan
beusefulfora
wholehostof
applications.
Wecouldhookit
intothehacking
codetomessage
ourfriendas
if it werean
NPCwithsome
basicAI.

Spawning diamond ore in our friend’s Minecraft game, using a remote Python script.
Free download pdf