When you write a script to interact with minecraft, you’ll likely have to deal with the “coordinate system”. This means if you want to understand where the player is or where to set a block, you need to represent everything with a X, Y and Z coordinate.
What these mean is:
- A value of “0,0,0” or “x=0, y=0, z=0” means the player spawn location. Everything is relative from here.
- X = east/west. a positive number such as 5 means 5 blocks east of the spawn location. -5 means 5 blocks west of the spawn location.
- Z = north/south. As above, a positive number such as 5 means 5 blocks north. -5 means 5 blocks south…
- Y = up/down. Up is positive, Down is negative. 5 is 5 blocks above, -5 is 5 blocks below.
Here’s an image showing the relationship between these three values.
To help you understand this better, I’ve created a script “brooksc_findpos.py”. Source is further below, and it’s also on github here.
What this script does is:
- Flattens out the territory (credit to snowbound)
- Puts a diamond block at 0,0,0 so it’s easy for you to see where this is
- Once a second it sends to the chat window what your location is
here’s an example of what it outputs:
In a future post we’ll go into more detail on how to make use of all of this…
One final comment — You may notice that minecraft uses a floating point value for x, y and z. That is x may equal 0.0555, z=1.342, y=4.23. In this example — that’s the same as x=0, z=1, y=4. All this precision isn’t necessary — so the script just prints out the integer version (so you don’t see the decimal point or fraction)
Here’s the code:
#!/usr/bin/env python #import the minecraft.py module from the minecraft directory import mcpi.minecraft as minecraft #import minecraft block module import mcpi.block as block #import time, so delays can be used import time import server if __name__ == "__main__": mc = minecraft.Minecraft.create(server.address) mc.postToChat("Flattening surface") mc.setBlocks(-128,0,-128,128,64,128,0) mc.setBlocks(-128,0,-128,128,-64,128,block.SANDSTONE.id) mc.postToChat("Putting a diamong block at 0,0,0") mc.setBlock(0,0,0,block.DIAMOND_BLOCK.id) while True: #Find out your players position playerPos = mc.player.getPos() mc.postToChat("Find your position - its x=%s z=%s y=%s" % (int(playerPos.x), int(playerPos.z), int(playerPos.y))) time.sleep(1)