Closed JonnyD closed 10 years ago
Using this reconnects but the events stop working, could anyone point me in the right direction?
bot.on('kicked', function(reason) {
console.log("I got kicked for", reason, "lol");
bot.quit
bot.end
bot = mineflayer.createBot(options);
});
Seems like that code should work, although keep in mind that in JavaScript, bot.quit
and bot.end
do nothing - what you want are bot.quit()
and bot.end()
.
That error above looks like maybe if you get kicked, you're already disconnected, so you can't call bot.quit()
. Try only calling bot.end()
or calling neither and skip straight to the bot = mineflayer.createBot(options)
part.
Thanks for your response. I have tried this but it only works once. After being kicked the first time it reconnects but it then stops triggering events and because it stops triggering events (e.g. bot.on('kicked'..)) the next kick disconnects the bot forever. Any idea?
var mineflayer = require('mineflayer');
var options = {
host: "192.227.244.15", // optional
port: 25565, // optional
username: "", // email and password are required only for
password: "", // online-mode=true servers
};
var bot = mineflayer.createBot(options);
bot.on('login', function() {
console.log("I logged in.");
console.log("settings", bot.settings);
});
bot.on('playerJoined', function(player) {
console.log(player.username + " joined");
});
bot.on('playerLeft', function(player) {
console.log(player.username + " left");
});
bot.on('kicked', function(reason) {
console.log("I got kicked for", reason, "lol");
bot = mineflayer.createBot(options);
});
bot.on('spawn', function() {
console.log("I have spawned");
console.log("game", bot.game);
});
bot.on('death', function() {
bot.chat("I died x.x");
});
You're replacing the bot
variable with a new object. The handlers you attached are all attached to the old reference. You need to attach your handlers to the new instances as well.
Got it working like this but is it best practice? Coming from a Java & PHP background node.js isn't as intuitive to me.
var mineflayer = require('mineflayer');
var options = {
//host: "mc.civcraft.vg",
host: "192.227.244.15",
port: 25565,
username: "",
password: "",
};
var bot = mineflayer.createBot(options);
bindEvents(bot);
function bindEvents(bot) {
bot.on('login', function() {
console.log("I logged in.");
console.log("settings", bot.settings);
});
bot.on('kicked', function(reason) {
console.log("I got kicked for", reason, "lol");
bot = mineflayer.createBot(options);
bindEvents(bot);
});
}
Seems fine to me. As your code base grows you'll figure out what organization works best for you.
Closed, issue seems fixed, comment if this needs a re-open
Here's what I have tried so far:
but all I got was this error:
(using Cloud9ide)