2024-08-03 16:26:00 -04:00
|
|
|
const { parseText } = require('../util/chat/utility')
|
2024-03-17 23:52:58 -04:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2024-08-03 16:26:00 -04:00
|
|
|
const vanillaParser = require('../util/chat/message_parser/vanilla')
|
2024-03-17 23:52:58 -04:00
|
|
|
const kaboomParser = require('../util/chat/message_parser/kaboom')
|
2024-08-03 16:26:00 -04:00
|
|
|
const kaboomVanillaLikeParser = require('../util/chat/message_parser/kaboom_vanilla_like')
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
function inject (bot) {
|
2024-02-11 21:23:41 -05:00
|
|
|
bot.chat = {
|
|
|
|
queue: [],
|
2024-08-03 16:26:00 -04:00
|
|
|
parsers: [vanillaParser, kaboomParser, kaboomVanillaLikeParser],
|
2024-03-17 23:52:58 -04:00
|
|
|
|
2024-02-12 21:54:29 -05:00
|
|
|
message (message) {
|
2024-08-03 16:26:00 -04:00
|
|
|
if (!bot.supportFeature('signedChat')) {
|
|
|
|
bot._client.write('chat', { message })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 21:54:29 -05:00
|
|
|
bot._client.write('chat_message', {
|
|
|
|
message,
|
|
|
|
timestamp: BigInt(Date.now()),
|
|
|
|
salt: 0n,
|
|
|
|
offset: 0,
|
2024-03-01 22:41:57 -05:00
|
|
|
acknowledged: Buffer.allocUnsafe(3)
|
2024-02-12 21:54:29 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
command (command) {
|
2024-08-03 16:26:00 -04:00
|
|
|
if (!bot.supportFeature('signedChat')) {
|
|
|
|
bot._client.write('chat', { message: '/' + command })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 21:54:29 -05:00
|
|
|
bot._client.write('chat_command', {
|
|
|
|
command,
|
|
|
|
timestamp: BigInt(Date.now()),
|
|
|
|
salt: 0n,
|
|
|
|
argumentSignatures: [],
|
|
|
|
signedPreview: false,
|
|
|
|
messageCount: 0,
|
|
|
|
acknowledged: Buffer.alloc(3),
|
|
|
|
previousMessages: []
|
|
|
|
})
|
|
|
|
}
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-02-12 21:54:29 -05:00
|
|
|
|
2024-02-11 21:23:41 -05:00
|
|
|
setInterval(() => {
|
2024-03-17 23:52:58 -04:00
|
|
|
if (!bot.loggedIn) return
|
2024-02-11 21:23:41 -05:00
|
|
|
|
|
|
|
const message = bot.chat.queue.shift()
|
|
|
|
if (message != null) {
|
2024-02-12 21:54:29 -05:00
|
|
|
if (message[0] === '/') bot.chat.command(message.substring(1))
|
|
|
|
else bot.chat.message(message)
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
}, 200)
|
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.on('packet.profileless_chat', (packet) => {
|
2024-08-03 16:26:00 -04:00
|
|
|
const message = parseText(packet.message)
|
|
|
|
const senderName = parseText(packet.name)
|
2024-03-16 16:03:36 -04:00
|
|
|
const type = bot.registry?.chatFormattingById[packet.type]
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.emit('profileless_chat', { message, senderName, type })
|
|
|
|
bot.emit('chat', message)
|
2024-03-17 23:52:58 -04:00
|
|
|
|
|
|
|
tryParsingMessage(message, { senderName, players: bot.players, lang: bot.registry.language })
|
2024-02-11 21:23:41 -05:00
|
|
|
})
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
bot.on('packet.player_chat', (packet) => {
|
|
|
|
const plain = packet.plainMessage
|
2024-08-03 16:26:00 -04:00
|
|
|
const unsigned = parseText(packet.unsignedChatContent)
|
2024-04-02 17:53:10 -04:00
|
|
|
let sender = bot.players.find(player => player.uuid === packet.senderUuid)
|
2024-03-16 16:03:36 -04:00
|
|
|
const type = bot.registry?.chatFormattingById[packet.type]
|
2024-02-12 21:54:29 -05:00
|
|
|
|
2024-04-02 17:53:10 -04:00
|
|
|
if (!sender) {
|
|
|
|
bot.console.warn(`Unable to find player_chat sender in player list (uuid: ${packet.senderUuid})`)
|
|
|
|
sender = undefined
|
|
|
|
}
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
bot.emit('player_chat', { plain, unsigned, sender, type: type.name })
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.emit('chat', unsigned)
|
2024-03-17 23:52:58 -04:00
|
|
|
|
2024-04-02 17:53:10 -04:00
|
|
|
tryParsingMessage(unsigned, { senderUuid: sender?.uuid, players: bot.players, lang: bot.registry.language, plain })
|
2024-02-12 21:54:29 -05:00
|
|
|
})
|
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
bot.on('packet.system_chat', (packet) => {
|
2024-03-16 16:03:36 -04:00
|
|
|
if (packet.isActionBar) return
|
|
|
|
|
2024-08-03 16:26:00 -04:00
|
|
|
const message = parseText(packet.content)
|
2024-03-16 16:03:36 -04:00
|
|
|
if (message?.translate === 'advMode.setCommand.success') return
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.emit('system_chat', message)
|
|
|
|
bot.emit('chat', message)
|
2024-02-11 21:23:41 -05:00
|
|
|
})
|
2024-03-17 23:52:58 -04:00
|
|
|
|
2024-08-03 16:26:00 -04:00
|
|
|
// legacy (1.18.2 and older)
|
|
|
|
bot.on('packet.chat', packet => {
|
|
|
|
const message = parseText(packet.message)
|
|
|
|
bot.emit('chat', message) // if only it were this easy nowadays...
|
|
|
|
|
|
|
|
switch (packet.position) {
|
|
|
|
case 0: tryParsingMessage(message, { senderUuid: packet.sender, players: bot.players, lang: bot.registry.language }); break
|
|
|
|
case 1: bot.emit('system_chat', message); break
|
|
|
|
// TODO: 2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
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)
|
|
|
|
}
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
module.exports = inject
|