Linux Format - UK (2019-12)

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

Hacking Minecraft CODING ACADEMY


GUIZERO


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 traditional 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
automatically, which is useful.

Combining chatbots with other projects
Where this gets really interesting 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!

If you’re looking to take your coding to the next level, get a copy of
LXF252 (see page 66 for back issues) and combine everything we’ve
done today with hacking and chatbots with the graphical user
interface we developed for Minecraft in that issue.
Using GUIZero, we could develop a visual interface for connecting
to another player’s Minecraft world, with buttons for spawning items
on a whim. For example we could supply our friends with diamond
pickaxes and armour at the push of a button, thus essentially turning
our fun project into a fully fledged admin tool for a multiplayer server.
To start you off:
def drawDiamond():
playerPos = mc.player.getPos()
mc.setBlock(playerPos.x, playerPos.y + 1, playerPos.z-1, 57)
mc.postToChat(“Diamond granted to player.“)
giveDiamond = PushButton(app, command=drawDiamond,
text=”Diamond”, width=10, grid=[1,2])

MINE US FOR MORE TUTORIALS! Subscribe now at http://bit.ly/LinuxFormat


As with all programming scripts, the best place to start is
with a basic ‘Hello World’.

And here we are putting
words into Steve’s mouth!

888Decmbr 2 rDm019dr09onsta December 2019 LXF257 91


Hacking Minecraft CODING ACADEMY


GUIZERO


didearlier,we’llstartoffwitha“HelloWorld!”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 traditional 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:
forlineinmcScript.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:
withopen(“script.txt”,”w”)asmcScript:
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
automatically, which is useful.

Combining chatbots with other projects
Where this gets really interesting 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!

If you’re looking to take your coding to the next level, get a copy of
LXF252 (see page 66 for back issues) and combine everything we’ve
done today with hacking and chatbots with the graphical user
interface we developed for Minecraft in that issue.
Using GUIZero, we could develop a visual interface for connecting
to another player’s Minecraft world, with buttons for spawning items
on a whim. For example we could supply our friends with diamond
pickaxes and armour at the push of a button, thus essentially turning
our fun project into a fully fledged admin tool for a multiplayer server.
To start you off:
def drawDiamond():
playerPos = mc.player.getPos()
mc.setBlock(playerPos.x, playerPos.y + 1, playerPos.z-1, 57)
mc.postToChat(“Diamond granted to player.“)
giveDiamond = PushButton(app, command=drawDiamond,
text=”Diamond”, width=10, grid=[1,2])

MINE US FOR MORE TUTORIALS! Subscribe now at http://bit.ly/LinuxFormat


As with all programming scripts, the best place to start is
with a basic ‘Hello World’.

And here we are putting
words into Steve’s mouth!
Free download pdf