It is possible to build things, sense things, move around and handle events in Minecraft Pi Edition using JavaScript via Java. JavaScript is normally used in web browsers but can also be used by web servers, but also for stand-alone programs. JavaScript is a rather flexible language and looks similar to C but is inspired by LISP. It is e.g. possible to write a program that puts together a program using strings of text and then runs it. JavaScript is getting faster all the time with e.g. asm.js and SIMD. There are many implementations of JavaScript and this example uses Rhino which has a good connection to Java, which means we can use the Minecraft Pi Edition API for Java.
Start a terminal on Desktop 1 in Raspbian on Raspberry Pi. And enter the bold text after the prompt "$ " or "js> ". There are no line-feeds or carriage-returns in the command-lines even if they are shown broken.
$ sudo apt-get install rhino rlwrap openjdk-6-jre
$ rlwrap java -cp ~/mcpi/api/java/\*:/usr/share/java/js.jar org.mozilla.javascript.tools.shell.Main
Alternative for built-in Minecraft:
$ rlwrap java -cp /opt/minecraft-pi/api/java/\*:/usr/share/java/js.jar org.mozilla.javascript.tools.shell.Main
Try this:
js> for(y=1;y<=42;++y){print(y);}
Try out the Rhino JavaScript Debugger. Use Ctrl+D to quit Rhino to Bash. (It's possible to start Rhino using rhino but then you will not have support for Minecraft Pi, and it also has a buggy support for arrow-keys, see the topic in the Java-forum.)
$ java -cp ~/mcpi/api/java/\*:/usr/share/java/\* org.mozilla.javascript.tools.debugger.Main &
Alternative for built-in Minecraft:
$ java -cp /opt/minecraft-pi/api/java/\*:/usr/share/java/\* org.mozilla.javascript.tools.debugger.Main &
Try this (if it seems to hang after Enter press F5 or click Go):
js> for(y=1;y<=42;++y){print(y);}
Download Minecraft Pi Edition from:
http://pi.minecraft.net/
Install according to their instructions and read some instructions in the txt-files using e.g. less.
Switch to Desktop 2. Start a terminal.
Start Minecraft in Raspberry Pi and go to a world:
$ ~/mcpi/minecraft-pi
Alternative for built-in Minecraft:
$ minecraft-pi
Switch to Desktop 1 and build a pillar by in the debugger enter:
js> pi=Packages.pi;con=pi.Minecraft.connect();for(y=1;y<=42;++y){con.setBlock(pi.Vec.xyz(0,y,0),pi.Block.IRON_BLOCK)}
Switch to Desktop 2 and view the result at coordinates (0,0,0). You can see the coordinates for Steve at the top left corner. Switch back to Desktop 1.
You can remove the pillar using:
js> pi=Packages.pi;con=pi.Minecraft.connect();for(y=1;y<=42;++y){con.setBlock(pi.Vec.xyz(0,y,0),pi.Block.AIR)}
You may also want to run the JavaScript-console in the terminal in Desktop 2 because you can see some lower lines of the terminal even if it is covered by Minecraft and this is enough for JavaScript since you can write compact code on a single line:
$ rlwrap java -cp ~/mcpi/api/java/\*:/usr/share/java/js.jar org.mozilla.javascript.tools.shell.Main
Alternative for built-in Minecraft:
$ rlwrap java -cp /opt/minecraft-pi/api/java/\*:/usr/share/java/js.jar org.mozilla.javascript.tools.shell.Main
Sometimes when you call a Java-function/method from JavaScript and you want to pass no arguments you have to pass [], i.e. an empty list. I have tested all the examples for Java from the Rhino JavaScript Debugger and they all seem to work, except Usage (due a bug that a class is not public). E.g.
js> pi=Packages.pi;pi.demo.DigitalClock.main([])
That demo prints a digital clock each minute, and this particular demo doesn't require a Raspberry Pi since it doesn't actually communicate with a running Minecraft even though it loads the Minecraft API code-libraries. The only way to stop this program seems to be to quit the debugger or console using e.g. Ctrl+Q and Ctrl+C, respectively.