Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

REPL example bot for mineflayer. Allows testing javascript over CLI to speed up development.

View repl_bot.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// REPL example bot for mineflayer
//
// Connects to server but doesn't have any built in logic. The terminal where
// it was started is now a REPL (Read-Eval-Print-Loop). Anything typed will be
// interpreted as javascript printed using util.inspect. Don't forget to try
// tab completion. These variables are exposed as local:
//
// var mineflayer = require('mineflayer');
// var bot = mineflayer.createBot({ username: 'REPL' });
//
// Examples:
//
// Navigate to named player (requires optional mineflayer-navigate):
// bot.navigate.to(bot.players.vogonistic.entity.position)
 
var fs = require('fs');
var os = require('os');
var path = require('path');
var repl = require('repl');
 
// create and connect the bot
var mineflayer = require('mineflayer');
var bot = mineflayer.createBot({ username: 'REPL' });
bot.on('chat', function(username, message) {
console.log(username+' says: '+message)
});
bot.on('login', function() {
console.log(bot.username+' logged in.');
});
 
// optional navigation support
try {
var navigatePlugin = require('mineflayer-navigate')(mineflayer);
navigatePlugin(bot);
bot.navigate.on('pathFound', function (path) {
console.log('navigate: found path. I can get there in ' + path.length + ' moves.');
});
bot.navigate.on('cannotFind', function () {
console.log('navigate: unable to find path');
});
bot.navigate.on('arrived', function () {
console.log('navigate: I have arrived');
});
bot.navigate.on('stop', function() {
console.log('navigate: stopping');
});
console.log('Info: mineflayer-navigate enabled.')
} catch(err) {
console.log('Info: Optional mineflayer-navigate module not installed, so no navigation support.')
}
 
// create repl interface
var historyFile = path.join(os.tmpDir(), 'bot_repl_history.json')
var r = repl.start('> ');
r.context.bot = bot;
r.context.mineflayer = mineflayer;
r.on('exit', function onExitSaveHistory() {
// ensure the history are available during the next sessions again
console.log('quitting.');
bot.quit();
fs.writeFile(historyFile, JSON.stringify(r.rli.history), function(err) {
process.exit();
})
});
 
// load repl history
fs.readFile(historyFile, function onLoadReadHistory(err, data) {
if (!err && data) {
r.rli.history = JSON.parse(data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.