2020-12-24 03:52:50 +00:00
|
|
|
const env = require("dotenv-packed").parseEnv().parsed
|
2020-12-21 16:08:38 +00:00
|
|
|
const fs = require('fs');
|
2021-04-23 01:07:29 +00:00
|
|
|
const cfg = {
|
|
|
|
admin: process.env.MINECRAFT_PLAYER_ADMIN || env.MINECRAFT_PLAYER_ADMIN || console.warn("main: bot admin user not provided"),
|
|
|
|
mods: process.env.MINECRAFT_PLAYER_MODS || env.MINECRAFT_PLAYER_MODS || [], // json array,
|
2020-12-21 16:08:38 +00:00
|
|
|
stateMachines: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mineflayer = require("mineflayer");
|
|
|
|
// const { createGetAccessor } = require('typescript');
|
|
|
|
|
2020-12-24 08:38:16 +00:00
|
|
|
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
|
2020-12-21 16:08:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
let plugins = {}
|
|
|
|
|
2021-04-29 03:14:40 +00:00
|
|
|
function loadplugin(pluginname, pluginpath = './plugins/' + pluginname) {
|
2020-12-21 16:08:38 +00:00
|
|
|
try {
|
|
|
|
plugins[pluginname] = require(pluginpath)
|
2020-12-24 04:19:32 +00:00
|
|
|
plugins[pluginname].load(cfg)
|
2020-12-21 16:08:38 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code == 'MODULE_NOT_FOUND') {
|
|
|
|
console.warn('plugin not used:', pluginpath)
|
2021-04-29 03:14:40 +00:00
|
|
|
} else if (plugins[pluginname] && !plugins[pluginname].load) {
|
|
|
|
unloadplugin(pluginname, pluginpath)
|
2020-12-21 16:08:38 +00:00
|
|
|
} else {
|
|
|
|
console.error(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:14:40 +00:00
|
|
|
function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) {
|
2020-12-21 16:08:38 +00:00
|
|
|
const plugin = require.resolve(pluginpath)
|
|
|
|
try {
|
|
|
|
if (plugin && require.cache[plugin]) {
|
2021-04-19 08:15:25 +00:00
|
|
|
// `unload()` isn't exported sometimes,
|
|
|
|
// when plugin isn't properly loaded
|
|
|
|
require.cache[plugin].exports?.unload?.()
|
2020-12-21 16:08:38 +00:00
|
|
|
delete plugins[pluginname]
|
|
|
|
delete require.cache[plugin]
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:14:40 +00:00
|
|
|
function reloadplugin(event, filename, pluginpath = './plugins/') {
|
2020-12-21 16:08:38 +00:00
|
|
|
if (!/\.js$/.test(filename)) { return }
|
2021-04-29 03:14:40 +00:00
|
|
|
filename = filename.replace("\\", "/") // windows
|
2020-12-21 16:08:38 +00:00
|
|
|
if (!cfg.fsTimeout) {
|
|
|
|
console.info(event, filename)
|
2021-04-29 03:14:40 +00:00
|
|
|
const fullpluginpath = pluginpath + filename
|
|
|
|
const pluginname = (filename.split(".js")[0]).replace(/\/.+/, "")
|
|
|
|
pluginpath = pluginpath + pluginname
|
|
|
|
const hassubplugin = fullpluginpath.replace(/\.js$/, "") !== pluginpath
|
2020-12-21 16:08:38 +00:00
|
|
|
const check = Object.keys(cfg.plugins)
|
2021-04-29 03:14:40 +00:00
|
|
|
console.info(`reload file: ./lib/${fullpluginpath}`)
|
|
|
|
const plugin = require.resolve(fullpluginpath)
|
2020-12-21 16:08:38 +00:00
|
|
|
if (plugin && require.cache[plugin]) {
|
|
|
|
// console.debug(Object.keys(cfg.plugins))
|
2021-04-29 03:14:40 +00:00
|
|
|
unloadplugin(pluginname)
|
2020-12-21 16:08:38 +00:00
|
|
|
console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak")
|
2021-04-29 03:14:40 +00:00
|
|
|
if (hassubplugin) {
|
|
|
|
console.info("reload: also unloading sub")
|
|
|
|
unloadplugin(pluginname, fullpluginpath)
|
|
|
|
}
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
2021-04-29 03:14:40 +00:00
|
|
|
loadplugin(pluginname)
|
2020-12-21 16:08:38 +00:00
|
|
|
if (Object.keys(cfg.plugins).length != check.length) {
|
|
|
|
// If left < right :
|
|
|
|
// - new plugin that's not registered in cfg.plugins, so added
|
|
|
|
// - rename, so old wasn't removed
|
|
|
|
// If left > right :
|
|
|
|
// - error in file (syntax, missing load()), so was not reloaded
|
|
|
|
console.warn("plugin count mismatch", check.length, Object.keys(cfg.plugins).length, Object.keys(cfg.plugins))
|
|
|
|
}
|
|
|
|
cfg.fsTimeout = setTimeout(function () { cfg.fsTimeout = null }, 2000) // give 2 seconds for multiple events
|
|
|
|
}
|
|
|
|
// console.log('file.js %s event', event)
|
|
|
|
}
|
|
|
|
|
2021-04-27 06:47:25 +00:00
|
|
|
fs.watch('./lib/plugins', { recursive: true }, reloadplugin)
|
2020-12-21 16:08:38 +00:00
|
|
|
|
|
|
|
cfg.bot = bot
|
2021-03-23 09:39:07 +00:00
|
|
|
// TODO better name, or switch to array
|
|
|
|
cfg.botAddressPrefix = '!'
|
2020-12-21 16:08:38 +00:00
|
|
|
cfg.quiet = true
|
|
|
|
|
|
|
|
|
|
|
|
// == actually do stuff
|
|
|
|
|
|
|
|
bot.once("spawn", () => {
|
|
|
|
plugins = {
|
|
|
|
command: require('./plugins/command'),
|
2020-12-24 16:39:50 +00:00
|
|
|
informer: require('./plugins/informer'),
|
2021-04-05 12:47:06 +00:00
|
|
|
inventory: require('./plugins/inventory'),
|
2020-12-24 16:32:08 +00:00
|
|
|
finder: require('./plugins/finder'),
|
2021-04-05 12:47:06 +00:00
|
|
|
mover: require('./plugins/mover'),
|
2020-12-21 16:08:38 +00:00
|
|
|
sleeper: require('./plugins/sleeper'),
|
2021-04-05 12:47:06 +00:00
|
|
|
eater: require('./plugins/eater'),
|
2020-12-21 16:08:38 +00:00
|
|
|
armor: require('./plugins/armor'),
|
|
|
|
guard: require('./plugins/guard'),
|
2020-12-24 05:40:46 +00:00
|
|
|
// miner: require('./plugins/miner.js'),
|
2021-03-22 09:32:08 +00:00
|
|
|
statemachine: require('./plugins/statemachine'),
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.plugins = plugins
|
2021-05-03 02:20:33 +00:00
|
|
|
// cfg.botAddressPrefix = ${bot.username.substr(-2,2)}
|
|
|
|
cfg.botAddressRegex = new RegExp(`^${bot.username}:? (/|${cfg.botAddressPrefix}.+)`)
|
2021-04-29 03:37:46 +00:00
|
|
|
// FIXME leaks every load, so adding here instead of command.js to load only once
|
|
|
|
bot.addChatPattern("web", /\[WEB\] (\[.+\])?\s*([\w.]+): (.+)/, { parse: true })
|
2020-12-21 16:08:38 +00:00
|
|
|
|
|
|
|
for (const plugin of Object.values(plugins)) {
|
|
|
|
try {
|
|
|
|
plugin.load(cfg)
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bot.on("death", () => {
|
|
|
|
bot.pathfinder && bot.pathfinder.setGoal(null)
|
|
|
|
// plugins.guard.unload()
|
|
|
|
})
|
|
|
|
// bot.on("respawn", () => {
|
|
|
|
// // setTimeout(plugins.guard.load, 2000)
|
|
|
|
// })
|