#get the entity id's of the players connected to the gameentityIds = mc.getPlayerEntityIds()for entityId in entityIds: print entityId
.getPlayerEntityId(playerName)
"Get the entity id for a named player => [id:int]"

#get the entity id of a name player 'martinohanlon'entityId = mc.getPlayerEntityId("martinohanlon")print entityId
.saveCheckpoint()
"Save a checkpoint that can be used for restoring the world"

mc.saveCheckpoint()
.restoreCheckpoint()
"Restore the world state to the checkpoint"

mc.restoreCheckpoint()
.postToChat(message)
"Post a message to the game chat"

Minecraft.player

.getPos()
"Gets the player's position in the world as a Vec3 of floats (decimal numbers), if the player is in the middle of a block x.5 is returned"

#get players position as floatsplayerPos = mc.player.getPos().setPos(x,y,z)
"Moves the player to a position in the world by passing co-ordinates ([x,y,z])"

#set the players position as floatsmc.player.setPos(0.0,0.0,0.0).getTilePos()
"Gets the position of the 'tile' the player is currently on."

#get the position of the tile the players is onplayerTile = mc.player.getTilePos().setTilePos(x,y,z)
"Move the player to a tile position in the world by passing co-ordinates ([x,y,z])"

#set the position of the tile the player is onmc.player.setTilePos(0,0,0).setting(setting, status)
"Set a player setting (setting, status). keys: autojump"#change the autojump setting to Truemc.player.setting("autojump", True).getRotation()
"Get the rotational angle (0 to 360) for the player => [angle:float]"

#get the rotation of the playerangle = mc.player.getRotation()print angle.getPitch()
"Get the pitch angle (-90 to 90) for the player => [pitch:float]"

#get the pitch for the playerpitch = mc.player.getPitch()print pitch.getDirection()
"Get unit vector of x,y,z for the player's direction => [Vec3]"

.getPos(entityId)
"Gets an entities position in the world as a Vec3 of floats (decimal numbers), if the entity is in the middle of a block x.5 is returned"

#get first entity position as floatsentityPos = mc.entity.getPos(entityId).setPos(entityId,x,y,z)
"Moves the entity to a position in the world by passing co-ordinates ([x,y,z])"

#set the players position as floatsmc.player.setPos(entityId,0.0,0.0,0.0).getTilePos(entityId)
"Gets the position of the 'tile' the entity is currently on."

#get the position of the tile the entity is onentityTile = mc.entity.getTilePos(entityId).setTilePos(entityId, x,y,z)
"Move the entity to a tile position in the world by passing co-ordinates ([x,y,z])"

#set the position of the tile the entity is onmc.player.setTilePos(entityId,0,0,0)

.getRotation(entityId)
"Get the rotational angle (0 to 360) for an entity => [angle:float]"

#get the rotation of an entityangle = mc.entity.getRotation(entityId)print angle.getPitch(entityId)
"Get the pitch angle (-90 to 90) for an entity => [pitch:float]"

#get the pitch for an entitypitch = mc.entity.getPitch(entityId)print pitch.getDirection(entityId)
"Get unit vector of x,y,z for an entities direction => [Vec3]"

Hi Martin, love your work. I'm wondering if there is a method to give the direction the player is currently facing. Obviously you can use position deltas to work out where someone may be facing when on the move, but if they are stationary, a Vec3 does not give directional information. Any ideas?

Hello, Martin. I'm a Japanese Minecraft user. I'm trying to gather information about Minecraft PI and add it to the website 'Minecraft Japan Wiki'. I have ever translated this page into Japanese for personal needs and want to contribue my translation to 'Minecraft Japan Wiki'. Could I ask you to permit my publishing the translation of this page on 'Minecraft Japan Wiki'? I'm looking forward to your reply.

First of all, congratulations for the book!! Nice stuff to introduce kids in programming. I have a question, is it possible to capture the keys pressed in minecraft? The idea is not to have to run the program in order to perform some action but execute it in the beginning (while True:) and a listener to fire some actions when a key is pressed, Thanks a lot!

Its not possible to 'ask' Minecraft what keys have been pressed but you can ask the operating system (windows, linux, mac os) etc and then recreate to those keypress events. It depends on what OS you are using, but if you were using windows the PyHook module would help. http://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/. Have a google.

Over the last two days, I've implemented most of the Raspberry Juice/PI protocol within a Forge mod for singleplayer desktop Minecraft (Raspberry Jam Mod). The most notable gap is the event handling right now, but you can draw shapes with python and have fun.

I'm pretty much new for Minecraft, and a fortiori to Minecraft modding, and I did this for my daughter to encourage her to learn python. I've tested this with 1.8 on Windows, but I assume it should work on Linux and OSX just as well.

Hi Alexander, looks good, another guy put a post of the adventures in minecraft forum who had done the same http://www.stuffaboutcode.com/p/adventures-in-minecraft-forum.html?place=msg%2Fadventures-in-minecraft-forum%2Fchq0v6Ka1zA%2FOxvwTFUWCXEJ

thanks for all the examples and API doc. Any idea how to control the range of a MCPI world? It is always 256 x 256 blocks in the xz - plane. But sometimes it runs from -128 to 127, sometimes anything else like -184 to 71. Is there a way to retrieve the range of the current world?

Hi Martin, I am new to this. Is it possible to write texts in raspberry minecraft standing sign object? Could you give a hint how to write the text in minecraft sign board object?. Thank you in advance.Gunhas

First and foremost, thanks a million for this guide, use it almost every day! We just found out you can also change the orientation (North, East, South, West) of the FENCE_GATE block. Maybe add that to the chest, ladders, and furnace section?

In example, if Minecraft.player would have setRotation, users could program a player rotation 90 degrees each second, or another action like moveForward to do a loop to move 10 blocks, or use pickaxe get a block or build a small vegetable garden...

Interesting because I didnt find something similar, and I would like to do some macros to mine, and build bridges using materials in survive mode, not only creating in creative mode. It would be very powerful to learn scripting and make cool things.

Hey Martin! I love all the minecraft programming information you provide! I have been able to make so many cool programs with it. I was wondering if there was a way to find the block below the player to do an action? Thanks!

Thanks for Repling Martin! How do you find out what block is below the player. I am making a program where it will print different messages to the chat depending what block is below the player. Thanks Again!

Hello,I'm trying to code using the minecraft API. my config is that the minecraft server is running on a raspberry PI 2 but the game (and player) is started from a mac connected on the same LAN. I try to do simple getBlock calls but I receive an error : File "/home/pi/ServeurMinecraft/myScripts/mcpi/minecraft.py", line 123, in getBlock return int(self.conn.sendReceive("world.getBlock", intFloor(args)))ValueError: invalid literal for int() with base 10: ''

Martin,Awesome stuff here. I've been adjusting the public API and adding a PEP8 compliant interface. Python devs will appreciate that more than the camelCase stuff.

On another note, as I'm using this to teach my daughter Python, we've discovered that we can't make torches stick to the walls. I can set torches that are pointed straight up, but when I use any other directions, the torch pops right off.I can even query a manually placed torch and get this as a response: Block(50, 4), but if I try to create such a block, it doesn't stick.

I wander why I cat get other block ID in block.py(folder MineAdventures)? like SPRUCE_WOOD_PLANK,REDSTONE.I have tried to add some block id in block.py(folder mcpi)such as[POLISHED_DIORITE=Block(1,4)],[DARK_OAK_WOOD_PLANK=Block(5,5)].But it is unworkable, when I run code like【mc.setBlock(pos.x,pos.y,pos.z,block.BIRCH_WOOD_PLANK.id)】,Minecraft still tell me I was running Oak Wood Plank. How can I perfect The block.py(mcpi) to easier to use before.

Thanks for the guide, plan to buy your book as the kids learn more.. question: is there a way to write an if loop that takes input from the keyboard... if 'w' key is pressed, drop a flower at playerPos?

getPos, getTilePos, setPos and setTilePos all appear to be relative to the player's spawn point at least in a single player world. I am going through trying to teleport my character but the coordinates I end up on are not the world's coordinates but a coordinate relative to where my character would spawn.

I'd love to be able to teleport to a specific world coordinates by cannot. Can anyone assist?

Thanks. Based on your info, I was expecting that either sleeping in a bed or calling /spawnpoint on the client would zero out the Python getTilePos(); it didn't have any effect for me.

I also assume that there's no method on the Python API to return the player's spawn point?

Am I right in saying that there's no Python code I can run to reliably print the coordinates that the user will be seeing in the F3 debug screen? If so, I'll just use mc.postToChat(pos) to let the user know where they are according to Python. That way, the user can write some simple scripts with hardcoded locations (rather then doing everything relative to their current position with mc.player.getTilePos()).

I was missing something obvious ;) I thought you were referring to a player's spawn point as I didn't know that a "world spawn point" existed. I learned about it here:http://minecraft.gamepedia.com/Spawn/Multiplayer_details

After that, I get the same info on the F3 debug screen and reported by Python's getTilePos(). No extra thought required. Nice.

Other details:If one didn't want to set the spawn point to 0 0 0 for some reason, and the spawn point is unlikely to be changed by an admin user, it would just be a one-time effort to get the required offsets for Python. That way, though, you'd need some extra Python code to help the user not have headaches. The only way to get at the world spawn point seems to be to look at level.dat (unzip then look for SpawnX, SpawnY, SpawnZ or use the "NTBExplorer" utility). There isn't a more helpful /getworldspawn on Vanilla Minecraft so it's not fair to ask Python to know the values automatically.

Hellow Martin,I did try Adventure 5 the first step to let my LED flash(LEDtest.py) on bread board, But it was useless.I dont know why.i checked it,software is ok,LED...I am not sure,if i just energize Arduino,LED shine.when i run“LEDtest.python”，LEDs was lighting，and only flashing which yellow on Arduino but not the bigger one.that is not like book what the book says.what's going on?

I was wondering how to specify which type of block you want to use in the setBlock function. I want to populate my world with tall grass except it sets the wrong type of GRASS_TALL. How do I use .data to do that?

For every block in this code, I have used the their block id. Whenever I try to put a block in that format, it brings this error message, which is why I posted my message in numbers. Im pretty sure block ID 108 is stair bricks, and I was hoping the ".3" would make them ascend in a different direction.

Hi, I am trying to use mcpi to control MC, but .getPlayerEntityId(playerName) method not working (Minecraft instance has no attribute 'getPlayerEntityId'). Did I use the wrong mcpi package? How could I get the user object by username? Thanks for your reply!

I can't seem to get mc.entity.SetPos() working correctly. I have the correct EntityID, and when I teleport the other player, I can see them in the new location. Seems good.

HOWEVER, on that user's own screen, they have not been moved! And if that user moves their character in anyway, they vanish on MY screen and go back to their original location (which it seems they never actually left).

I have the minecraft software running 1.9.I have spigot server running 1.9. I can connect to the serve via minecraft. I can control the server via spigot.I have the latest mcpi installation with python 3.5I can successfully run all the minecraft commands in python (no error output) but I don't get any results. I can connect in python and send / receive data but something's clearly not working. Any ideas?

Hi, We bought my daughter your adventures in Minecraft book at Christmas - she has the bukkit server setup along with RaspberryJuice on her laptop (using the link on the 'Wiley' website) and she is successfully following through the examples and adapting them to her own ideas (and having lots of fun with it :)), however she has tried referencing other players on her server, instead of just herself (i.e. for the geofencing example) - when others are connected to her server, the getPlayerEntityIds() noted in the list above gave an error. I updated to the latest version of the bukkit server (1.11.2) and the latest RaspberryJuice plugin (1.8), and this is now working, however there is no easy way to see which player is herself or which is others on the server, so we tried using getPlayerEntityId("PlayerName"), but this gives an error: "AttributeError: Minecraft instance has no attribute 'getPlayerEntityId'" - the list above suggests this should work with Raspberry Juice. can you advise where we might be going wrong please? - thanks...

1. Is the repo for Raspberry Juice in the previous comment the latest source for the mcpi library? Or does it exist on its own elsewhere?

2. Is the Mojang API to Minecraft documented somewhere? I've not been able to find it. I'd like to know whether it's possible to extend the work you've done here to include things like mining a block (producing a drop) instead of just setting the block to a different id. This would be useful in something like an automatic mining script.

1. Well its the latest source for the library which works with the extensions in RaspberryJuice, there are other versions, created by other people, but they probably dont work with the RaspberryJuice extensions.

2. The api was only documented (poorly) in a text file distributed with Minecraft: Pi edition, a little while ago I pulled this information together into a github repo https://github.com/martinohanlon/Minecraft-Pi-API/blob/master/api.md - this is the most complete description of the api I've seen.

Hi theren, I've just forked the https://github.com/zhuowei/RaspberryJuice project to add setEntity method to the api. You need to compile the plugin and then you can add new entities, mobs, animals, villagers with python code (https://github.com/pxai/RaspberryJuice)

I am having issues writing a program. The program is supposed to place 3 wool blocks (red, blue, and green). Then when I put a torch on red, for example, it activates a GPIO pin and turns an RGB LED red. Then if I put a torch on other colors, it will activate the LED accordingly.

I have not written the GPIO stuff in just yet, but I am testing with a loop and print. So that when I execute the code with a torch on red, it prints red, red, red... etc. If I execute the code with red and blue, it prints red, blue, red, blue... etc.

The problem is, if I place or remove torches while playing inside the world, the loop and print will not detect the change. How can I get it to recognize a change in the presence of blocks, such as torches? Ideally, I would like to place a torch on blue in-game and have my LED light up blue. Then destroy the torch and put it on red, and have the LED change to red. Any thoughts?

Hi Merlyn. You haven't posted the code that you're using to check for the torches, so it's worth checking that you understand that a torch is a whole block in the Minecraft world. Adding a torch block next to a wool block doesn't modify the wool block in any way. This is a little counter-intuitive as it looks like the torch is *attached* to the wool block.

If your wool block is at coordinates x, y, z and you're only interested in checking for torches attached to the sides of the wool block (not the top or bottom) then you should use:

# if variables find a torch above wool blocks, it will print the color.# print('color') will be later replaced to activate a GPIO pin.# for now, these are just used to test that Minecraft detects a change from# within game. Presently, for some reason it does not. It will only detect# the torches placed by mc.setBlocks(....)while True:>>>>if torchRed == torch:>>>>>>>>print('red')>>>>if torchGreen == torch:>>>>>>>>print('green')>>>>if torchBlue == torch:>>>>>>>>print('blue')

The program now works correctly. There was a block of code that checked to see if a torch was present or absent. Although this worked, it needed to be placed INSIDE the loop to be checked continuously. Below is the working code. Next step is to add GPIO functionality. Remember, >>>> means indent. However, the formatting of this post does not allow indents. So when you place it in your own code, replace >>>> with an indent and >>>>>>>> with 2 indents.

# Find player positionx, y, z = mc.player.getPos()

# Commonly used Minecraft variablesred = 14 # parameter of the wool blockgreen = 13 # parameter of the wool blockblue = 11 # parameter of the wool blockair = 0 # used to displace terrain and trees created above the platformobsidian = 49 # used to create an aesthetic border around colored wool blockswool = 35 # plain wool blocktorch = 50 # will be used as the mechanism to turn on GPIO pins in a future version of this program

# If a torch exists above a wool block, it will print the color.# print('color') will be later replaced to activate a GPIO pin.# for now, these are just used to test that Minecraft detects a change from# within game.

If you are using the full version of Minecraft (i.e. not Minecraft Pi edition) you are in luck and can use pollChatPosts() which will return you all the messages which have been posted to the chat. Unfortunately this isnt available on the Pi edition.

I had some trouble with setBlocks and setblock, but setBlock worked just fine.

I created a pillar of TNT, dirt, and Lava (stationary), hoping that the replacing the dirt with air on a game failure would result in the TNT detonating due to the falling lava, but the lava just flows down and covers the TNT. A regular block of lava just starts flowing and no explosion. Any ideas how to ignite the TNT automatically?

You dont say what platform you are using? Is it a Raspberry Pi or RaspberryJuice on a PC? If Pi, it doesnt support pollChatPosts. There are icons next to each function call which describe what system it is supported on. If you are using RaspberryJuice you will need an updated version of the python library, as it missing the pollChatPosts function so it must be pretty old.

Re TNT, I have never found a way of auto igniting it on a Pi, however on PC I have just created a redstone torch next to it.

If I need to make explosions, I tend to just create a sphere of air around the TNT which achieves the same thing.

Hi Martin, I was wondering if you had any advice as to how to best update the RaspberryJuice PC API with the Pi functionality once it's been installed on a computer. I've been replacing it piecemeal because replacing all the files seems to produce an error, but I have to think there's an easier way to go about this...

For anyone looking, there's two factors to consider if the Python API isn't updating properly: have the most up-to-date RaspberryJuice plugin (mine was somehow missing after some server updates) and make sure you're using the right Python API(2.x https://github.com/zhuowei/RaspberryJuice/tree/master/src/main/resources/mcpi/api/python vs 3.x https://github.com/shawill/py3minepi).

Hello. I got a question. I made my own minecraft server with spigot and Hamachi, and I tried to connect to the server as I wrote down a code line 'mc = minecraft.Minecraft("25.--", 25565)'. It seemed like python run this code without an error message but there was nothing changed in minecraft. Do you recognize this problem? Is this because of Hamachi? Thank you in advance for your help.

There are risks with everything! Opening any port will allow connections to that port. The risk, to me, seems relatively small, you are only opening a single port, which can only talk to Minecraft. You make your own choice tho.

Thanks! But I still have two problem :1.how to give a player a permission to cheat in game?I try to change permissions.yml.But can't run appropriately.And I can't find Canarymod folder or config\worlds\default\default_NORMAL.cfg2.I decide to make a program to change weather and time in game as same as the real world,I'm confused how to change them with python?e.g.When you are living in Beijing and weather is raining,in minecraft world it is also raining.

Is there possibility to send a MC command from python code?for example to set time or change gamemode? or just set worldspawn? this could be helpfull and give more fun for kids who wants to play and learn code.

This is awesome. I am having trouble with mc.pullChatPosts(), I am trying to pull my users input, but the returned list is empty. Is there any examples on how to use this function. I'm also using the full version of mine craft and I believe to have the modded mcpi. Thxs