The nice people at Mojang, who write Minecraft, also provide class libraries in Java and Python to simplify interactions with the game, which are in the ~/mcpi/api/[java/python] directories.
Create a directory for the program
mkdir ~/minecraft-api
Copy the python class library from minecraft to the programs directory
cp -r ~/mcpi/api/python/mcpi ~/minecraft-api/minecraft
Create minecraft-api.py python program
nano ~/minecraft-api/minecraft-api.py
or open Idle and save minecraft-api.py to the minecraft-api directory
Code
#www.stuffaboutcode.com
#Raspberry Pi, Minecraft API - the basics
#import minecraft block module
import minecraft.block as block
#import time, so delays can be used
import time
if __name__ == "__main__":
time.sleep(2)
#Connect to minecraft by creating the minecraft object
# - minecraft needs to be running and in a game
mc = minecraft.Minecraft.create()
#Post a message to the minecraft chat window
mc.postToChat("Hi, Minecraft API, the basics, what can you do? ")
time.sleep(5)
#Raspberry Pi, Minecraft API - the basics
#import the minecraft.py module from the minecraft directory
import minecraft.minecraft as minecraft#import minecraft block module
import minecraft.block as block
#import time, so delays can be used
import time
if __name__ == "__main__":
time.sleep(2)
#Connect to minecraft by creating the minecraft object
# - minecraft needs to be running and in a game
mc = minecraft.Minecraft.create()
#Post a message to the minecraft chat window
mc.postToChat("Hi, Minecraft API, the basics, what can you do? ")
time.sleep(5)
#Find out your players position
playerPos = mc.player.getPos()
mc.postToChat("Find your position - its x=" + str(playerPos.x) + ", y=" + str(playerPos.y) + ", z=" + str(playerPos.z))
time.sleep(5)
#Using your players position
# - the players position is an x,y,z coordinate of floats (e.g. 23.59,12.00,-45.32)
# - in order to use the players position in other commands we need integers (e.g. 23,12,-45)
# - so round the players position
# - the Vec3 object is part of the minecraft class library
playerPos = minecraft.Vec3(int(playerPos.x), int(playerPos.y), int(playerPos.z))
#Changing your players position
mc.postToChat("Move your player - 30 blocks UP!")
time.sleep(2)
mc.player.setPos(playerPos.x,playerPos.y + 30,playerPos.z)
# - wait for you to fall!
time.sleep(5)
#Interacting with a block
# - get the type block directly below you
blockType = mc.getBlock(playerPos.x,playerPos.y - 1,playerPos.z)
mc.postToChat("Interact with blocks - the block below you is of type - " + str(blockType))
time.sleep(5)
# - change the block below you to wood planks
mc.setBlock(playerPos.x,playerPos.y-1,playerPos.z,block.WOOD_PLANKS)
mc.postToChat("Change blocks - the block below you is now wood planks")
time.sleep(5)
#Creating many blocks
mc.postToChat("Create blocks - making a diamond tower")
# - loop 20 times
for up in range(0, 20):
mc.setBlock(playerPos.x + 1, playerPos.y + up, playerPos.z, block.DIAMOND_BLOCK)
time.sleep(2)
# - put you on top of the tower
mc.postToChat("Dont look down, because Im putting you on top of it!")
time.sleep(1)
mc.player.setPos(playerPos.x + 1, playerPos.y + 20, playerPos.z)
time.sleep(5)
mc.postToChat("www.stuffaboutcode.com")
Run
Note - minecraft must be running and you must be in a game
python ~/minecraft-api/minecraft-api.py
or if using Idle, click Run Module
Performance (on pre-release 0.1)
If you are using the pre-release (version 0.1) of Minecraft you will probably notice that the performance of the API is pretty slow, one of the key reasons for this is that the connection module supplied by mojang, prints the commands sent to the server, which is really useful for debugging but VERY slow - you can turn this off by modifying the connection.py module.
nano ~/minecraft-api/minecraft/connection.py
Change the send function and comment out the 2 print statements, so it looks like this:
def send(self, f, *data):
"""Sends data. Note that a trailing newline '\n' is added here"""
s = "%s(%s)\n"%(f, flatten_parameters(data))
#print "f,data:",f,data
#print "s",s
self.drain()
self.lastSent = s
self.socket.sendall(s)
"""Sends data. Note that a trailing newline '\n' is added here"""
s = "%s(%s)\n"%(f, flatten_parameters(data))
#print "f,data:",f,data
#print "s",s
self.drain()
self.lastSent = s
self.socket.sendall(s)
Check out my minecraft posts, such as a game of Hide and Seek in Minecraft, an Auto Bridge, no matter where you walk a bridge will always be under your feet or a Massive Analogue Clock, big enough so you can walk on the arms.
Great post on the API. Just got hold of the leaked version myself, so should be interesting to see what you can do with it so far!
ReplyDeleteGlad you found it useful
DeleteReally nice article, Martin. I used it as inspiration to get going on a Ruby version of the API.
ReplyDeleteI've just released a first cut of a ruby API for the minecraft raspberry pi. Please check it out, report issues and contribute! :-)
https://github.com/nhajratw/minecraft_api
It looks like in 0.1.1 they have commented out the 2 print commands already on connection.py.
ReplyDeleteI went in to do it hoping for a bit more performance to find they were already done.
Glad I didn't use it before this was done.
I made bindings for Smalltalk. And a rainbow - different from yours, but you gave me the idea :)
ReplyDeletehttp://croquetweak.blogspot.com/2013/02/smalltalk-bindings-for-minecraft-pi.html
Hi, I was wondering if there was an official documentation for the MC api? Thanks and keep up the great work!
ReplyDeleteThere is some basic documentation installed alongside MCPi. Take a look in the ~/mcpi/api directory they is a file called mcpi_protocol_spec.txt
DeleteAwesome Thank you!
DeleteHi ive been having problems using the API, I wrote a program with python to light an led based on what type of block is underneath the player but i have been unable to get it to run everytime i run it python gives me an error that says no module named minecraft.minecraft ... what do i have to do to install the module ? Thanks.
ReplyDeleteHave you copied the python api class library from the mcpi directory to your programs directory?
DeleteIf you have installed minecraft pi edition at ~/mcpi, you could use the following command
cp -r ~/mcpi/api/python/mcpi ~/yourProgramsDirectory/minecraft
Are you by chance using Python 3 (or IDLE 3)? If so switch to python 2 (or just IDLE)
DeleteYour problem is that you wrote
Deleteminecraft.minecraft.create() instead of
minecraft.Minecraft.create ()
Just a question:
ReplyDeleteWhen I try to start minecraft-pi, the message
"file or directory not found" appears
I have Linux (uname -a)
Linux raspberrypi 3.2.27+ #250 PREEMPT Thu Oct 18 19:03:02 BST 2012 armv6l GNU/Linux
and saw, that file minecraft-pi shows
file minecraft-pi
minecraft-pi: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x7f5a7cece83092451a55d83790b260ab639e59e9, stripped
is it because mincraft-pi is compiled against Linux 2.6.26 ?
(I have OpenJDK RTE 1.7.0_03)
Any help is appricated. Thanks in advance.
/PiT
Oh yeah, you don't type raspberry-pi. I used to do that. Okay, first you have to decompress it. You can right-click the zip and click extract here. Then you do run "cd mcpi" (no apostrophes), and then you type ./minecraft-api. (No period at the end)
DeleteIt might be that you wrote
Deleteminecraft.minecraft.create() instead of
minecraft.Minecraft.create ()
Okay, I did exactly what you said here. I tried reseting it, everything. Here is the error I get:
ReplyDeleteTraceback (most recent call last):
File '/home/pi/minecraft-api/minecraft-api.py', line 1, in
import minecraft.minecraft as minecraft
File '/home/pi/minecraft-api/minecraft/minecraft.py', line 1 in
from connection import Connection
ImportError: No module named connection
And this is the code I have in my minecraft-api.py:
import minecraft.minecraft as minecraft
import minecraft.block as block
import time
if __name__ == "__main__":
time.sleep(2)
mc = minecraft.Minecraft.create()
mc.postToChat("This is a test")
time.sleep(5)
Its sounds like you are missing some files, connection.py at least from the minecraft python api library. You should have a directory in your minecraft-api directory called minecraft, this is the python api library and it should have been copied from the minecraft game directory mcpi/api/python/mcpi. You need all the files from this directory as they are all used to interact with minecraft.
DeleteHmm... I have a file in my home folder called "minecraft-api" (no apostrophes), and in it is a folder called minecraft. Also in the minecraft-api folder there is a python file called minecraft-api.py. Inside the minecraft folder there are the copied libraries, the ones I copied when I ran the "cp -r mcpi/python/api/mcpi minecraft-api/minecraft". Don't understand whats wrong.
DeleteOh yeah, I also tried to re-install minecraft pi, including the hidden .minecraft folder. Still didn't work. Even reset my Pi. Doesn't work for some reason.
DeleteIm pretty sure you are getting an error because you are missing files or at least the connection.py file from the minecraft directory in your minecraft-api directory. Have a look in the minecraft-api/minecraft directory it should have all the files that appear in the mcpi/api/python/mcpi directory. I.e. they should have all the same files.
DeleteOkay, I'll do that. But I was wondering, do the files change when Minecraft Pi is running? Cause I wanna check the files on my computer, see if they match.
DeleteThe mcpi python files are the same as the ones in my minecraft-api/minecraft folder. Except, there seems to be a __pycache__ folder in my minecraft-api/minecraft folder. Wonder what that does.
DeleteThe pycache file is nothing to worry about. I am out of ideas. I would recommend you start again, create a new directory for your program minecraft-api2 maybe and copy the python library again.
DeleteYeah, I'm out of ideas too... I'll just have to try again until it works.
DeleteThis comment has been removed by the author.
DeleteWhoops, there is a : right after the while line.
DeleteI finally worked out why you originally had problems... I found out by mistake, when I accidentally used IDLE 3 and python 3, rather than python 2 and IDLE.
DeleteIt seems there is some incompatibility, I'll work out what it is one day!
I'm getting the same 'No module named connection' error after updating from the prerelease version. All the files seam to in place. I will try to do some debugging later.
ReplyDeleteI'm guessing that there is a bug with the newest version. I hope they fix it.
DeleteJosh - I think you are experiencing this problem due to using python 3 (i.e. IDLE 3) rather than python 2 (i.e. IDLE).
DeleteThat makes sense. I was using Idle, and when I switched to a different text editor it worked again.
DeleteHey, are you still going to take a video of my program? It's okay if you can't, I think I might be able to do it myself. :)
DeleteI will, it might be a next week though, busy times. Get a video done though, get it on youtube, stick a link on MCPI or Raspberry PI sub-reddit's on reddit.com. See what people think.
DeleteOh, well then that's okay. Thanks anyway!
DeleteIts definetly not a bug with minecraft, its works fine on my pi. If you can zip up your code directory and send it to me ill happily debug and test
ReplyDeleteSure, I'll do that. Mind if I got your email to send the file to, or do you want to use some other way?
DeleteGot it all zipped up. I included the minecraft-api folder and the mcpi folder.
DeleteOkay, here is the .zip file:
Deletehttp://pancake.io/1371/minecraft-pi-files.zip
Ok, well your code looks ok and everything looks like its in the right structure, I cant test it right now as Im on holiday and dont have a pi with me, but I'll test it when I get home.
DeleteIn the mean time, Im wonder whether it might be security issue, i.e. your program doesnt have permission to read the minecraft directory, this might be the case if you installed minecraft using sudo.
So either try running your minecraft-api program as sudo, i.e. sudo python minecraft-api.py or run chown against your -minecraft-api directory changing all files ownership to your user (probably pi) i.e. cd minecraft-api then sudo chown -R pi *
Whoa. All of a sudden, it works again. Maybe these are the two reasons why:
DeleteI had my internet off. Plugged it in.
Did the cp command while minecraft was running.
Maybe cause I ran sudo this time. Though I don't remember making the file or directory with root...
DeleteCan you have a go and see whether its the need for network connection or sudo to make it work?
DeleteI really can't tell what the problem was. Sorry. Maybe though it was doing the cp command while minecraft was running.
DeleteThe reason I haven't responded before is because I have been very busy making a program. It is really cool. It is an arena. First, it generates the arena. Next, it places you into it. Then, blocks on the floor of the arena start disappearing and you have to try not to fall. If you do fall, it will teleport you onto a platform and displays your score.
Mind if you take a video of my program? I don't know how to screen video on Raspberry Pi.
It sounds really good, great concept as well, I love a game in a game. Happy to record it for you, but I dont do anything hitech I just put a camera on a tripod in front of the screen. If you've got a digi camera just mount it on something, a book a box anything and just record.
DeleteMind if I write about it on stuffaboutcode
I wouldn't mind if you wrote about it at all! :D
DeleteI have been adding on to it, so it's more like a 5-in-one program. Two games in it, too. I'll get the link for the file soon.
Wow, you use a camera for that? It really looked like a screencast. :)
Okay, here is the latest version of my program:
Deletehttp://1371.pancakeapps.com/api.py
However, the dodgegame is not entirely complete. I only made it yesterday. But it does work, there are just a few extra features I wanted to add to it.
Thanks!
Hi Nicolas,
DeleteI hope you did not mind me trying out your code. I got an interesting error. In your line 6 you are getting the PlayerEntityIds; however minecraft.py gives me (in line 151) following error:
ValueError: invalid literal for int() with base 10: ''
Have tried to solve it with changing int into float or eval but no luck. Did you get this error too?
Hi me again,
DeleteSolved it by making your line 6 a comment line (#). Turned out you are not using the variable allPlayers anyway.
Arena game is really nice. Good graphs. Only on first try I put myself in one corner and it took until 192 to fall. So GOLD right away. :-)
Horse if funny! Rest I did not try yet.
Well done!!
Yeah, I was going to use that so I can control PE players. Didn't work, and I had forgotten to remove it. And I don't mind you using the code at all! Just to let people know, you do have to delete the allPlayers variable. I kinda had to erase some of the testing stuff that didn't work (I don't delete the parts the don't work, even when I don't try and fix 'em), and I added a few things. I just didn't test anything.
DeleteBTW, you can do /help for all the commands.
Okay, I updated the file. The program works fine now!
DeleteOh yeah, you can also use the program to chat. Just type in something besides a command, and it will display it on Minecraft Pi.
Just updated it again. Do /? or /help for the newest commands.
DeleteHi Nicholas, sorry its take me a while but I got round to looking at your program, very impressive, Ive video'd it and its uploading to youtube as we speak - plus a blog post at http://www.stuffaboutcode.com/2013/05/minecraft-pi-edition-2-games-by.html
DeleteI am going to try and build Something which will be HUGE!!!! but i have no idea of how to do it... ._. can you please make a video that explains how to install, run...ETC.
ReplyDeleteThank you and i believe that it is a job well done!!!
Im a bit short on time to make a video right now ill pit it on the list to do though.
DeleteIn the meantime you might find this post useful which has install instructions and a walk through on how to get started.
http://www.stuffaboutcode.com/2013/04/minecraft-pi-edition-api-tutorial.html
Thank you Martin!!! :)
DeleteI am having problem to run minecraft-api.py. I receiving an error: Socket error in
ReplyDeleteline mc = minecraft.Minecraft.create()
I run server on the same rasberry-pi. and it is in game. But i having connection error.
May be I should use other server then on rasberry pi?
Hi, your getting the error because your program cant connect to minecraft. When you say you are running the server, do you mean minecraft pi edition? Or are you running a full minecraft server? You need to start up minecraft pi edition, go into a game and then run your program. It doesnt matter that they are running on the same pi.
DeleteI am being lazy by asking, but can I use what you are here with the Minecraft server here http://www.instructables.com/id/Raspberry-Pi-Minecraft-Server/?ALLSTEPS
ReplyDeleteYou can, but you will need to install the bukkit plugin 'raspberry juice'. Check out my 'recipe' for creating a raspberry pi minecraft server.
DeleteJust a question... Why do you have to write
ReplyDeleteif __name__=__main__:???
because i never write this and my scripts work fine!
You don't have to write it, particularly in a script just for yourself. But it serves two purposes: For one, it indicates to the reader where the "main" routine actually starts. Secondly, if you define functions in your file, this lets you include the file from another one without having the "main" part execute, so you only get the function definitions.
DeleteHello all. Total noob here. My son and i have been trying the real basics of tutorial but we get stuck pretty early on.
ReplyDeleteKeep getting:
AttributeError: 'module' object has no attribute 'minecraft'
We're inputting
import minecraft as minecraft
mc = minecraft.minecraft.creat()
mc.postToChat ("Hello Minecraft!")
import minecraft as minecraft
Our Python program file is in the correct directroy (minecraft.magpi) and minecraft is running.
Could it be:
Deletemc = minecraft.minecraft.creat()
should be:
mc = minecraft.Minecraft.create()
Note the capital M (python is case sensitive so upper and lower case letters make a difference and the missing e from create
I'm running on an up to data Pi, running "cat /opt/minecraft-pi/VERSION.txt shows "0.1.1 alpha". Is this the right version ?
ReplyDeleteIt's just that none of the new player getDirection(*), getPitch(), or getRotation() work - as if I'm running an older API version.
Has this API now been abandoned ?
DeleteYeah, Mojang released it and hasn't updated it in years. Nor are there plans to update it in the future; it's meant as a learning tool anyway, to get you started with general programming.
DeleteOK, so what's the new alternative?
ReplyDeleteWell, there isn't one. This API was never really meant to be changed; I don't see much else that needs to be added. And again, it's a learning tool, and not something like the Minecraft modding API for PC.
DeleteHowever, Martin is working on a Spigot plugin for those who have PC Minecraft ($27 USD), which uses the same API as MC:PI with a few additions.
http://dev.bukkit.org/bukkit-plugins/raspberryjuice/