refactor client and make it public

This commit is contained in:
Chipmunk 2024-02-29 23:26:30 -05:00
parent 990bc7038c
commit fe0bcf9c8b
2 changed files with 40 additions and 24 deletions

View file

@ -4,7 +4,7 @@ const usages = ['create <options (json)>', 'end <i>', 'write <i> <name> <data (j
const aliases = ['client'] const aliases = ['client']
const enabled = true const enabled = true
const permLevel = 2 const permLevel = 0
const mc = require('minecraft-protocol') const mc = require('minecraft-protocol')
const clients = [] const clients = []
@ -14,30 +14,46 @@ const util = require('util')
function execute (bot, cmd, player, args, handler) { function execute (bot, cmd, player, args, handler) {
const subCmd = args.shift() const subCmd = args.shift()
let client, i, name, data
switch (subCmd) { switch (subCmd) {
case 'create': case 'create': {
client = mc.createClient(JSON.parse(args.join(' ').replace(sectionRegex, ''))) const options = { host: bot.host, port: bot.port, ...JSON.parse(args.join(' ').replace(sectionRegex, '')) }
i = clients.length
client.on('login', () => bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: client.username + '\u00a7r logged in.', color: bot.colors.primary })))
client.on('end', () => {
clients.splice(i, 1)
bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: client.username + '\u00a7r ended.', color: bot.colors.primary }))
})
client.on('error', (err) => bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: util.inspect(err).replace(/\n.*/g, ''), color: bot.colors.error })))
clients.push(client)
break
case 'end':
i = parseInt(args.join(' '))
clients[i].end()
clients.splice(i, 1)
break
case 'write':
i = parseInt(args.shift())
name = args.shift()
data = JSON.parse(args.join(' ').replace(sectionRegex, ''))
clients[i].write(name, data) const client = mc.createClient(options)
const idx = clients.length
client.once('login', () => bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{ text: client.username, color: bot.colors.primary }, ' logged in'])))
client.on('end', () => {
clients.splice(idx, 1)
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{ text: client.username, color: bot.colors.primary }, ' ended']))
})
client.on('error', (err) => bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: util.inspect(err).replace(/\n.*/g, ''), color: bot.colors.error })))
clients.push(client)
}
break
case 'end': {
const stridx = args.join(' ').replace(sectionRegex, '')
let idx = Number(stridx)
if (!Number.isInteger(idx)) clients.forEach((client, cidx) => { if (client.username === stridx) idx = cidx })
if (!Number.isInteger(idx)) throw new Error('Unknown client: ' + stridx)
clients[idx].end()
clients.splice(idx, 1)
}
break
case 'write': {
const stridx = args.shift().replace(sectionRegex, '')
let idx = Number(stridx)
if (!Number.isInteger(idx)) clients.forEach((client, cidx) => { if (client.username === stridx) idx = cidx })
if (!Number.isInteger(idx)) throw new Error('Unknown client: ' + stridx)
const name = args.shift()
const data = JSON.parse(args.join(' ').replace(sectionRegex, ''))
clients[idx].write(name, data)
}
break break
case 'list': case 'list':
bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: 'Clients: ' + clients.map(client => client.username).join('\u00a7r, '), color: bot.colors.primary })) bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: 'Clients: ' + clients.map(client => client.username).join('\u00a7r, '), color: bot.colors.primary }))

File diff suppressed because one or more lines are too long