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