-
Notifications
You must be signed in to change notification settings - Fork 48
Javascript Examples
Here are some Javascript examples, to show you how some tasks can be done.
Put them in a text file, then run: /minebot js /path/to/file or on windows: /minebot js C:\path\to\file.txt
Simply put the simple script lines in a file in the order you want them. If you want to loop indefinitely, use this:
while (true) {
// Replace this with the commands you want.
}
Mine
minescript.doStrategy(minescript.strategy("minebot", "mine"));
Mine iron ore
minescript.doStrategy(minescript.strategy("minebot", "mine", "iron_ore"));
Tunnel for 100 Blocks in the direction you are looking
minescript.doStrategy(minescript.strategy("minebot", "tunnel", "100"));
Of course this works with any other command the bot understands (minebot + minebuild).
Walk to a given position (x, z)
minescript.doStrategy(minescript.strategyWalkTowards(250, 110));
Align to grid:
minescript.doStrategy(minescript.strategyWalkTowards(minescript.getPlayerPosition().getX(), minescript.getPlayerPosition().getZ()));
Walk 10 Blocks in x direction:
minescript.doStrategy(minescript.strategyWalkTowards(minescript.getPlayerPosition().getX() + 10, minescript.getPlayerPosition().getZ()));
Do multiple things at once: eat when hungry, lumberjack any tree in range and replant it, collect items, harvest/replant any crops and feed cows as long as there are fedable cows nearby, in that order (Hint: There might be times in which none of this is possible, you should put this in a while loop to avoid the bot to stop.)
minescript.doStrategy(minescript.stack(
minescript.strategy("minebot", "eat"),
minescript.strategy("minebot", "lumberjack", "replant"),
minescript.strategy("minebot", "kill", "0"),
minescript.strategy("minebot", "plant"),
minescript.strategy("minebot", "feed", "cow")
));
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");
}
This advanced example shows how to list all sheep in range and then kill them in a way that leaves 20 sheep per color.
var colors = {};
var totalSheep = 0;
var sheepToLeave = 20; // Total of 320 sheep. Brings you to level 30 by feeding once.
minescript.getEntities(net.minecraft.entity.passive.EntitySheep, 100).forEach(function(sheep) {
if (sheep.getColor() in colors)
colors[sheep.getColor()]++;
else
colors[sheep.getColor()] = 1;
totalSheep++;
});
// Print a list of sheep
minescript.displayChat("Sheep around you: " + totalSheep);
sorted = [];
for (color in colors) {
sorted.push(color);
}
sorted.sort(function(a, b) {return -colors[a] + colors[b];});
for (var i = 0; i < sorted.length; i++) {
minescript.displayChat(sorted[i] + ": " + colors[sorted[i]] + " (" + Math.round(100 * colors[sorted[i]] / totalSheep) + "%)");
}
// Now kill those that are too much
for (color in colors) {
var toKill = colors[color] - sheepToLeave;
minescript.setDescription("Killing " + toKill + " " + color + " sheep");
if (toKill > 0) {
minescript.doStrategy(minescript.stack(
minescript.strategy("minebot", "eat"),
minescript.strategy("minebot", "kill", "sheep", color, toKill + "")));
}
}