This program test the function mc.getPlayerEntityId in two case.
Case 1: The user "Present" that is connected to the server Case 2: The user "Absent" that is not connected to the server
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
# The Minecraft API has to be imported before it can be used
import mcpi.minecraft as minecraft
# To communicate with a running Minecraft game, you need a connection to that game.
mc = minecraft.Minecraft.create()
# Getting the list of entity (players) in the game
entityIds = mc.getPlayerEntityIds()
# For all entity (player) display their position
for entityId in entityIds:
pos = mc.entity.getPos(id=entityId)
mc.postToChat("entityId="+str(entityId)+"x="+str(pos.x) + " y="+str(pos.y) +" z="+str(pos.z))
mc.postToChat("+++ath 1")
# This should get the Id of user "Present"
PresentId = mc.getPlayerEntityId("Present")
mc.postToChat("Present="+str(PresentId))
mc.postToChat("+++ath 2")
# This should get the Id of user "Absent"
AbsentId = mc.getPlayerEntityId("Absent")
mc.postToChat("Absent="+str(AbsentId))
mc.postToChat("+++ath 3")
# END
Here is the result server side, no error, just an INFO but the "+++ath 3" is never reached:
[21:02:51 INFO]: [RaspberryJuice] Opened connection to/127.0.0.1:34183.
[21:02:51 INFO]: [RaspberryJuice] Starting input thread
[21:02:51 INFO]: [RaspberryJuice] Starting output thread!
[21:02:51 INFO]: entityId=14x=128.5 y=66.0 z=144.5
[21:02:51 INFO]: +++ath 1
[21:02:51 INFO]: Present=14
[21:02:51 INFO]: +++ath 2
[21:02:51 INFO]: [RaspberryJuice] Player [Absent] not found.
Here is the result client side where an Traceback is given for the call with the unexisting (not connected) user:
>>> %Run PresentAbsent.py
Traceback (most recent call last):
File "/home/pi/AdventuresInMinecraft-Mac/MyAdventures/PresentAbsent.py", line 26, in <module>
AbsentId = mc.getPlayerEntityId("Absent")
File "/home/pi/AdventuresInMinecraft-Mac/MyAdventures/mcpi/minecraft.py", line 185, in getPlayerEntityId
return int(self.conn.sendReceive(b"world.getPlayerId", name))
File "/home/pi/AdventuresInMinecraft-Mac/MyAdventures/mcpi/connection.py", line 63, in sendReceive
return self.receive()
File "/home/pi/AdventuresInMinecraft-Mac/MyAdventures/mcpi/connection.py", line 57, in receive
raise RequestError("%s failed"%self.lastSent.strip())
mcpi.connection.RequestError: b'world.getPlayerId(Absent)' failed
>>>
I believe a gracefull return should be provided if the name used is not connected.