Chipmunk
5f3560910b
not sure if it's better or worse now. also, i have tried to optimize color codes, but this optimization seems unreliable (i might fix or remove it in the future)
106 lines
3.1 KiB
JavaScript
106 lines
3.1 KiB
JavaScript
const { parseNbtText } = require('../util/chat/utility')
|
|
|
|
const plainStringify = require('../util/chat/stringify/plain')
|
|
const colorCodeStringify = require('../util/chat/stringify/color_code')
|
|
const htmlStringify = require('../util/chat/stringify/html')
|
|
const ansiStringify = require('../util/chat/stringify/ansi')
|
|
|
|
const kaboomParser = require('../util/chat/message_parser/kaboom')
|
|
|
|
function inject (bot) {
|
|
bot.chat = {
|
|
queue: [],
|
|
parsers: [kaboomParser],
|
|
|
|
message (message) {
|
|
bot._client.write('chat_message', {
|
|
message,
|
|
timestamp: BigInt(Date.now()),
|
|
salt: 0n,
|
|
offset: 0,
|
|
acknowledged: Buffer.allocUnsafe(3)
|
|
})
|
|
},
|
|
|
|
|
|
command (command) {
|
|
bot._client.write('chat_command', {
|
|
command,
|
|
timestamp: BigInt(Date.now()),
|
|
salt: 0n,
|
|
argumentSignatures: [],
|
|
signedPreview: false,
|
|
messageCount: 0,
|
|
acknowledged: Buffer.alloc(3),
|
|
previousMessages: []
|
|
})
|
|
}
|
|
}
|
|
|
|
setInterval(() => {
|
|
if (!bot.loggedIn) return
|
|
|
|
const message = bot.chat.queue.shift()
|
|
if (message != null) {
|
|
if (message[0] === '/') bot.chat.command(message.substring(1))
|
|
else bot.chat.message(message)
|
|
}
|
|
}, 200)
|
|
|
|
bot.on('packet.profileless_chat', (packet) => {
|
|
const message = parseNbtText(packet.message)
|
|
const senderName = parseNbtText(packet.name)
|
|
const type = bot.registry?.chatFormattingById[packet.type]
|
|
|
|
bot.emit('profileless_chat', { message, senderName, type })
|
|
bot.emit('chat', message)
|
|
|
|
tryParsingMessage(message, { senderName, players: bot.players, lang: bot.registry.language })
|
|
})
|
|
|
|
bot.on('packet.player_chat', (packet) => {
|
|
const plain = packet.plainMessage
|
|
const unsigned = parseNbtText(packet.unsignedChatContent)
|
|
const sender = bot.players.find(player => player.uuid === packet.senderUuid)
|
|
const type = bot.registry?.chatFormattingById[packet.type]
|
|
|
|
bot.emit('player_chat', { plain, unsigned, sender, type: type.name })
|
|
bot.emit('chat', unsigned)
|
|
|
|
tryParsingMessage(unsigned, { senderUuid: sender.uuid, players: bot.players, lang: bot.registry.language, plain })
|
|
})
|
|
|
|
bot.on('packet.system_chat', (packet) => {
|
|
if (packet.isActionBar) return
|
|
|
|
const message = parseNbtText(packet.content)
|
|
if (message?.translate === 'advMode.setCommand.success') return
|
|
|
|
bot.emit('system_chat', message)
|
|
bot.emit('chat', message)
|
|
})
|
|
|
|
bot.on('chat', message => {
|
|
const stringifyOptions = { lang: bot.registry.language }
|
|
bot.emit('chat_plain', plainStringify(message, stringifyOptions))
|
|
bot.emit('chat_color_code', colorCodeStringify(message, stringifyOptions))
|
|
bot.emit('chat_ansi', ansiStringify(message, stringifyOptions))
|
|
bot.emit('chat_html', htmlStringify(message, stringifyOptions))
|
|
|
|
bot.console.log(message)
|
|
})
|
|
|
|
|
|
function tryParsingMessage (message, data) {
|
|
let parsed
|
|
for (const parser of bot.chat.parsers) {
|
|
parsed = parser(message, data)
|
|
if (parsed) break
|
|
}
|
|
|
|
if (!parsed) return
|
|
bot.emit('message', parsed)
|
|
}
|
|
}
|
|
|
|
module.exports = inject
|