Skip to content

Instantly share code, notes, and snippets.

@dglaude
Last active May 1, 2019 20:11
Show Gist options
  • Save dglaude/57b1aaf432360e998464875690ff0158 to your computer and use it in GitHub Desktop.
Save dglaude/57b1aaf432360e998464875690ff0158 to your computer and use it in GitHub Desktop.
Where is Everybody in Adventure in Minecraft

Raspberry Pi comes with a very special version of Minecraft.

That free version is bost limited and extensible because you can write a program that interact with the game. But that version is also mono player, so the Python programming API does not offer information about multiple player.

In the book Adventure in Minecraft of Martin O'Hanlon and David Whale, there are instruction to do the same thing on PC and MAC. This involve a trick to locally start a moddified version of the Minecraft server, based on Bukkit. That version include an API compatible with the one available on Raspberry Pi, that is the RaspberryJuice plugin. But because you now have a full Minecraft server, you can have multiple simultaneous connected users.

So I wrote this little piece to display the position of current players:

# 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

# The time module allows you to insert time delays
import time

# To communicate with a running Minecraft game, you need a connection to that game.
mc = minecraft.Minecraft.create()

# A game loop will make sure your game runs forever
while True:
    # delay, to prevent the Minecraft chat filling up too quickly
    time.sleep(1)

    # 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))

# END

However, I found a bug and here is a little piece of code that demonstrate that:

# 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()

mc.postToChat("+++ath 1")

# This should get the list of players connected
players= mc.getPlayerEntityIds()

mc.postToChat("+++ath 2")

# Try to display this on the chat
mc.postToChat("players="+str(players))

mc.postToChat("+++ath 3")

# END

If I run this code with a single user connected, here is the output:

[15:26:07 INFO]: XXXXXXXX[/192.168.192.120:37148] logged in with entity id 14 at ([world]111.10392722469632, 80.20000004768372, 119.8046478129002)
[15:26:33 INFO]: [RaspberryJuice] Starting input thread
[15:26:33 INFO]: [RaspberryJuice] Opened connection to/127.0.0.1:52665.
[15:26:33 INFO]: [RaspberryJuice] Starting output thread!
[15:26:33 INFO]: +++ath 1
[15:26:33 INFO]: +++ath 2
[15:26:33 INFO]: players=[14]
[15:26:33 INFO]: +++ath 3

When I run this code without any user connected to the server, here is the error I get:

[15:18:11 INFO]: [RaspberryJuice] Opened connection to/127.0.0.1:52624.
[15:18:11 INFO]: [RaspberryJuice] Starting input thread
[15:18:11 INFO]: [RaspberryJuice] Starting output thread!
[15:18:11 INFO]: +++ath 1
[15:18:11 WARN]: [RaspberryJuice] Error occured handling command
[15:18:11 WARN]: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
[15:18:11 WARN]: 	at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:824)
[15:18:11 WARN]: 	at java.lang.StringBuilder.deleteCharAt(StringBuilder.java:253)
[15:18:11 WARN]: 	at net.zhuoweizhang.raspberryjuice.RemoteSession.handleCommand(RemoteSession.java:181)
[15:18:11 WARN]: 	at net.zhuoweizhang.raspberryjuice.RemoteSession.handleLine(RemoteSession.java:133)
[15:18:11 WARN]: 	at net.zhuoweizhang.raspberryjuice.RemoteSession.tick(RemoteSession.java:113)
[15:18:11 WARN]: 	at net.zhuoweizhang.raspberryjuice.RaspberryJuicePlugin$TickHandler.run(RaspberryJuicePlugin.java:167)
[15:18:11 WARN]: 	at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:53)
[15:18:11 WARN]: 	at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:352)
[15:18:11 WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:689)
[15:18:11 WARN]: 	at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:366)
[15:18:11 WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:650)
[15:18:11 WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:554)
[15:18:11 WARN]: 	at java.lang.Thread.run(Thread.java:745)

My conclusion is that there is a bug in RaspberryJuice that crash when no users are connected and some code does mc.getPlayerEntityIds()

@martinohanlon
Copy link

Yes probably...

@martinohanlon
Copy link

If you put an issue on the Raspberry Juice repo I'll fix it at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment