diff --git a/plugins/chat.js b/plugins/chat.js index e59bbfc..8e933c0 100755 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -3,6 +3,7 @@ const parsePlain = require('../util/chatparse_plain.js') const parseConsole = require('../util/chatparse_console.js') const parse1204 = require('../util/parseNBT.js') const { getMessage } = require('../util/lang.js') +const fs = require('fs') const convertChatStyleItem = (item) => { const output = {} for (const i in item) { @@ -25,6 +26,22 @@ const convertChatTypeItem = (item) => { } } } + +// Level 0: highly specific parsers for certain players +// Level 1: server chat format parsers +// Level 2: generic parsers +const parsers = [[], [], []] +const bpl = fs.readdirSync('plugins/chatParsers') +for (const plugin of bpl) { + if (!plugin.endsWith('.js')) { + continue + } + try { + const parser = require(`./chatParsers/${plugin}`) + parsers[parser.priority].push(parser.parse) + } catch (e) { console.log(e) } +} + module.exports = { load: (b) => { b.messageCount = 0 @@ -135,6 +152,28 @@ module.exports = { }) }) + b.on('chat_unparsed', (data) => { + for (const lvl of parsers) { + for (const item of lvl) { + const output = item(data, b) + if (output.parsed) { + b.emit('chat', output) + return + } + } + } + b.emit('chat', { + parsed: true, + json: data.json, + type: data.type, + subtype: 'fallback', + uuid: '00000000-0000-0000-0000-000000000000', + message: '', + nickname: '', + username: '' + }) + }) + b.on('chat', (data) => { b.messageCount++ if (Date.now() < b.chatDisabledUntil) return diff --git a/plugins/chatParsers/chat_cmm.js b/plugins/chatParsers/chat_cmm.js index a062c3c..e347f6f 100755 --- a/plugins/chatParsers/chat_cmm.js +++ b/plugins/chatParsers/chat_cmm.js @@ -2,7 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js') module.exports = { parse: (data, b) => { if (data.type === 'system' || data.type === 'legacy') { - if (data.parsed) return if (data.json.translate === '%s %s › %s' || data.json.translate === '[%s] %s › %s') { let subtype = 'chipmunkmod_' if (data.json.translate === '%s %s › %s') { @@ -16,8 +15,8 @@ module.exports = { const uuid = b.findUUID(username) const nickname = b.findDisplayName(uuid) const message = parsePlain(data.json.with[2].extra) - data.parsed = true - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype, @@ -25,10 +24,11 @@ module.exports = { message, nickname, username - }) + } } else { subtype += '_invalid' - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype, @@ -36,10 +36,13 @@ module.exports = { message: '', nickname: '', username: '' - }) - data.parsed = true + } } } } - } + return { + parsed: false + } + }, + priority: 0 } diff --git a/plugins/chatParsers/chat_cmm_mcp.js b/plugins/chatParsers/chat_cmm_mcp.js index 709c161..bddfaaf 100755 --- a/plugins/chatParsers/chat_cmm_mcp.js +++ b/plugins/chatParsers/chat_cmm_mcp.js @@ -2,13 +2,13 @@ const parsePlain = require('../../util/chatparse_plain.js') module.exports = { parse: (data, b) => { if (data.type === 'system' || data.type === 'legacy') { - if (data.parsed) return if (data.json.extra && data.json.extra[4] && data.json.extra[3] && data.json.extra[5] && data.json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player const username = parsePlain(data.json.extra[3]) const uuid = b.findUUID(username) const nickname = b.findDisplayName(uuid) const message = parsePlain(data.json.extra[5]) - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype: `chipmunkmod_mcp_${data.type}`, @@ -16,8 +16,12 @@ module.exports = { message, nickname, username - }) + } } } - } + return { + parsed: false + } + }, + priority: 0 } diff --git a/plugins/chatParsers/chat_extras_profileless.js b/plugins/chatParsers/chat_extras_profileless.js index 0a8deca..8ce318c 100755 --- a/plugins/chatParsers/chat_extras_profileless.js +++ b/plugins/chatParsers/chat_extras_profileless.js @@ -2,7 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js') module.exports = { parse: (data, b) => { if (data.type === 'profileless') { - if (data.parsed) return if (data.playerChatType.translation_key === '%s') { const parsed = parsePlain(data.json) const split = parsed.split(': ') @@ -12,7 +11,8 @@ module.exports = { const username = b.findRealName(chatName) const uuid = b.findUUID(username) const message = split.join(': ') - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype: 'extras_profileless', @@ -20,8 +20,12 @@ module.exports = { message, nickname, username - }) + } } } - } + return { + parsed: false + } + }, + priority: 1 } diff --git a/plugins/chatParsers/chat_player.js b/plugins/chatParsers/chat_player.js index be5eaf1..1c3d283 100755 --- a/plugins/chatParsers/chat_player.js +++ b/plugins/chatParsers/chat_player.js @@ -1,9 +1,8 @@ module.exports = { parse: (data, b) => { if (data.type === 'player' || data.type === 'profileless') { - if (data.parsed) return - if (data.type === 'profileless' && data.playerChatType.translation_key === '%s') return - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype: `generic_${data.type}`, @@ -11,7 +10,11 @@ module.exports = { message: data.message, nickname: data.nickname, username: data.username - }) + } } - } + return { + parsed: false + } + }, + priority: 2 } diff --git a/plugins/chatParsers/chat_system.js b/plugins/chatParsers/chat_system.js index cf1b1ea..1ed28fc 100755 --- a/plugins/chatParsers/chat_system.js +++ b/plugins/chatParsers/chat_system.js @@ -2,8 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js') module.exports = { parse: (data, b) => { if (data.type === 'system' || data.type === 'legacy') { - if (data.parsed) return - if (data.type === 'legacy' && data.json.translate === 'chat.type.text') return let subtype = `generic_${data.type}` if (data.type === 'legacy' && data.uuid) subtype += '_withuuid' const parsed = parsePlain(data.json) @@ -13,7 +11,8 @@ module.exports = { const nickname = chatNameSplit[chatNameSplit.length - 1] const username = b.findRealName(chatName) const uuid = b.findUUID(username) - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype, @@ -21,7 +20,11 @@ module.exports = { message: split.join(': '), nickname, username - }) + } } - } + return { + parsed: false + } + }, + priority: 2 } diff --git a/plugins/chatParsers/chat_vanilla_legacy.js b/plugins/chatParsers/chat_vanilla_legacy.js index e70246b..e68b1e5 100755 --- a/plugins/chatParsers/chat_vanilla_legacy.js +++ b/plugins/chatParsers/chat_vanilla_legacy.js @@ -14,7 +14,8 @@ module.exports = { } const uuid = b.findUUID(username) const nickname = b.findDisplayName(uuid) - b.emit('chat', { + return { + parsed: true, json: data.json, type: data.type, subtype, @@ -22,8 +23,12 @@ module.exports = { message, nickname, username - }) + } } } - } + return { + parsed: false + } + }, + priority: 1 }