mob-i-l
Posts: 362
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden

Extend Minecraft Pi Edition using JavaScript via Java

Sun Mar 30, 2014 10:44 pm

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.
Last edited by mob-i-l on Tue Sep 30, 2014 3:37 pm, edited 5 times in total.
Have Pi0&1A&1B&1B+&2B&3B&4B w/ rasPiOS. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

mob-i-l
Posts: 362
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden

Re: Extend Minecraft Pi Edition using JavaScript via Java

Mon May 19, 2014 12:12 pm

If you control a Raspberry Pi running Minecraft Pi remotely via SSH e.g. using PuTTY in Windows then you can't use the GUI-debugger.

Start PuTTY or Terminal and SSH to the Raspberry Pi running Minecraft Pi in a world.

In Shell: Start the JavaScript-interpreter:
$ rlwrap java -cp ~/mcpi/api/java/\*:/usr/share/java/js.jar org.mozilla.javascript.tools.shell.Main
Alternative for Java 8 and built-in Minecraft:
$ rlwrap /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/jjs -cp /opt/minecraft-pi/api/java/McPi.jar

Test JavaScript:
js> for(y=1;y<=42;++y){print(y);}

Draw a pillar in Minecraft Pi at (0, 1, 0):
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)}
Alternative for Java 8 with Nashorn (i.e. JJS):
jjs> mc=Java.type("pi.Minecraft");vec=Java.type("pi.Vec");block=Java.type("pi.Block");con=mc.connect();for(y=1;y<=42;++y){con.setBlock(vec.xyz(0,y,0),block.IRON_BLOCK)}

Remove the pillar in Minecraft Pi i.e. overwrite with air:
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)}
Alternative for Java 8 with Nashorn (i.e. JJS):
jjs> mc=Java.type("pi.Minecraft");vec=Java.type("pi.Vec");block=Java.type("pi.Block");con=mc.connect();for(y=1;y<=42;++y){con.setBlock(vec.xyz(0,y,0),block.AIR)}

Return to Shell:
js> quit()

See possible blocks:
$ links ~/mcpi/api/java/doc/pi/Block.html
Alternative for built-in Minecraft:
$ links /opt/minecraft-pi/api/java/doc/pi/Block.html
Have Pi0&1A&1B&1B+&2B&3B&4B w/ rasPiOS. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

IamSkeleton
Posts: 3
Joined: Thu Jun 18, 2015 4:32 am

Re: Extend Minecraft Pi Edition using JavaScript via Java

Tue Jun 30, 2015 3:24 am

Hi Guys,

I tried the install just for fun and could run the first 3 lines oyour install.

But strangely now each time I launch Minecraft Pi,
I am stuck on the first screen and after a while, MCPI crashes...

any idea ? :shock:

mob-i-l
Posts: 362
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden

Re: Extend Minecraft Pi Edition using JavaScript via Java

Tue Jun 30, 2015 11:42 am

I tested the previous examples now and it works here with the most updated Raspbian Wheezy on Raspberry Pi 2 B. I don't think installing rhino, rlwrap and openjdk-6-jre could affect Minecraft PI, when they don't run since Minecraft PI doesn't use these programs. It is similar to that a web-server would not crash when you don't run a client, even if that client is installed. What does java -version in LXTerminal show? Do you use the built-in Minecraft or did you install it manually? I've also tested to start the Rhino Debugger using the built-in Java (i.e. Java 8) and the built-in Minecraft, and it works too:
$ /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/java -cp /opt/minecraft-pi/api/java/\*:/usr/share/java/\* org.mozilla.javascript.tools.debugger.Main &
Then you could use the Rhino commands there (and press F5 if it doesn't react on the first command).
Have Pi0&1A&1B&1B+&2B&3B&4B w/ rasPiOS. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

IamSkeleton
Posts: 3
Joined: Thu Jun 18, 2015 4:32 am

Re: Extend Minecraft Pi Edition using JavaScript via Java

Sat Jul 04, 2015 3:00 pm

Hey Mobil !
Thanks for your very much for our quick reply and sorry for my delayed answer...

No idea yet why I did break my Raspbian Minecraft Pi however it might be possible that because I installed something and I have now Python version 2.8.x, I cannot start anymore normally the soft.

I am not that good with Linux and will try to figure out what's wrong.
But agree that it shouldn't be related at all with your soft and tuto !

At last but not lease, I would like to thank you for your work and idea !
I followed your tuto mostly by accident, wanted to play around...
I am learning Python for about 3 weeks now and I don't think it's an easy programming language for children !
When I read your lines of code in Javascript, I do understand better even if I have limited knowledge on this language.

My personal analysis and only "a feeling" but I think your Javascript extension is more practicable with children while you want to teach them about programming...

Cheers and good work !
Skel

Return to “Teaching and learning resources”