From 086251bce60b9f98346432b501d2aa8c20e2c020 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 24 Dec 2020 13:38:16 +0500 Subject: [PATCH] feat: add a basic feature to rejoin on server restart --- lib/index.js | 29 ++++++++++++++++------------- lib/plugins/command.js | 12 +++++++++++- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5ef7750..a933290 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,19 +9,22 @@ let cfg = { const mineflayer = require("mineflayer"); // const { createGetAccessor } = require('typescript'); -const bot = - !isNaN(parseInt(process.argv[3])) && parseInt(process.argv[3]) > 1e2 ? - mineflayer.createBot({ - host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want. - port: parseInt(process.argv[3]) || process.env.MINECRAFT_PORT || env.MINECRAFT_PORT // || 58471, - }) - : - mineflayer.createBot({ - host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want. - username: process.argv[3] || process.env.MINECRAFT_USER || env.MINECRAFT_USER, - password: process.argv[4] || process.env.MINECRAFT_PASS || env.MINECRAFT_PASS, - // port: process.argv[5] || process.env.MINECRAFT_PORT || 58471, - }) +const options = !isNaN(parseInt(process.argv[3])) && parseInt(process.argv[3]) > 1e2 ? + { + host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want. + port: parseInt(process.argv[3]) || process.env.MINECRAFT_PORT || env.MINECRAFT_PORT // || 58471, + } + : + { + host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want. + username: process.argv[3] || process.env.MINECRAFT_USER || env.MINECRAFT_USER, + password: process.argv[4] || process.env.MINECRAFT_PASS || env.MINECRAFT_PASS, + // port: process.argv[5] || process.env.MINECRAFT_PORT || 58471, + } + +const bot = mineflayer.createBot(options) + +cfg.botOptions = options let plugins = {} diff --git a/lib/plugins/command.js b/lib/plugins/command.js index 17910db..3085e3d 100644 --- a/lib/plugins/command.js +++ b/lib/plugins/command.js @@ -46,7 +46,17 @@ const events = { } } , chat: command - , kicked: (reason, loggedIn) => console.warn(reason, loggedIn) + , kicked: function rejoin(reason, loggedIn) { + console.warn(reason, loggedIn && "logged_in") + if (reason.extra && reason.extra[0].text === "Server closed") { + bot.quit() + bot.end() + // TODO implement all startup features (maybe refactor all into a single function / module?) + setTimeout((bot, cfg) => { + bot = mineflayer.createBot(cfg.botOptions) + }, 15 * 60 * 1000, bot, cfg); + } + } } const events_registered = []