Finding your position…

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.

 

Screen Shot 2013-02-23 at 8.56.55 AM

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:

  1. Flattens out the territory (credit to snowbound)
  2. Puts a diamond block at 0,0,0 so it’s easy for you to see where this is
  3. Once a second it sends to the chat window what your location is

here’s an example of what it outputs:

Screen Shot 2013-02-22 at 6.24.38 PM

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)
Advertisements

One thought on “Finding your position…

  1. If the x z y co-ordinate system is confusing you, in the same way it did me, you have to get into character pronto. Minecraft’s world may appear to be all about top-down, but it’s really about viewpoint. X is east-west – following the sun; Z is for zooming north to south; and Y is about how wet you are likely to get, going down or up. Hope this translation helps some folk – running the code snippet above is really useful to put this into practice. You can fetch an updated copy which includes this snippet right now to save typing, by deleting the old gitted folder and re-running the git command again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s