https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6f073b40-c2a6-4319-befa-e46117b3d1ce/06-v5_finished_output.png

“Hello, World” is the most basic program we can build with any programming language. Let’s see what we can do now to make it more interesting with Python and Minecraft.

What you’ll learn

The Minecraft API lets us have access to more than just being able to post to the game chat. We can also get the position of our player in the game world, create and destroy blocks, and even detect the kinds of blocks around us.

# Hello, Minecraft! A test to post to the Minecraft world.
# Jim Christian
# CodinginMinecraft.com

# import the Minecraft module
import mcpi.minecraft as minecraft
# import the block module
import mcpi.block as block
#import the time module
import time

# connect to the game
mc = minecraft.Minecraft.create()

# set a variable for the player's position
pos = mc.player.getPos()

# convert our position coordinates from floating point to integer by rounding up
pos = minecraft.Vec3(int(pos.x), int(pos.y), int(pos.z))

# Find out which block is directly underneath the player.
whichBlock = mc.getBlock(pos.x,pos.y - 1,pos.z)

# post text to the world!
mc.postToChat("Hello world!")
# sleep for two seconds
time.sleep(2)
# post concatenated location values to chat
mc.postToChat("I'm currently at x:" + str(pos.x) + "y: " + str(pos.y) + "z: " + str(pos.z))
# sleep for two seconds
time.sleep(2)
# post concatenated text and block value to chat
mc.postToChat("The block below me is block ID:" + str(whichBlock))