2024-10-20 14:39:25 -04:00
|
|
|
import { createClient } from "minecraft-protocol";
|
|
|
|
import { default as settings } from './settings.json' with {type: "json"}
|
2024-10-23 22:59:54 -04:00
|
|
|
import generateUser from './util/usergen.js'
|
2024-10-20 14:39:25 -04:00
|
|
|
import EventEmitter from 'node:events'
|
2024-10-20 17:41:10 -04:00
|
|
|
import { readdirSync } from "node:fs";
|
2024-10-20 14:39:25 -04:00
|
|
|
|
|
|
|
const bots = [];
|
|
|
|
|
2024-10-21 17:47:49 -04:00
|
|
|
const plugins = [];
|
2024-10-20 17:41:10 -04:00
|
|
|
const bpl = readdirSync('plugins')
|
|
|
|
for (const plugin of bpl) {
|
|
|
|
if (!plugin.endsWith('.js')) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
import(`./plugins/${plugin}`).then((pluginItem)=>{
|
|
|
|
for(const bot of bots){
|
2024-10-23 22:59:54 -04:00
|
|
|
pluginItem.default(bot)
|
2024-10-20 17:41:10 -04:00
|
|
|
}
|
2024-10-24 00:43:22 -04:00
|
|
|
plugins.push(pluginItem.default) // For rejoining
|
2024-10-20 17:41:10 -04:00
|
|
|
})
|
|
|
|
} catch (e) { console.log(e) }
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:39:25 -04:00
|
|
|
const createBot = function createBot (host, oldId) {
|
|
|
|
const bot = new EventEmitter()
|
2024-10-24 00:43:22 -04:00
|
|
|
|
|
|
|
bot.host = host
|
|
|
|
bot.interval = {}
|
|
|
|
|
2024-10-20 14:39:25 -04:00
|
|
|
bot._client = createClient({
|
|
|
|
host: host.host,
|
|
|
|
port: host.port ? host.port : 25565,
|
|
|
|
username: generateUser(host.options.legalName),
|
|
|
|
version: host.version ? host.version : settings.version_mc
|
|
|
|
})
|
|
|
|
if (typeof oldId !== 'undefined') {
|
|
|
|
for (const i in bots[oldId].interval) {
|
|
|
|
clearInterval(bots[oldId].interval[i])
|
|
|
|
}
|
|
|
|
delete bots[oldId]
|
|
|
|
bot.id = oldId
|
|
|
|
bots[oldId] = bot
|
2024-10-21 17:47:49 -04:00
|
|
|
for(const pluginItem of plugins){
|
2024-10-24 00:43:22 -04:00
|
|
|
pluginItem(bot)
|
2024-10-21 17:47:49 -04:00
|
|
|
}
|
2024-10-20 14:39:25 -04:00
|
|
|
} else {
|
|
|
|
bot.id = bots.length
|
|
|
|
bots.push(bot)
|
|
|
|
}
|
|
|
|
|
2024-10-21 17:47:49 -04:00
|
|
|
bot.info = (msg) => {
|
|
|
|
console.log(`[${bot.id}] [info] ${msg}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.displayChat = (type, subtype, msg) => {
|
|
|
|
if (settings.displaySubtypesToConsole) {
|
|
|
|
console.log(`[${bot.id}] [${type}] [${subtype}] ${msg}`)
|
|
|
|
} else {
|
|
|
|
console.log(`[${bot.id}] [${type}] ${msg}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:39:25 -04:00
|
|
|
bot._client.on('error', (err) => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const i in settings.servers) {
|
|
|
|
createBot(settings.servers[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
bots,
|
|
|
|
createBot
|
|
|
|
}
|