Rewrite chat parser

This commit is contained in:
7cc5c4f330d47060 2024-08-22 15:24:23 -04:00
parent 07e30a2a89
commit eca002bd5a
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA

View file

@ -2,120 +2,112 @@ const settings = require('../settings.json')
const parsePlain = require('../util/chatparse_plain.js') 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 messageTypes = [ const convertChatStyleItem = (item) => {
'', let output={};
'chat.type.emote', for(const i in item){
'commands.message.display.incoming', output[i] = item[i].value
'commands.message.display.outgoing', }
'', return output;
'chat.type.announcement', }
'chat.type.team.text', const convertChatTypeItem = (item) => {
'chat.type.team.sent' if(item.style){
] return {
translation_key: item.translation_key.value,
parameters: item.parameters.value.value,
style: convertChatStyleItem(item.style.value)
}
} else {
return {
translation_key: item.translation_key.value,
parameters: item.parameters.value.value,
style: {}
}
}
}
module.exports = { module.exports = {
load: (b) => { load: (b) => {
b.messageTypes = [];
b._client.on("registry_data",(data)=>{
if(data.codec.value['minecraft:chat_type']){
b.messageTypes = data.codec.value['minecraft:chat_type']
const nbtItems = data.codec.value['minecraft:chat_type'].value.value.value.value;
nbtItems.forEach((item, i) => {
b.messageTypes[i] = convertChatTypeItem(item.element.value.chat.value)
})
}
})
b._client.on('profileless_chat', (data) => { b._client.on('profileless_chat', (data) => {
if (data.type === 4) { const messageType = b.messageTypes[data.type]
const json = parse1204(data.message) let json = {translate: messageType.translation_key, with: []}
messageType.parameters.forEach((item, i)=>{
if(item == "content"){
json.with[i] = parse1204(data.message)
} else if(item == "sender"){
json.with[i] = parse1204(data.name)
} else if(item == "target"){
json.with[i] = parse1204(data.target)
}
})
for(const i in messageType.style){
json[i] = messageType.style[i]
}
let username = ""
let nickname = ""
let uuid = "00000000-0000-0000-0000-000000000000"
let message = ""
if (messageType.translation_key == "%s") {
const parsed = parsePlain(json) const parsed = parsePlain(json)
const split = parsed.split(': ') const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0] const chatName = split.splice(0, 1)[0]
const chatNameSplit = chatName.split(' ') const chatNameSplit = chatName.split(' ')
const nickname = chatNameSplit[chatNameSplit.length - 1] nickname = chatNameSplit[chatNameSplit.length - 1]
const username = b.findRealName(chatName) username = b.findRealName(chatName)
const uuid = b.findUUID(username) uuid = b.findUUID(username)
b.emit('chat', { message = split.join(": ")
json,
type: 'profileless',
uuid,
message: split.join(': '),
nickname,
username
})
} else if (data.type === 6 || data.type === 7) {
const uuid = b.findUUID(parsePlain(parse1204(data.name)))
const nickname = b.findDisplayName(uuid)
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.target),
parse1204(data.name),
data.message
]
},
type: 'profileless',
uuid,
message: parsePlain(data.message),
nickname,
username: parsePlain(parse1204(data.name))
})
} else { } else {
const uuid = b.findUUID(parsePlain(parse1204(data.name))) message = parsePlain(parse1204(data.message))
const nickname = b.findDisplayName(uuid) uuid = b.findUUID(parsePlain(parse1204(data.name)))
b.emit('chat', { nickname = b.findDisplayName(uuid)
json: { username = parsePlain(parse1204(data.name))
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.name),
parse1204(data.message)
]
},
type: 'profileless',
uuid,
message: parsePlain(parse1204(data.message)),
nickname,
username: parsePlain(parse1204(data.name))
})
} }
console.log()
b.emit('chat', {
json: json,
type: 'profileless',
uuid,
message,
nickname,
username
})
}) })
b._client.on('player_chat', (data) => { b._client.on('player_chat', (data) => {
if (data.type === 4) { const messageType = b.messageTypes[data.type]
b.emit('chat', { let json = {translate: messageType.translation_key, with: []}
json: parse1204(data.unsignedChatContent), messageType.parameters.forEach((item, i)=>{
type: 'player', if(item == "content"){
uuid: data.senderUuid, if(messageType.translation_key === "%s"){
message: data.plainMessage, json.with[i] = parse1204(data.unsignedChatContent)
nickname: parsePlain(parse1204(data.networkName)), } else {
username: b.findRealNameFromUUID(data.senderUuid) json.with[i] = data.plainMessage
}) }
} else if (data.type === 6 || data.type === 7) { } else if(item == "sender"){
b.emit('chat', { json.with[i] = parse1204(data.networkName)
json: { } else if(item == "target"){
translate: messageTypes[data.type], json.with[i] = parse1204(data.networkTargetName)
color: data.type === 2 ? 'gray' : 'reset', }
with: [ })
parse1204(data.networkTargetName), for(const i in messageType.style){
parse1204(data.networkName), json[i] = messageType.style[i]
data.plainMessage
]
},
type: 'player',
uuid: data.senderUuid,
message: parsePlain(data.plainMessage),
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
})
} else {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.networkName),
data.plainMessage
]
},
type: 'player',
uuid: data.senderUuid,
message: parsePlain(data.plainMessage),
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
})
} }
b.emit('chat', {
json: json,
type: 'player',
uuid: data.senderUuid,
message: data.plainMessage,
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
})
}) })
b._client.on('system_chat', (data) => { b._client.on('system_chat', (data) => {