Exercise 02

Creating a Single Block

In this exercise we'll learn coordinates and how to spawn unique simple blocks

Before spawning blocks we first need to understand how they disposed in Minecraft world. These images are explicites :

Coordinates in Minecraft Coordinates in Minecraft by MacFreek34570

and:

Coordinates in Minecraft 2 Coordinates in Minecraft

Using this coordinate system you can then spawn a block using the function mc.setBlock. You must give the coordinate and ID of the block. (you’ll find the list of Block ID here : TODO: Link to ressources)

Most of the time you’ll want to spawn the block near the position of the player. So you’ll need to get the Player coordinates and use it as origin.

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
#The Grass block ID is 2 :
mc.setBlock(x+1,y,z+1,2)

Which should gives you :

Coordinates in Minecraft 2 A block of grass on the ground

In this example we’ll put it just below the player :

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
#Here we will use the block ID name but we could have used 
#57 instead of block.DIAMOND_BLOCK
mc.setBlock(x,y-1,z,block.DIAMOND_BLOCK)

Which should gives you :

Coordinates in Minecraft 2 A block of diamond below you

And now above the ground a purple wool block

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
#Some blocks have subid, like Wool. Instead of 35 you can use
# 35,10 to get purple wool for instance or 35,14 for Red . 
# 35,0  or 35 will be white Wool
mc.setBlock(x,y+2,z,35,10)

Which should gives you :

Coordinates in Minecraft 2 A block of violet wool above the ground