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