How to Setup the Minecraft Python API on a Raspberry Pi

The great thing about the Raspberry Pi Edition of Minecraft is that you can manipulate the game world using Python scripts. You can move the player as well as create and destroy blocks. This allows you to create structures in seconds that would take hours to create by hand.

By repeating a script you can quickly create structures you have designed. For example you could write a script to create a tower and a section of wall and use it to quickly create a massive castle.

To configure your Pi to allow this you need to follow a few initial steps. They are fairly straight-forward and only take five minutes.

Step 1 – Install Minecraft

Please follow my Installing Minecraft on the Raspberry Pi tutorial and check the game works as expected.

Step 2 – Configure the API

The files required are now already installed but to make them a bit easier to access we are going to copy them into a new folder. So lets create it in the default home directory :

mkdir ~/mcpi-api

and then copy the files from Minecraft installation :

cp -r ~/mcpi/api/python/mcpi ~/mcpi-api/

Step 3 – API Test

Before diving in with your own scripts it is best to test everything is working ok. Navigate to the API directory created earlier :

cd ~/mcpi-api

To test your setup you can use the following simple script. It creates a cube made of stone and places you on top of it.

#!/usr/bin/python
# Import Minecraft libraries
import mcpi.minecraft as minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create()

# Get player position
pPos = mc.player.getTilePos()

mc.postToChat("API Test!")

# Change block
print "Create stone 3x3 cube"
mc.setBlocks(pPos.x-1,pPos.y,pPos.z-1,pPos.x+1,pPos.y+2,pPos.z+1,block.STONE)

print "Position player on top"
mc.player.setPos(pPos.x,pPos.y+3,pPos.z)

mc.postToChat("Move and have another go.")

You can download this script directly to your Pi using :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/minecraft/api_test.py

It can be run using :

python api_test.py

Make sure Minecraft is running before you try the test script or nothing will happen! Pressing TAB will free the mouse from the game window and let you use the LXTerminal.

When the script has run you should see a stone cube under your feet :

Minecraft API Test

Minecraft Python API Block Names Reference

For reference the following Block names are available if you want to change the script above to create a cube made of a different material :

AIR                 
STONE               
GRASS               
DIRT                
COBBLESTONE         
WOOD_PLANKS         
SAPLING             
BEDROCK             
WATER_FLOWING       
WATER               
WATER_STATIONARY    
LAVA_FLOWING        
LAVA                
LAVA_STATIONARY     
SAND                
GRAVEL              
GOLD_ORE            
IRON_ORE            
COAL_ORE            
WOOD                
LEAVES              
GLASS               
LAPIS_LAZULI_ORE    
LAPIS_LAZULI_BLOCK  
SANDSTONE           
BED                 
COBWEB              
GRASS_TALL          
WOOL                
FLOWER_YELLOW       
FLOWER_CYAN         
MUSHROOM_BROWN      
MUSHROOM_RED        
GOLD_BLOCK          
IRON_BLOCK          
STONE_SLAB_DOUBLE   
STONE_SLAB          
BRICK_BLOCK         
TNT                 
BOOKSHELF           
MOSS_STONE          
OBSIDIAN            
TORCH               
FIRE                
STAIRS_WOOD         
CHEST               
DIAMOND_ORE         
DIAMOND_BLOCK       
CRAFTING_TABLE      
FARMLAND            
FURNACE_INACTIVE    
FURNACE_ACTIVE      
DOOR_WOOD           
LADDER              
STAIRS_COBBLESTONE  
DOOR_IRON           
REDSTONE_ORE        
SNOW                
ICE                 
SNOW_BLOCK          
CACTUS              
CLAY                
SUGAR_CANE          
FENCE               
GLOWSTONE_BLOCK     
BEDROCK_INVISIBLE   
STONE_BRICK         
GLASS_PANE          
MELON               
FENCE_GATE          
GLOWING_OBSIDIAN    
NETHER_REACTOR_CORE 

Next Steps

You are now ready to create your own scripts! Exactly how complicated you make them is up to you.

This entry was posted in Minecraft, Python, Tutorials & Help and tagged , , . Bookmark the permalink.



8 Responses to How to Setup the Minecraft Python API on a Raspberry Pi

  1. tmgiewont says:

    wait i must have missed something where do you type in this code,”mkdir ~/mcpi-api” can you please tell me where to type this in thank you very much.

    • Matt says:

      It needs to be typed into a terminal window or the default command prompt after the Pi has booted. In the same way as the tutorial mentioned in Step 1.

  2. aganid says:

    When i do the first step it says no such file or directory from bash. How do i solve this?

    • Matt says:

      What command is causing the error. The “first step” is installing Minecraft. Is that the bit you are having problems with?

  3. Erik Dreyer says:

    Hi i followd your instructions, but it keeps on giving an error when I run the api_test.py, that there is no module mcpi, or that it can not find the needed modules.

  4. John.neary says:

    Hi,

    I’m following your instructions and pasted the text into my api_test file, but when i tried to run it got the following error.

    File “api_test.py”, line 4
    SyntaxError: Non-ASCII character ‘\xc2′ in file api_test.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

    I went to the site but could not really understand the problem. Could it be the way I pasted the text?

    John

  5. John.neary says:

    An update.

    Managed to get this to work by cutting and pasting the text from your version on bitbucket.

    Thanks

    John

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>