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)
message = split.join(": ")
} else {
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', { b.emit('chat', {
json, json: json,
type: 'profileless', type: 'profileless',
uuid, uuid,
message: split.join(': '), message,
nickname, nickname,
username 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 {
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))
})
}
}) })
b._client.on('player_chat', (data) => { b._client.on('player_chat', (data) => {
if (data.type === 4) { 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', { b.emit('chat', {
json: parse1204(data.unsignedChatContent), json: json,
type: 'player', type: 'player',
uuid: data.senderUuid, uuid: data.senderUuid,
message: data.plainMessage, message: data.plainMessage,
nickname: parsePlain(parse1204(data.networkName)), nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid) 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)
})
}
}) })
b._client.on('system_chat', (data) => { b._client.on('system_chat', (data) => {