Skip to content

Javascript Examples

michaelzangl edited this page Oct 10, 2014 · 23 revisions

Here are some Javascript examples, to show you how some tasks can be done.

Utility functions

The same as a good old linux sleep(time). Pauses your bot some seconds.

function pause(time) {
  minescript.doStrategy(minescript.strategy(
      "minebot", "pause", (time * 1) + ""));
}

A teleport to home wrapper.

function home(name) {
  minescript.serverCommand("/home " + name);
  // Wait 2 seconds because the server might take some time...
  pause(2);
}

If you want to get sure that you really teleported away. Good if you want to send your bot mining and do not want it to destroy your home. Mind that it will trigger a false alert if you are already at the right point. This function pauses the game loop on exit.

function awayToHome(name) {
  var pos = minescript.getPlayerPosition();
  home(name);
  if (pos.distance(minescript.getPlayerPosition()) < 100)
    throw "Distance to old position too small."
}

Respawn if required (just in case you were killed...):

if (!minescript.isAlive()) {
  minescript.doStrategy(minescript.strategy(
       "minebot", "respawn"));
  pause(1);
  home("my-work-home");
}

TODO: Walk

function goTowards(x, z) {
  minescript.doStrategy(minescript.strategy(
      "minebot", "move", "towards", x + "", z + ""));
}

TODO: List sheep colors

var colors = {};
var totalSheep = 0;
minescript.getEntities(net.minecraft.entity.passive.EntitySheep.class, 50).forEach(function(sheep) {
  if (sheep.getColor() in colors)
    colors[sheep.getColor()]++;
  else
    colors[sheep.getColor()] = 1;
  totalSheep++;
});
for (color in colors) {
  minescript.displayChat(color + ": " + (100 * colors[color] / totalSheep) + "%");
}
Clone this wiki locally