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 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

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}