Author: Tim Cummings

Python in Minecraft 4 – Creating a solid tunnel

Our next exercise it to create a tunnel of glass filled with air. Glass is useful because it is transparent. To start with the tunnel will be horizontal heading east which means the x coordinate will be increasing and the y and z coordinates will keep constant values. We could use the setBlocks command for the entire length of the tunnel. However we want to keep the code flexible enough that later on the tunnel could start ascending or descending, i.e. the y coordinate could start increasing or decreasing. The profile of the tunnel which will be a wall of glass 5 blocks across and 7 blocks high. Here is the code to create a profile of the tunnel for one value of the x coordinate.

nano tunnelprofile.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
x = -200
y = 72
z = -222
mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)

python3 tunnelprofile.py

A loop can be added which will draw the profile of the tunnel over and over again for a range of different x values. Note how important indentation is in python. I am indenting 4 spaces when in a loop.

nano solidglasstunnel.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
xmin = -224
xmax = -200
y = 72
z = -222
for x in range(xmin,xmax+1):
    mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)

python3 solidglasstunnel.py

Categories: CoderDojo

Python in Minecraft 3 – Placing many blocks at once

The previous example showed that python scripts could quickly become very long if each block had to be set individually. Fortunately there is the setBlocks command which can set many blocks at once. It also has two forms.

mc.setBlocks(x1,y1,z1,x2,y2,z2,block)
mc.setBlocks(x1,y1,z1,x2,y2,z2,block.id,variation)

The minecraft coordinates (x1,y1,z1) represent one corner of the rectangular prism about to be set. The minecraft coordinates (x2,y2,z2) represent the diagonally opposite corner. The previous example can be rewritten to use the setBlocks command. The glass is set first otherwise it would overwrite the setting of the middle block. In this example the middle block is air which is equivalent to there being no block.

nano placeglassblocks.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
mc.setBlocks(-220,72,-223,-218,74,-221,block.GLASS)
mc.setBlock(-219,73,-222, block.AIR)

python3 placeglassblocks.py

Categories: CoderDojo

Python in Minecraft 2 – Placing many blocks individually

In the last exercise we placed a wool block with variation 1. If you want to know which blocks you can place, check out the reference material at

http://www.stuffaboutcode.com/p/minecraft-api-reference.html

In the next example, the same wool block is placed and glass is placed all around it. Glass doesn’t have any variations so we are using the first form of the setBlock command

nano placeglassaroundwool.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
mc.setBlock(-219,73,-222, block.WOOL.id, 1)
mc.setBlock(-220,72,-223, block.GLASS)
mc.setBlock(-220,72,-222, block.GLASS)
mc.setBlock(-220,72,-221, block.GLASS)
mc.setBlock(-220,73,-223, block.GLASS)
mc.setBlock(-220,73,-222, block.GLASS)
mc.setBlock(-220,73,-221, block.GLASS)
mc.setBlock(-220,74,-223, block.GLASS)
mc.setBlock(-220,74,-222, block.GLASS)
mc.setBlock(-220,74,-221, block.GLASS)
mc.setBlock(-219,72,-223, block.GLASS)
mc.setBlock(-219,72,-222, block.GLASS)
mc.setBlock(-219,72,-221, block.GLASS)
mc.setBlock(-219,73,-223, block.GLASS)
mc.setBlock(-219,73,-221, block.GLASS)
mc.setBlock(-219,74,-223, block.GLASS)
mc.setBlock(-219,74,-222, block.GLASS)
mc.setBlock(-219,74,-221, block.GLASS)
mc.setBlock(-218,72,-223, block.GLASS)
mc.setBlock(-218,72,-222, block.GLASS)
mc.setBlock(-218,72,-221, block.GLASS)
mc.setBlock(-218,73,-223, block.GLASS)
mc.setBlock(-218,73,-222, block.GLASS)
mc.setBlock(-218,73,-221, block.GLASS)
mc.setBlock(-218,74,-223, block.GLASS)
mc.setBlock(-218,74,-222, block.GLASS)
mc.setBlock(-218,74,-221, block.GLASS)

Run this python script to see it build in minecraft

python3 placeglassaroundwool.py

Categories: CoderDojo

Python in Minecraft 1 – Placing a block

Linking to other servers

In your first script, mychat.py, you created a link to the minecraft server on the local host using the command

mc = minecraft.Minecraft.create()

If you want to create a link to a minecraft server on another host, provide the IP address of the minecraft server to the create function.

mc = minecraft.Minecraft.create("192.168.17.50")

Setting a block

This exercise will set a block at a position specified by (x,y,z) coordinates in the minceraft world. We will use the setBlock command to specify the position and type of the block. The setBlock command can be used as either

mc.setBlock(x,y,z,block)

or

mc.setBlock(x,y,z,block.id,variation)

In the following example a wool block with colour orange (variation 1 for wool) is placed at x=-219, y=73, z=-222. This example uses the nano text editor which available on Mac and Linux and easier to use than vi. It has reminders at the bottom of the screen of the special key functions. To save type ctrl-o. To exit type ctrl-x.

nano placewool.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
mc.setBlock(-219,73,-222, block.WOOL.id, 1)

Run it while the server is running. The command will be

python placewool.py

If you have python 2 and python 3 on the same computer (eg Mac or Linux) then the command is most likely

python3 placewool.py

In minecraft, pressing F3 will let you see your coordinates so you can navigate (or teleport) to the coordinates to see the wool block you have placed.

Categories: CoderDojo

CoderDojo – Minecraft with Python – setup

Minecraft Pi on the Raspberry Pi can be controlled using python scripts. Here we will set up a Mac, Linux or Windows PC to achieve the same functionality by using the RaspberryJuice plugin with the Spigot Minecraft server.

Requirements

Installation

Installation of Java, Git and Minecraft is straight forward following instructions from sites listed above. Installing Git provides a bash shell for Windows.

Python installation

When downloading python, choose Python 3 not Python 2.

When installing python, tick the check box to add python to the path. This makes it easier to run python from the command line, although you will normally run python by starting the application called IDLE. Then click on “Install Now”

To run python on the command line, the command is python3 on Mac and Linux and python on Windows. I will use python3 in these instructions.

Windows only – python installation

To make python3 work in the bash shell on windows, start Git-Bash and type the following command:

echo "alias python3='winpty python.exe'" >> .bashrc

The winpty is required to enable python interactive interpreter to work in git-bash the same way it works in Windows command prompt, so this alias is providing two solutions in one line. Now quit git-bash (by typing exit). Restart git-bash and type python3. You should see something like the following:

$ python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you forgot to click the check box to add python to the path when installing python then it can be added later by going to Control Panel > Uninstall a program and double clicking on the Python program which will be called Python 3.6.2 (64-bit) or similar. Select “Modify” to change your original setup. You don’t need to change the Optional Features so click “Next”. In the Advanced Options click the checkbox now called “Add Python to environment variables” and then click on “Install”.

Spigot minecraft server installation using bash shell

To install Spigot server, use the installer caller BuildTools.jar available from https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar . Download this file and put it in a folder you want to install the Spigot minecraft folder, then run it using java.

These instructions show how to install using bash shell. You don’t have to use bash shell, but by using bash shell the same instructions work for all platforms. Bash shell is available on Mac OS X (Terminal), Linux (command line or Terminal) or Windows (Git-bash is available after you installed git above).

mkdir minecraft-spigot
mv Downloads/BuildTools.jar minecraft-spigot/
cd minecraft-spigot
java -jar BuildTools.jar

Now edit the file eula.txt and change eula=false to eula=true . This is to acknowledge that you agree with the EULA which is necessary to run the server. You can do this with a text editor from the GUI such as Windows NotePad or Mac TextEdit. Alternatively do it from the command line using vi text editor

vi eula.txt

The vi commands are cryptic. When you start vi you will be in command mode. Use the arrow keys to move the cursor to the line you want to change. Then press the [a] key which is the command to put vi in edit (append) mode. In edit mode you can move the cursor to the word false, delete it with the [delete] key and type in true. To get out of edit mode and back into command mode, press the [ESC] key. You now have to issue the command to write the changed file, and quit vi, which is pressing the following four keys [:][w][q][Enter] .

Return to original directory ready for next step.

cd ..

Install RaspberryJuice

You can get the RaspberryJuice plugin from https://github.com/zhuowei/RaspberryJuice/ using Git.

git clone https://github.com/zhuowei/RaspberryJuice.git
mkdir -p minecraft-spigot/plugins
cp RaspberryJuice/jars/RaspberryJuice-1.9.jar minecraft-spigot/plugins/

Run Spigot Server

Using Terminal or Command Prompt from the minecraft-spigot folder. Your spigot version number may be different.

cd minecraft-spigot
java -jar spigot-1.12.jar

To stop the server, type

stop

as a server command.

You should now have a configuration file in the RaspberryJuice folder in the plugins folder called config.yml. I like to edit that file and change the

location: RELATIVE
to
location: ABSOLUTE

RELATIVE makes RaspberryJuice behave the same way as Python on Minecraft Pi which uses coordinates relative to the spawn point. However on PCs it is easier to use ABSOLUTE coordinates which equate to the coordinates visible using F3 in Minecraft.

To prepare for the next section, change back up a directory to where we first started.

cd ..
Python mcpi module for RaspberryJuice

To write python scripts you need to install the mcpi module. You can choose the original one, exactly as supplied for Minecraft Pi or the modded one which adds some additional commands. I prefer the modded one. They are included in the files you cloned from github.

The following command tells you where user site-packages should be installed.

python3 -m site --user-site

Use this information to link to the module in the git repository you downloaded. This has to be done for each user on computer who will want to run python scripts.

Mac or Linux using Bash

STARTDIR=`pwd`
PKGDIR=`python3 -m site --user-site`
mkdir -p "$PKGDIR"
cd "$PKGDIR"
ln -s "${STARTDIR}/RaspberryJuice/src/main/resources/mcpi/api/python/modded/mcpi"
cd "$STARTDIR"

Windows command prompt – run as administrator

for /f %a in ('python -m site --user-site') do set PKGDIR=%a
mkdir %PKGDIR%
cd %PKGDIR%
mklink /D mcpi %HOMEPATH%\RaspberryJuice\src\main\resources\mcpi\api\python\modded\mcpi
cd %HOMEPATH%

Now start the spigot server

cd minecraft-spigot
java -jar spigot-1.12.jar

In another bash shell, create a python script to send a chat message. Here is command using vi.

vi mychat.py
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("hello")

Run this script and you should see your message in the first bash shell.

python3 mychat.py
Troubleshooting for Windows

The following tests should be done in Git-Bash:

1. Type python --version. It should return “Python 3.6.2” or similar. If it says “bash: python: command not found” then you have not ticked “Add python to PATH” when installing. See Windows only python installation instructions above for remedy.

2. Type python3. It should start the python interpreter shell ending with >>>. If you not then you haven’t created the .bashrc file correctly. See Windows only python installation instructions. You can delete the existing .bashrc file in your home directory and try creating it again. Be extremely careful with spaces and using the correct type of quotation marks. It is best if you can copy and paste directly from this web site into Git-Bash. To paste in Git-Bash, right click the mouse and select “Paste”.

3. Once you are in the python interpreter shell, type import mcpi.minecraft. If there is an error then there is a problem with the link you created in “Python mcpi module for RaspberryJuice”. Try creating it again.

Categories: CoderDojo, Minecraft