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.
2015 Update :
The latest version of Raspbian includes the installation of Minecraft and the Minecraft API. This mean you can skip Steps 1-3. Your Python scripts can be run from any directory. I strongly suggest you update your SD card with the latest Raspbian image if you don’t already have Minecraft or the API installed.
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 – Navigate To Folder
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
Step 4 – API Test
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 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.
- Keep an eye on my Minecraft category for new posts
- A full list of block names and ID numbers can be found in my Minecraft Block ID post.
- Take a look at Minecraft: Pi Edition – API Tutorial on Martin O’Hanlon’s site for a more detailed look at the range of commands available to you.
14 Comments
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.
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.
When i do the first step it says no such file or directory from bash. How do i solve this?
What command is causing the error. The “first step” is installing Minecraft. Is that the bit you are having problems with?
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.
This sounds like either the test script is not in /home/pi/mcpi-api/ directory or the api files are not.
I ran into a similar problem, and while Matt’s solution should be tried first, here are some other tips:
-Make sure that you load the program from the Terminal, and not from IDLE
-Make sure that when in the terminal first you use this command: cd ~/mcpi-api (step 3)
-If all else fails you can always delete all the minecraft files, and follow the steps again to re-download, hopefully with all the files in their proper places.
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
I also ran into the exact same problem, but easily fixed it. When copying and pasting the code, for some reason there are these little invisible characters at the start of every blank line in-between the text. Just delete these characters and you will be fine.
An update.
Managed to get this to work by cutting and pasting the text from your version on bitbucket.
Thanks
John
I do not have a folder named “mcpi” anymore.
If you are using the latest Raspbian image you can skip Steps 1-3 as Minecraft and the API are already installed. The api_test script should still work.
Hi! Could I use this version of Minecraft on the Windows PC?
No. It’s a special version written for the Pi.