Make the chat parsers have priority levels to prevent running generic parsers first

This commit is contained in:
7cc5c4f330d47060 2024-09-19 22:45:12 -04:00
parent 24ce5d13fe
commit 9d50d224ae
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
7 changed files with 90 additions and 29 deletions

View file

@ -3,6 +3,7 @@ const parsePlain = require('../util/chatparse_plain.js')
const parseConsole = require('../util/chatparse_console.js') const parseConsole = require('../util/chatparse_console.js')
const parse1204 = require('../util/parseNBT.js') const parse1204 = require('../util/parseNBT.js')
const { getMessage } = require('../util/lang.js') const { getMessage } = require('../util/lang.js')
const fs = require('fs')
const convertChatStyleItem = (item) => { const convertChatStyleItem = (item) => {
const output = {} const output = {}
for (const i in item) { 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 = { module.exports = {
load: (b) => { load: (b) => {
b.messageCount = 0 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.on('chat', (data) => {
b.messageCount++ b.messageCount++
if (Date.now() < b.chatDisabledUntil) return if (Date.now() < b.chatDisabledUntil) return

View file

@ -2,7 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js')
module.exports = { module.exports = {
parse: (data, b) => { parse: (data, b) => {
if (data.type === 'system' || data.type === 'legacy') { if (data.type === 'system' || data.type === 'legacy') {
if (data.parsed) return
if (data.json.translate === '%s %s %s' || data.json.translate === '[%s] %s %s') { if (data.json.translate === '%s %s %s' || data.json.translate === '[%s] %s %s') {
let subtype = 'chipmunkmod_' let subtype = 'chipmunkmod_'
if (data.json.translate === '%s %s %s') { if (data.json.translate === '%s %s %s') {
@ -16,8 +15,8 @@ module.exports = {
const uuid = b.findUUID(username) const uuid = b.findUUID(username)
const nickname = b.findDisplayName(uuid) const nickname = b.findDisplayName(uuid)
const message = parsePlain(data.json.with[2].extra) const message = parsePlain(data.json.with[2].extra)
data.parsed = true return {
b.emit('chat', { parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype, subtype,
@ -25,10 +24,11 @@ module.exports = {
message, message,
nickname, nickname,
username username
}) }
} else { } else {
subtype += '_invalid' subtype += '_invalid'
b.emit('chat', { return {
parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype, subtype,
@ -36,10 +36,13 @@ module.exports = {
message: '', message: '',
nickname: '', nickname: '',
username: '' username: ''
})
data.parsed = true
} }
} }
} }
} }
return {
parsed: false
}
},
priority: 0
} }

View file

@ -2,13 +2,13 @@ const parsePlain = require('../../util/chatparse_plain.js')
module.exports = { module.exports = {
parse: (data, b) => { parse: (data, b) => {
if (data.type === 'system' || data.type === 'legacy') { 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 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 username = parsePlain(data.json.extra[3])
const uuid = b.findUUID(username) const uuid = b.findUUID(username)
const nickname = b.findDisplayName(uuid) const nickname = b.findDisplayName(uuid)
const message = parsePlain(data.json.extra[5]) const message = parsePlain(data.json.extra[5])
b.emit('chat', { return {
parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype: `chipmunkmod_mcp_${data.type}`, subtype: `chipmunkmod_mcp_${data.type}`,
@ -16,8 +16,12 @@ module.exports = {
message, message,
nickname, nickname,
username username
})
} }
} }
} }
return {
parsed: false
}
},
priority: 0
} }

View file

@ -2,7 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js')
module.exports = { module.exports = {
parse: (data, b) => { parse: (data, b) => {
if (data.type === 'profileless') { if (data.type === 'profileless') {
if (data.parsed) return
if (data.playerChatType.translation_key === '%s') { if (data.playerChatType.translation_key === '%s') {
const parsed = parsePlain(data.json) const parsed = parsePlain(data.json)
const split = parsed.split(': ') const split = parsed.split(': ')
@ -12,7 +11,8 @@ module.exports = {
const username = b.findRealName(chatName) const username = b.findRealName(chatName)
const uuid = b.findUUID(username) const uuid = b.findUUID(username)
const message = split.join(': ') const message = split.join(': ')
b.emit('chat', { return {
parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype: 'extras_profileless', subtype: 'extras_profileless',
@ -20,8 +20,12 @@ module.exports = {
message, message,
nickname, nickname,
username username
})
} }
} }
} }
return {
parsed: false
}
},
priority: 1
} }

View file

@ -1,9 +1,8 @@
module.exports = { module.exports = {
parse: (data, b) => { parse: (data, b) => {
if (data.type === 'player' || data.type === 'profileless') { if (data.type === 'player' || data.type === 'profileless') {
if (data.parsed) return return {
if (data.type === 'profileless' && data.playerChatType.translation_key === '%s') return parsed: true,
b.emit('chat', {
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype: `generic_${data.type}`, subtype: `generic_${data.type}`,
@ -11,7 +10,11 @@ module.exports = {
message: data.message, message: data.message,
nickname: data.nickname, nickname: data.nickname,
username: data.username username: data.username
})
} }
} }
return {
parsed: false
}
},
priority: 2
} }

View file

@ -2,8 +2,6 @@ const parsePlain = require('../../util/chatparse_plain.js')
module.exports = { module.exports = {
parse: (data, b) => { parse: (data, b) => {
if (data.type === 'system' || data.type === 'legacy') { 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}` let subtype = `generic_${data.type}`
if (data.type === 'legacy' && data.uuid) subtype += '_withuuid' if (data.type === 'legacy' && data.uuid) subtype += '_withuuid'
const parsed = parsePlain(data.json) const parsed = parsePlain(data.json)
@ -13,7 +11,8 @@ module.exports = {
const nickname = chatNameSplit[chatNameSplit.length - 1] const nickname = chatNameSplit[chatNameSplit.length - 1]
const username = b.findRealName(chatName) const username = b.findRealName(chatName)
const uuid = b.findUUID(username) const uuid = b.findUUID(username)
b.emit('chat', { return {
parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype, subtype,
@ -21,7 +20,11 @@ module.exports = {
message: split.join(': '), message: split.join(': '),
nickname, nickname,
username username
})
} }
} }
return {
parsed: false
}
},
priority: 2
} }

View file

@ -14,7 +14,8 @@ module.exports = {
} }
const uuid = b.findUUID(username) const uuid = b.findUUID(username)
const nickname = b.findDisplayName(uuid) const nickname = b.findDisplayName(uuid)
b.emit('chat', { return {
parsed: true,
json: data.json, json: data.json,
type: data.type, type: data.type,
subtype, subtype,
@ -22,8 +23,12 @@ module.exports = {
message, message,
nickname, nickname,
username username
})
} }
} }
} }
return {
parsed: false
}
},
priority: 1
} }