Using Scratch & Scriptcraft (& more) to teach kids programming in Minecraft – updated configuration

Scratch-logo-outline

&

9ccf0-minecraftlogo

The first solution that I found to make Scratch work together with Minecraft & Scriptcraft, described here, used Node RED to transform http commands sent by Scratch into mqtt packets, brokered by a mosquitto server and finally received by Scriptcraft MQTT extension.

Reading the awsome post Child Processes, Streams, and Minecraft Server Management via Text Message using Node.js by Kevin Whinnery, I discovered a much easier way to send commands from Scratch to Scriptcraft.

The Node.js script below launches Minecraft server and also creates a very simple http server process that will transfer commands directly to Scriptcraft:

screen-shot-2016-09-23-at-20-33-04

The servers listens to port 8088, the same used by the Scratch extension:

screen-shot-2016-09-23-at-20-48-23

As with the previous setup, the most important part is done using Scriptcraft, the mod created by Walter Higgins that lets you write Minecraft plugins using Javascript.

9xth1ei

The /js scratch() call in Node.js script invokes the Javascript function scratch that will interpret the commands received from Scratch. For now it buffers the command until a build instruction is received, then proceeds to execute them in order, inserting also delays based on the complexity of the building instructions (i.e. dimension of impacted areas).

Below is a very basic form of scratch() function working with only one client, that you can paste into a .js file saved in \Scriptcraft\plugins folder.

 var Drone = require('/drone/drone').Drone;
 var foreach = require('utils').foreach;
 var utils = require('utils');
 var connected = false;
 var drona = '';
 var targetPlayer = '';
 var playerName = '';

 function scratch(command,ip){ //ip not used in this version
 var players = utils.players();
 var cmd=command.split("/");

 if(cmd[0] == 'connect'){
   console.log('trying to connect to player ' + cmd[1]);
   playerName=cmd[1];
   for (pl in players){
     if (players[pl].name.toLowerCase()== playerName.toLowerCase()){
       targetPlayer = players[pl];
       echo(targetPlayer, 'Connected to Scratch client');
       drona = new Drone (targetPlayer.location);
       connected = true;
       }
     }
   }
 if (!connected) { //use of other command before connecting to any player
   console.log('Not connected to any player');
   return
   }
 console.log("command received -" + cmd[0] + ";");
 switch(cmd[0]){
   case 'boxCommand':
   case 'box':
     drona.box('' + cmd[1] + ':' + parseInt(cmd[2]), parseInt(cmd[3]), parseInt(cmd[4]), parseInt(cmd[5]));
   break;
   case 'box0':
   case 'box0Command':
     drona.box0('' + cmd[1] + ':' + parseInt(cmd[2]) ,parseInt(cmd[3]), parseInt(cmd[4]), parseInt(cmd[5]));
   break;
   case 'moveDrone':
     switch(cmd[1]){
       case 'reset':      
         drona=new Drone (targetPlayer.location);
       break;
       case 'up':
         drona.up(parseInt(cmd[2]));
       break;
       case 'down':
         drona.down(parseInt(cmd[2]));
       break;
       case 'left':
         drona.left(parseInt(cmd[2]));
       break;
       case 'right':
         drona.right(parseInt(cmd[2]));
       break;
       case 'fwd':
         drona.fwd(parseInt(cmd[2]));
       break;
       case 'back':
         drona.back(parseInt(cmd[2]));
       break;
       case 'turn':
         drona.turn(parseInt(cmd[2]));
       break;
     }
     break;
   }
 };
 exports.scratch=scratch;

This is an example of some functions created in Scratch screen-shot-2016-09-23-at-21-18-27

And this is how it looks:

I’m still looking for a solution to access in Scratch values from Minecraft world. It will make more sense if commands are no more buffered until the build instruction is received, but executed instantly.

Advertisements

4 thoughts on “Using Scratch & Scriptcraft (& more) to teach kids programming in Minecraft – updated configuration

  1. I’ve been trying to replicate this and I have minecraft and scratch communicating on port 8088 but I keep running into trouble at the ” /js scratch() call ” — are you leaving out a step here?
    Are you still using MQTT in this latest version?…if so, how do you load it with your minecraft.js ?

  2. Hi, MQTT is no more used, but you need the javascript scratch() function that wasn’t presented here.
    I will try to add to this post a very basic form of the function to let you test the configuration and when I will have stable versions of the scripts I will create a github repository and also a new blog post with all the installation instructions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s