2024-08-19 14:07:17 -04:00
|
|
|
import * as m from 'minecraft-protocol'
|
|
|
|
import * as settings from './settings.json' with {type: "json"}
|
2024-08-19 14:20:15 -04:00
|
|
|
import { generateUser } from './util/usergen.js'
|
2024-08-19 14:07:17 -04:00
|
|
|
import EventEmitter from 'node:events'
|
|
|
|
import * as fs from 'fs'
|
2024-08-12 04:33:43 -04:00
|
|
|
|
2024-08-19 14:07:17 -04:00
|
|
|
let botArray = []
|
2024-08-12 04:33:43 -04:00
|
|
|
|
2024-08-19 14:20:15 -04:00
|
|
|
const botplug = [];
|
|
|
|
|
|
|
|
import { default as botplug_testPlugin } from "./plugins/testing.mjs" // We have to load plugins manually, because auto-detection does not work in this format
|
|
|
|
|
|
|
|
botplug.push(botplug_testPlugin)
|
|
|
|
|
2024-08-17 09:56:26 -04:00
|
|
|
const bpl = fs.readdirSync('plugins')
|
|
|
|
|
|
|
|
const loadplug = (botno) => {
|
2024-08-19 14:20:15 -04:00
|
|
|
botplug.forEach((plug) => {
|
2024-08-12 04:33:43 -04:00
|
|
|
try {
|
|
|
|
if (plug.load) {
|
2024-08-19 14:07:17 -04:00
|
|
|
plug.load(botArray[botno])
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
|
|
|
} catch (e) { console.log(e) }
|
2024-08-19 14:20:15 -04:00
|
|
|
})
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const createBot = function createBot (host, oldId) {
|
|
|
|
if (host.options.disabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const bot = new EventEmitter()
|
|
|
|
bot._client = m.createClient({
|
|
|
|
host: host.host,
|
|
|
|
port: host.port ? host.port : 25565,
|
2024-08-19 14:20:15 -04:00
|
|
|
username: generateUser(host.options.legalName),
|
2024-08-12 04:33:43 -04:00
|
|
|
version: host.version ? host.version : settings.version_mc
|
|
|
|
})
|
|
|
|
if (typeof oldId !== 'undefined') {
|
2024-08-19 14:07:17 -04:00
|
|
|
for (const i in botArray[oldId].interval) {
|
|
|
|
clearInterval(botArray[oldId].interval[i])
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
2024-08-19 14:07:17 -04:00
|
|
|
delete botArray[oldId]
|
2024-08-12 04:33:43 -04:00
|
|
|
bot.id = oldId
|
2024-08-19 14:07:17 -04:00
|
|
|
botArray[oldId] = bot
|
2024-08-12 04:33:43 -04:00
|
|
|
} else {
|
2024-08-19 14:07:17 -04:00
|
|
|
bot.id = botArray.length
|
|
|
|
botArray.push(bot)
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
2024-08-19 14:07:17 -04:00
|
|
|
|
2024-08-12 04:33:43 -04:00
|
|
|
bot.host = host
|
|
|
|
bot.interval = {}
|
|
|
|
|
|
|
|
bot.info = (msg) => {
|
|
|
|
console.log(`[${bot.id}] [info] ${msg}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.displayChat = (type, msg) => {
|
|
|
|
console.log(`[${bot.id}] [${type}] ${msg}`)
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:20:15 -04:00
|
|
|
loadplug(bot.id)
|
2024-08-12 04:33:43 -04:00
|
|
|
bot._client.on('error', (err) => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:07:17 -04:00
|
|
|
for (const i in settings.default.servers) {
|
|
|
|
createBot(settings.default.servers[i])
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
|
|
|
|
2024-08-19 14:07:17 -04:00
|
|
|
//module.exports.createBot = createBot
|