Update main/chatparser.js

This commit is contained in:
Yaode_owo 2024-10-12 09:27:44 -04:00
parent 130f4f4e62
commit cacadafd59

View file

@ -1,273 +1,282 @@
const lang = require("./en_us.json"); // translate message const lang = require("./en_us.json"); // translate message
const nbt = require('prismarine-nbt'); const nbt = require('prismarine-nbt');
function uuidFromIntArray (arr) { function uuidFromIntArray (arr) {
const buf = Buffer.alloc(16) const buf = Buffer.alloc(16)
arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) }) arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) })
return buf.toString('hex') return buf.toString('hex')
} }
function processNbtMessage(msg) { function processNbtMessage(msg) {
try { // RangeError: Maximum call stack size exceeded try {
if (!msg || msg.type === 'end' || msg === undefined) return undefined if (typeof msg === 'string') { // just create simple nbt structure
const simplified = nbt.simplify(msg) msg = {
const json = JSON.stringify(simplified, (key, val) => { id: 'string',
if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val) value: msg
return val };
}) }
return json
} catch (e) { if (!msg || msg.type === 'end') return msg;
return '{"text":""}';
} const simplified = nbt.simplify(msg); // Ensure nbt is defined elsewhere
} const json = JSON.stringify(simplified, (key, val) => {
if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val);
function inject(bot) { return val;
});
bot.on('system_chat', (packet) => { // system
if (packet.isActionBar) return; return json;
let systemchat = {}; } catch (e) {
systemchat.jsonMsg = packet.content; return '{"text":""}';
systemchat.message = parseMinecraftMessage(processNbtMessage(packet.content)); }
systemchat.nocolor_message = parseMinecraftMessageNoColor(processNbtMessage(packet.content)); }
bot.emit('Custom_SystemChat', systemchat.message, systemchat);
bot.emit('Custom_AllChat', systemchat.message, systemchat); function inject(bot) {
});
bot.on('system_chat', (packet) => { // system
if (packet.isActionBar) return;
bot.on('profileless_chat', (packet) => { // sudo and vanish let systemchat = {};
let profilelesschat = {}; systemchat.jsonMsg = packet.content;
systemchat.message = parseMinecraftMessage(processNbtMessage(packet.content));
profilelesschat.type = packet.type systemchat.nocolor_message = parseMinecraftMessageNoColor(processNbtMessage(packet.content));
bot.emit('Custom_SystemChat', systemchat.message, systemchat);
profilelesschat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.message)) bot.emit('Custom_AllChat', systemchat.message, systemchat);
profilelesschat.senderName = parseMinecraftMessage(processNbtMessage(packet.name)) });
profilelesschat.targetName = parseMinecraftMessage(processNbtMessage(packet.target))
profilelesschat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.message)) bot.on('profileless_chat', (packet) => { // sudo and vanish
profilelesschat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.name)) let profilelesschat = {};
profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.target))
profilelesschat.type = packet.type
bot.emit('Custom_ProfilelessChat', profilelesschat.formattedMessage, profilelesschat)
bot.emit('Custom_AllChat', profilelesschat.formattedMessage, profilelesschat) profilelesschat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.message))
}) profilelesschat.senderName = parseMinecraftMessage(processNbtMessage(packet.name))
profilelesschat.targetName = parseMinecraftMessage(processNbtMessage(packet.target))
bot.on('player_chat', (packet) => { // player
let playerchat = {}; profilelesschat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.message))
profilelesschat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.name))
playerchat.plainMessage = packet.plainMessage; profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.target))
playerchat.type = packet.type;
playerchat.sender = packet.senderUuid; bot.emit('Custom_ProfilelessChat', profilelesschat.formattedMessage, profilelesschat)
bot.emit('Custom_AllChat', profilelesschat.formattedMessage, profilelesschat)
playerchat.unsignedContent = parseMinecraftMessage(processNbtMessage(packet.unsignedChatContent)); })
playerchat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.formattedMessage));
playerchat.senderName = parseMinecraftMessage(processNbtMessage(packet.networkName)); bot.on('player_chat', (packet) => { // player
playerchat.targetName = parseMinecraftMessage(processNbtMessage(packet.networkTargetName)); let playerchat = {};
playerchat.nocolor_unsignedContent = parseMinecraftMessageNoColor(processNbtMessage(packet.unsignedChatContent)); playerchat.plainMessage = packet.plainMessage;
playerchat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.formattedMessage)); playerchat.type = packet.type;
playerchat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkName)); playerchat.sender = packet.senderUuid;
playerchat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkTargetName));
playerchat.unsignedContent = parseMinecraftMessage(processNbtMessage(packet.unsignedChatContent));
switch (playerchat.type) { playerchat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.formattedMessage));
case 1: // /me text playerchat.senderName = parseMinecraftMessage(processNbtMessage(packet.networkName));
msg = `* ${playerchat.senderName} ${playerchat.formattedMessage}`; playerchat.targetName = parseMinecraftMessage(processNbtMessage(packet.networkTargetName));
playerchat.nocolor_msg = `* ${playerchat.nocolor_senderName} ${playerchat.nocolor_formattedMessage}`;
break; playerchat.nocolor_unsignedContent = parseMinecraftMessageNoColor(processNbtMessage(packet.unsignedChatContent));
case 2: // someone /tell you text playerchat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.formattedMessage));
msg = `${playerchat.senderName} whispers to you: ${playerchat.plainMessage || playerchat.formattedMessage}`; playerchat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkName));
playerchat.nocolor_msg = `${playerchat.nocolor_senderName} whispers to you: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; playerchat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkTargetName));
break;
case 3: // you /tell someone text switch (playerchat.type) {
msg = `You whisper to ${playerchat.targetName}: ${playerchat.plainMessage || playerchat.formattedMessage}`; case 1: // /me text
playerchat.nocolor_msg = `You whisper to ${NoColorTargetName}: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; msg = `* ${playerchat.senderName} ${playerchat.formattedMessage}`;
break; playerchat.nocolor_msg = `* ${playerchat.nocolor_senderName} ${playerchat.nocolor_formattedMessage}`;
case 4: // player chat break;
msg = playerchat.unsignedContent; case 2: // someone /tell you text
playerchat.nocolor_msg = playerchat.nocolor_unsignedContent; msg = `${playerchat.senderName} whispers to you: ${playerchat.plainMessage || playerchat.formattedMessage}`;
break; playerchat.nocolor_msg = `${playerchat.nocolor_senderName} whispers to you: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
case 5: // /say text break;
msg = `[${playerchat.senderName}] ${playerchat.plainMessage || playerchat.formattedMessage}`; case 3: // you /tell someone text
playerchat.nocolor_msg = `[${playerchat.nocolor_senderName}] ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; msg = `You whisper to ${playerchat.targetName}: ${playerchat.plainMessage || playerchat.formattedMessage}`;
break; playerchat.nocolor_msg = `You whisper to ${NoColorTargetName}: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
case 6: // /minecraft:teammsg text break;
msg = `${playerchat.targetName} <${playerchat.senderName}> ${playerchat.plainMessage}`; case 4: // player chat
playerchat.nocolor_msg = `${playerchat.nocolor_targetName} <${playerchat.nocolor_senderName}> ${playerchat.plainMessage}`; msg = playerchat.unsignedContent;
break; playerchat.nocolor_msg = playerchat.nocolor_unsignedContent;
default: break;
console.log(`Unknown player_chat packet. Type: ${playerchat.type}`); case 5: // /say text
console.log(packet); msg = `[${playerchat.senderName}] ${playerchat.plainMessage || playerchat.formattedMessage}`;
break; playerchat.nocolor_msg = `[${playerchat.nocolor_senderName}] ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
} break;
bot.emit('custom_PlayerChat', msg, playerchat); case 6: // /minecraft:teammsg text
bot.emit('Custom_AllChat', msg, playerchat) msg = `${playerchat.targetName} <${playerchat.senderName}> ${playerchat.plainMessage}`;
}); playerchat.nocolor_msg = `${playerchat.nocolor_targetName} <${playerchat.nocolor_senderName}> ${playerchat.plainMessage}`;
break;
const ansiColorCodes = { default:
'§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m', console.log(`Unknown player_chat packet. Type: ${playerchat.type}`);
'§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m', console.log(packet);
'§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m', break;
'§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m', }
'black': '\x1B[30m', 'dark_blue': '\x1B[34m', 'dark_green': '\x1B[32m', bot.emit('custom_PlayerChat', msg, playerchat);
'dark_aqua': '\x1B[36m', 'dark_red': '\x1B[31m', 'dark_purple': '\x1B[35m', bot.emit('Custom_AllChat', msg, playerchat)
'gold': '\x1B[33m', 'gray': '\x1B[37m', 'dark_gray': '\x1B[90m', 'blue': '\x1B[94m', });
'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m',
'yellow': '\x1B[93m', 'white': '\x1B[97m' const ansiColorCodes = {
}; '§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m',
'§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m',
const ansiFormatCodes = { '§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m',
'§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m', '§k': '\x1B[5m', '§r': '\x1B[0m', '§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m',
'bold': '\x1B[1m', 'italic': '\x1B[3m', 'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m', 'black': '\x1B[30m', 'dark_blue': '\x1B[34m', 'dark_green': '\x1B[32m',
}; 'dark_aqua': '\x1B[36m', 'dark_red': '\x1B[31m', 'dark_purple': '\x1B[35m',
'gold': '\x1B[33m', 'gray': '\x1B[37m', 'dark_gray': '\x1B[90m', 'blue': '\x1B[94m',
function parseMinecraftMessage(component) { 'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m',
if (component === undefined) return; 'yellow': '\x1B[93m', 'white': '\x1B[97m'
};
let jsonComponent;
try { const ansiFormatCodes = {
jsonComponent = JSON.parse(component); '§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m', '§k': '\x1B[5m', '§r': '\x1B[0m',
} catch (e) { 'bold': '\x1B[1m', 'italic': '\x1B[3m', 'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m',
console.error("Invalid JSON format:", component); };
return '';
} function parseMinecraftMessage(component) {
if (component === undefined) return;
function extractText(comp) {
let text = ''; let jsonComponent;
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) { try {
text += comp.text; jsonComponent = JSON.parse(component);
} } catch (e) {
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) { console.error("Invalid JSON format:", component);
text += comp[""]; return '';
} }
if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
return comp; function extractText(comp) {
} let text = '';
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
if (comp.extra) { text += comp.text;
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] }
comp.extra.forEach(subComp => { if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
text += formatfunction(comp, extractText(subComp)); text += comp[""];
}); }
} if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
return comp;
if (comp.translate) { }
let translateString = lang[comp.translate] || comp.translate;
if (comp.with) { if (comp.extra) {
const withArgs = comp.with.map(arg => { return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(arg) }); if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
let usedReplacements = 0; text += formatfunction(comp, extractText(subComp));
translateString = translateString.replace(/%s/g, () => { });
if (usedReplacements < withArgs.length) { }
return formatfunction(comp, withArgs[usedReplacements++]);
} if (comp.translate) {
return "%s"; let translateString = lang[comp.translate] || comp.translate;
}); if (comp.with) {
const withArgs = comp.with.map(arg => { return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(arg) });
withArgs.forEach((arg, index) => {
const regex = new RegExp(`%${index + 1}\\$s`, 'g'); let usedReplacements = 0;
translateString = translateString.replace(regex, formatfunction(comp, arg)); translateString = translateString.replace(/%s/g, () => {
}); if (usedReplacements < withArgs.length) {
} return formatfunction(comp, withArgs[usedReplacements++]);
text += formatfunction(comp, translateString); }
} return "%s";
});
text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
return text; withArgs.forEach((arg, index) => {
} const regex = new RegExp(`%${index + 1}\\$s`, 'g');
return extractText(jsonComponent) + ansiFormatCodes['reset']; translateString = translateString.replace(regex, formatfunction(comp, arg));
} });
}
function formatfunction(comp, text) { text += formatfunction(comp, translateString);
if (text === undefined) return ''; }
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
} text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
return text;
function parseMinecraftColor(color) { }
if (color && ansiColorCodes[color] && !color.startsWith('#')) { return extractText(jsonComponent) + ansiFormatCodes['reset'];
return ansiColorCodes[color]; }
} else if (color && color.startsWith('#')) {
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/; function formatfunction(comp, text) {
const hexCodes = hexRegex.exec(color); if (text === undefined) return '';
if (hexCodes) { return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
const red = parseInt(hexCodes[1], 16); }
const green = parseInt(hexCodes[2], 16);
const blue = parseInt(hexCodes[3], 16); function parseMinecraftColor(color) {
const ansiColor = `\u001b[38;2;${red};${green};${blue}m`; if (color && ansiColorCodes[color] && !color.startsWith('#')) {
return ansiColor; return ansiColorCodes[color];
} } else if (color && color.startsWith('#')) {
} else { const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
return ''; const hexCodes = hexRegex.exec(color);
} if (hexCodes) {
} const red = parseInt(hexCodes[1], 16);
const green = parseInt(hexCodes[2], 16);
function parseMinecraftFormat(format) { const blue = parseInt(hexCodes[3], 16);
let result = ''; const ansiColor = `\u001b[38;2;${red};${green};${blue}m`;
if (format.bold && format.bold === 1) result += ansiFormatCodes['bold']; return ansiColor;
if (format.italic && format.italic === 1) result += ansiFormatCodes['italic']; }
if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined']; } else {
if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough']; return '';
if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated']; }
return result; }
}
function parseMinecraftFormat(format) {
let result = '';
function parseMinecraftMessageNoColor(component) { if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
if (component === undefined) return; if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
let jsonComponent; if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
try { if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
jsonComponent = JSON.parse(component); if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
} catch (e) { return result;
console.error("Invalid JSON format:", component); }
}
function extractText(comp) { function parseMinecraftMessageNoColor(component) {
let text = ''; if (component === undefined) return;
let jsonComponent;
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) { try {
text += comp.text; jsonComponent = JSON.parse(component);
} } catch (e) {
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) { console.error("Invalid JSON format:", component);
text += comp[""]; }
}
if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) { function extractText(comp) {
return comp; let text = '';
}
if (comp.extra) { if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] text += comp.text;
comp.extra.forEach(subComp => { }
text += extractText(subComp); if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
}); text += comp[""];
} }
if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
if (comp.translate) { return comp;
let translateString = lang[comp.translate] || comp.translate; }
if (comp.with) { if (comp.extra) {
const withArgs = comp.with.map(arg => extractText(arg)); if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
let usedReplacements = 0; text += extractText(subComp);
translateString = translateString.replace(/%s/g, () => { });
if (usedReplacements < withArgs.length) { }
return formatfunction(comp, withArgs[usedReplacements++]);
} if (comp.translate) {
return "%s"; let translateString = lang[comp.translate] || comp.translate;
}); if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => {
const regex = new RegExp(`%${index + 1}\\$s`, 'g'); let usedReplacements = 0;
translateString = translateString.replace(regex, arg); translateString = translateString.replace(/%s/g, () => {
}); if (usedReplacements < withArgs.length) {
} return formatfunction(comp, withArgs[usedReplacements++]);
text += translateString; }
} return "%s";
});
return text; withArgs.forEach((arg, index) => {
} const regex = new RegExp(`%${index + 1}\\$s`, 'g');
msgText = extractText(jsonComponent) translateString = translateString.replace(regex, arg);
// msgText = msgText.replace('§', '&') });
return msgText; }
} text += translateString;
}
}
return text;
}
msgText = extractText(jsonComponent)
// msgText = msgText.replace('§', '&')
return msgText;
}
}
module.exports = { inject }; module.exports = { inject };