Info in chatparser.js
This commit is contained in:
parent
dbed9db104
commit
6c01f81814
3 changed files with 1006 additions and 324 deletions
|
@ -1,14 +1,28 @@
|
|||
// Don't change this or you gay.
|
||||
// Version: 1.3
|
||||
/**
|
||||
* Minecraft Version:
|
||||
* - 1.20.4
|
||||
* - 1.20.5
|
||||
* - 1.20.6
|
||||
*
|
||||
* Language Version:
|
||||
* - 1.21.1
|
||||
*
|
||||
* Version:
|
||||
* - 1.4T (Test Version)
|
||||
*
|
||||
* Update:
|
||||
* - Keybind added
|
||||
* - Profileless chat with no color messages
|
||||
* - Rewritten color and format handling
|
||||
* - Edit error output
|
||||
*
|
||||
* Maybe Issues:
|
||||
* - Incorrect parsing colors or formats
|
||||
*/
|
||||
|
||||
|
||||
const lang = require("./en_us.json"); // translate message
|
||||
|
||||
function uuidFromIntArray (arr) {
|
||||
const buf = Buffer.alloc(16)
|
||||
arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) })
|
||||
return buf.toString('hex')
|
||||
}
|
||||
|
||||
function simplify (data) {
|
||||
try { // Prevent RangeError
|
||||
if (data === undefined) return data;
|
||||
|
@ -26,18 +40,13 @@ function simplify (data) {
|
|||
}
|
||||
return transform(data.value, data.type)
|
||||
} catch (e) {
|
||||
return e;
|
||||
return '\x1B[91m*** Component is too complex ***\x1B[0m';
|
||||
}
|
||||
}
|
||||
|
||||
function formatfunction(comp, text) {
|
||||
if (text === undefined) return '';
|
||||
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
|
||||
}
|
||||
|
||||
function parseMinecraftColor(color) {
|
||||
if (typeof color === 'string' && ansiColorCodes[color] && !color.startsWith('#')) {
|
||||
return ansiColorCodes[color];
|
||||
if (typeof color === 'string' && ansiMap[color] && !color.startsWith('#')) {
|
||||
return { color: ansiMap[color], have: true };
|
||||
} else if (typeof color === 'string' && color.startsWith('#')) {
|
||||
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
|
||||
const hexCodes = hexRegex.exec(color);
|
||||
|
@ -45,22 +54,22 @@ function parseMinecraftColor(color) {
|
|||
const red = parseInt(hexCodes[1], 16);
|
||||
const green = parseInt(hexCodes[2], 16);
|
||||
const blue = parseInt(hexCodes[3], 16);
|
||||
const ansiColor = `\u001b[38;2;${red};${green};${blue}m`;
|
||||
return ansiColor;
|
||||
return { color: `\u001b[38;2;${red};${green};${blue}m`, have: true };
|
||||
}
|
||||
} else {
|
||||
return '';
|
||||
return { color: '', have: false };
|
||||
}
|
||||
}
|
||||
|
||||
function parseMinecraftFormat(format) {
|
||||
let result = '';
|
||||
if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
|
||||
if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
|
||||
if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
|
||||
if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
|
||||
if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
|
||||
return result;
|
||||
if (!format) return { format: result, have: false };
|
||||
if (format.bold === 1) result += ansiMap['bold'];
|
||||
if (format.italic === 1) result += ansiMap['italic'];
|
||||
if (format.underlined === 1) result += ansiMap['underlined'];
|
||||
if (format.strikethrough === 1) result += ansiMap['strikethrough'];
|
||||
if (format.obfuscated === 1) result += ansiMap['obfuscated'];
|
||||
return result !== '' ? { format: result, have: true } : { format: '', have: false };
|
||||
}
|
||||
|
||||
function inject(bot) {
|
||||
|
@ -72,35 +81,35 @@ let actionbar = {};
|
|||
let bossbar = {};
|
||||
let title = {};
|
||||
|
||||
bot.on('end', () => { // Memory Leak Removed.
|
||||
playerchat = {}; delete playerchat;
|
||||
systemchat = {}; delete systemchat;
|
||||
profilelesschat = {}; delete profilelesschat;
|
||||
actionbar = {}; delete actionbar;
|
||||
bossbar = {}; delete bossbar;
|
||||
title = {}; delete title;
|
||||
})
|
||||
bot.on('end', () => {
|
||||
playerChat = {};
|
||||
systemChat = {};
|
||||
profilelessChat = {};
|
||||
actionbar = {};
|
||||
bossbar = {};
|
||||
title = {};
|
||||
});
|
||||
|
||||
bot.on('set_title_text', (packet) => {
|
||||
title.message = parseMinecraftMessage(simplify(packet.text));
|
||||
title.nocolor_message = parseMinecraftMessageNoColor(simplify(packet.text));
|
||||
title.noColor_message = parseMinecraftMessageNoColor(simplify(packet.text));
|
||||
|
||||
bot.emit('custom_title', title.message, title, packet);
|
||||
bot.emit('custom_allchat', 'title', title.message, title, packet);
|
||||
bot.emit('custom_allmessage', 'Title', title.message, title, packet);
|
||||
|
||||
title.message = undefined;
|
||||
title.nocolor_message = undefined;
|
||||
title.noColor_message = undefined;
|
||||
});
|
||||
|
||||
bot.on('set_title_subtitle', (packet) => {
|
||||
title.submessage = parseMinecraftMessage(simplify(packet.text));
|
||||
title.nocolor_submessage = parseMinecraftMessageNoColor(simplify(packet.text));
|
||||
title.subMessage = parseMinecraftMessage(simplify(packet.text));
|
||||
title.noColor_subMessage = parseMinecraftMessageNoColor(simplify(packet.text));
|
||||
|
||||
bot.emit('custom_subtitle', title.submessage, title, packet);
|
||||
bot.emit('custom_allchat', 'subtitle', title.submessage, title, packet);
|
||||
bot.emit('custom_subtitle', title.subMessage, title, packet);
|
||||
bot.emit('custom_allmessage', 'subtitle', title.subMessage, title, packet);
|
||||
|
||||
title.submessage = undefined;
|
||||
title.nocolor_submessage = undefined;
|
||||
title.subMessage = undefined;
|
||||
title.noColor_subMessage = undefined;
|
||||
});
|
||||
|
||||
bot.on('set_title_time', (packet) => {
|
||||
|
@ -109,7 +118,7 @@ bot.on('set_title_time', (packet) => {
|
|||
title.fadeOut = packet.fadeOut;
|
||||
|
||||
bot.emit('custom_timetitle', undefined, title, packet);
|
||||
bot.emit('custom_allchat', 'timetitle', undefined, title, packet);
|
||||
bot.emit('custom_allmessage', 'timetitle', undefined, title, packet);
|
||||
});
|
||||
|
||||
bot.on('boss_bar', (packet) => {
|
||||
|
@ -153,7 +162,7 @@ bot.on('boss_bar', (packet) => {
|
|||
}
|
||||
|
||||
bot.emit('custom_bossbar', bossbar.message, bossbar, packet);
|
||||
bot.emit('custom_allchat', 'bossbar', bossbar.message, bossbar, packet);
|
||||
bot.emit('custom_allmessage', 'bossbar', bossbar.message, bossbar, packet);
|
||||
});
|
||||
|
||||
bot.on('action_bar', (packet) => {
|
||||
|
@ -161,7 +170,7 @@ bot.on('action_bar', (packet) => {
|
|||
actionbar.message = parseMinecraftMessage(simplify(packet.text));
|
||||
actionbar.nocolor_message = parseMinecraftMessageNoColor(simplify(packet.text))
|
||||
bot.emit('custom_actionbar', actionbar.message, actionbar, packet);
|
||||
bot.emit('custom_allchat', 'actionbar', actionbar.message, actionbar, packet);
|
||||
bot.emit('custom_allmessage', 'actionbar', actionbar.message, actionbar, packet);
|
||||
});
|
||||
|
||||
|
||||
|
@ -171,13 +180,11 @@ bot.on('system_chat', (packet) => { // system
|
|||
systemchat.jsonMsg = simplify(packet.content);
|
||||
systemchat.message = parseMinecraftMessage(simplify(packet.content));
|
||||
systemchat.nocolor_message = parseMinecraftMessageNoColor(simplify(packet.content));
|
||||
|
||||
bot.emit('custom_systemchat', systemchat.message, systemchat, packet);
|
||||
bot.emit('custom_allchat', 'systemchat', systemchat.message, systemchat, packet);
|
||||
bot.emit('custom_allmessage', 'systemchat', systemchat.message, systemchat, packet);
|
||||
|
||||
});
|
||||
|
||||
|
||||
bot.on('profileless_chat', (packet) => { // kinda player_chat
|
||||
|
||||
profilelesschat = {};
|
||||
|
@ -188,6 +195,10 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat
|
|||
profilelesschat.senderName = simplify(packet.name);
|
||||
profilelesschat.targetName = simplify(packet.target);
|
||||
|
||||
profilelesschat.nocolor_formattedMessage = parseMinecraftMessageNoColor(simplify(packet.message));
|
||||
profilelesschat.nocolor_senderName = parseMinecraftMessageNoColor(simplify(packet.name));
|
||||
profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.target));
|
||||
|
||||
switch (profilelesschat.type) {
|
||||
case 1: // /me
|
||||
profilelesschat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]});
|
||||
|
@ -218,13 +229,13 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat
|
|||
profilelesschat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.team.sent', with: [ profilelesschat.targetName, profilelesschat.senderName, profilelesschat.formattedMessage ]});
|
||||
break;
|
||||
default:
|
||||
console.log(`Unknown profilelesschat packet. Type: ${playerchat.type}`);
|
||||
console.log(`Unknown profilelesschat packet. Type: ${profilelesschat.type}`);
|
||||
console.log(packet);
|
||||
break;
|
||||
}
|
||||
|
||||
bot.emit('custom_profilelesschat', profilelesschat.message, profilelesschat, packet)
|
||||
bot.emit('custom_allchat', 'profilelesschat', profilelesschat.message, profilelesschat, packet)
|
||||
bot.emit('custom_allmessage', 'profilelesschat', profilelesschat.message, profilelesschat, packet)
|
||||
})
|
||||
|
||||
|
||||
|
@ -242,6 +253,7 @@ bot.on('player_chat', (packet) => { // player
|
|||
playerchat.nocolor_unsignedContent = parseMinecraftMessageNoColor(simplify(packet.unsignedChatContent));
|
||||
playerchat.nocolor_senderName = parseMinecraftMessageNoColor(simplify(packet.networkName));
|
||||
playerchat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.networkTargetName));
|
||||
|
||||
switch (playerchat.type) { // vanish off
|
||||
case 1: // /minecraft:me
|
||||
playerchat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]});
|
||||
|
@ -277,43 +289,115 @@ bot.on('player_chat', (packet) => { // player
|
|||
break;
|
||||
}
|
||||
bot.emit('custom_playerchat', playerchat.message, playerchat, packet);
|
||||
bot.emit('custom_allchat', 'playerchat', playerchat.message, playerchat, packet)
|
||||
bot.emit('custom_allmessage', 'playerchat', playerchat.message, playerchat, packet)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
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',
|
||||
'§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m',
|
||||
'§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m',
|
||||
'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',
|
||||
'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m',
|
||||
'yellow': '\x1B[93m', 'white': '\x1B[97m'
|
||||
};
|
||||
|
||||
const ansiFormatCodes = {
|
||||
'§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m', '§k': '\x1B[5m', '§r': '\x1B[0m',
|
||||
'bold': '\x1B[1m', 'italic': '\x1B[3m', 'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m',
|
||||
|
||||
const ansiMap = {
|
||||
'§0': '\x1B[30m',
|
||||
'§1': '\x1B[34m',
|
||||
'§2': '\x1B[32m',
|
||||
'§3': '\x1B[36m',
|
||||
'§4': '\x1B[31m',
|
||||
'§5': '\x1B[35m',
|
||||
'§6': '\x1B[33m',
|
||||
'§7': '\x1B[37m',
|
||||
'§8': '\x1B[90m',
|
||||
'§9': '\x1B[94m',
|
||||
'§a': '\x1B[92m',
|
||||
'§b': '\x1B[96m',
|
||||
'§c': '\x1B[91m',
|
||||
'§d': '\x1B[95m',
|
||||
'§e': '\x1B[93m',
|
||||
'§f': '\x1B[97m',
|
||||
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',
|
||||
green: '\x1B[92m',
|
||||
aqua: '\x1B[96m',
|
||||
red: '\x1B[91m',
|
||||
light_purple: '\x1B[95m',
|
||||
yellow: '\x1B[93m',
|
||||
white: '\x1B[97m',
|
||||
|
||||
'§l': '\x1B[1m',
|
||||
'§o': '\x1B[3m',
|
||||
'§n': '\x1B[4m',
|
||||
'§m': '\x1B[9m',
|
||||
'§k': '\x1B[5m',
|
||||
'§r': '\x1B[0m',
|
||||
bold: '\x1B[1m',
|
||||
italic: '\x1B[3m',
|
||||
underlined: '\x1B[4m',
|
||||
strikethrough: '\x1B[9m',
|
||||
obfuscated: '\x1B[5m',
|
||||
reset: '\x1B[0m',
|
||||
};
|
||||
|
||||
function parseMinecraftMessage(component) {
|
||||
if (component === undefined) return;
|
||||
if (component === undefined || typeof component === "string") return component ? component : '';
|
||||
|
||||
function extractText(comp) {
|
||||
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
|
||||
let text = '';
|
||||
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
|
||||
let color, format, shouldReset = false;
|
||||
|
||||
if (parseMinecraftColor(comp?.color).have) {
|
||||
color = parseMinecraftColor(comp?.color)
|
||||
} else {
|
||||
color = prevColor
|
||||
}
|
||||
|
||||
if (parseMinecraftFormat(comp).have) {
|
||||
format = parseMinecraftFormat(comp)
|
||||
} else {
|
||||
format = prevFormat
|
||||
}
|
||||
|
||||
if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
|
||||
if (format.have || prevFormat.have) {
|
||||
if (format.have) {
|
||||
text += format.format
|
||||
} else if (prevFormat.have) {
|
||||
text += prevFormat.format
|
||||
}
|
||||
shouldReset = true;
|
||||
};
|
||||
|
||||
if (color.have || prevColor.have) {
|
||||
if (color.have) {
|
||||
text += color.color
|
||||
} else if (prevColor.have) {
|
||||
text += prevColor.color
|
||||
}
|
||||
} else {
|
||||
text += ansiMap['white']
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
text += comp.text;
|
||||
}
|
||||
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') {
|
||||
if (typeof comp[""] === 'string' || typeof comp[""] === 'number') {
|
||||
text += comp[""];
|
||||
}
|
||||
if (comp && typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
if (typeof comp === 'string' || typeof comp === 'number') {
|
||||
text += comp;
|
||||
}
|
||||
|
||||
if (typeof comp.keybind === 'string' || typeof comp.keybind === 'number') {
|
||||
text += lang[comp.keybind] || comp.keybind;
|
||||
}
|
||||
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultTranslateString = lang[comp.translate] || comp.translate;
|
||||
|
@ -322,7 +406,7 @@ function parseMinecraftMessage(component) {
|
|||
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
|
||||
|
||||
if (comp.with && !fallbackMsg) {
|
||||
const withArgs = comp.with.map(arg => extractText(arg));
|
||||
const withArgs = comp.with.map(arg => extractText(arg, color, format));
|
||||
let usedReplacements = 0;
|
||||
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
|
||||
|
||||
|
@ -332,7 +416,7 @@ function parseMinecraftMessage(component) {
|
|||
}
|
||||
|
||||
if (usedReplacements < withArgs.length) {
|
||||
if (translateString.length + formatfunction(comp, withArgs[usedReplacements]).length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[usedReplacements].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
return `thing__placeholder__${usedReplacements++}`;
|
||||
}
|
||||
|
||||
|
@ -351,14 +435,14 @@ function parseMinecraftMessage(component) {
|
|||
if (stringindex > 0 && string[stringindex - 1] === '%') {
|
||||
return match;
|
||||
}
|
||||
if (translateString.length + formatfunction(comp, withArgs[argIndex]).length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[argIndex].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
return `thing__placeholder__${argIndex}`;
|
||||
});
|
||||
|
||||
for (let i = 0; i < withArgs.length; i++) {
|
||||
if (translateString.length + formatfunction(comp, withArgs[i]).length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[i].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
|
||||
const formattedArg = formatfunction(comp, withArgs[i]);
|
||||
const formattedArg = withArgs[i];
|
||||
return formattedArg;
|
||||
});
|
||||
}
|
||||
|
@ -366,43 +450,62 @@ function parseMinecraftMessage(component) {
|
|||
}
|
||||
|
||||
if (DefaultMsg) {
|
||||
text += formatfunction(comp, DefaultTranslateString);
|
||||
text += DefaultTranslateString;
|
||||
} else if (fallbackMsg) {
|
||||
text += formatfunction(comp, comp.fallback);
|
||||
text += comp.fallback;
|
||||
} else {
|
||||
text += formatfunction(comp, translateString);
|
||||
text += translateString;
|
||||
}
|
||||
}
|
||||
|
||||
if (comp.extra) {
|
||||
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
||||
comp.extra.forEach(subComp => {
|
||||
text += formatfunction(comp, extractText(subComp));
|
||||
text += extractText(subComp, color, format);
|
||||
});
|
||||
}
|
||||
|
||||
return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
|
||||
if (shouldReset) text += ansiMap['reset'];
|
||||
|
||||
if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
|
||||
if (prevFormat.have) {
|
||||
text += prevFormat.format
|
||||
shouldReset = true;
|
||||
}
|
||||
|
||||
if (prevColor.have) {
|
||||
text += prevColor.color
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
return extractText(component) + ansiFormatCodes['reset'];
|
||||
|
||||
const themsg = extractText(component) + ansiMap['reset'];
|
||||
console.log(JSON.stringify(component, null, 2))
|
||||
console.log(JSON.stringify(themsg))
|
||||
return themsg;
|
||||
}
|
||||
|
||||
|
||||
function parseMinecraftMessageNoColor(component) {
|
||||
if (component === undefined) return;
|
||||
if (component === undefined || typeof component === "string") return component ? component : '';
|
||||
|
||||
function extractText(comp) {
|
||||
let text = '';
|
||||
|
||||
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
if (typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
text += comp.text;
|
||||
}
|
||||
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') {
|
||||
if (typeof comp[""] === 'string' || typeof comp[""] === 'number') {
|
||||
text += comp[""];
|
||||
}
|
||||
if (comp && typeof comp === 'string' || typeof comp === 'number') {
|
||||
if (typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
}
|
||||
if (typeof comp.keybind === 'string' || typeof comp.keybind === 'number') {
|
||||
text += lang[comp.keybind] || comp.keybind
|
||||
}
|
||||
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
|
@ -422,7 +525,7 @@ function parseMinecraftMessageNoColor(component) {
|
|||
}
|
||||
|
||||
if (usedReplacements < withArgs.length) {
|
||||
if (translateString.length + withArgs[usedReplacements].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[usedReplacements].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
return `thing__placeholder__${usedReplacements++}`;
|
||||
}
|
||||
|
||||
|
@ -442,12 +545,12 @@ function parseMinecraftMessageNoColor(component) {
|
|||
return match;
|
||||
}
|
||||
|
||||
if (translateString.length + withArgs[argIndex].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[argIndex].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
return `thing__placeholder__${argIndex}`;
|
||||
});
|
||||
|
||||
for (let i = 0; i < withArgs.length; i++) {
|
||||
if (translateString.length + withArgs[i].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
if (translateString.length + withArgs[i].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
|
||||
const formattedArg = withArgs[i];
|
||||
return formattedArg;
|
||||
|
@ -478,109 +581,16 @@ function parseMinecraftMessageNoColor(component) {
|
|||
return extractText(component);
|
||||
}
|
||||
|
||||
|
||||
function kickparser(component) {
|
||||
if (component === undefined) return;
|
||||
if (typeof component === "string") return component;
|
||||
|
||||
function kickparserText(comp) {
|
||||
let text = '';
|
||||
|
||||
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
text += comp.text;
|
||||
}
|
||||
|
||||
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') {
|
||||
text += comp[""];
|
||||
}
|
||||
|
||||
if (comp && typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
}
|
||||
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultTranslateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultMsg = false;
|
||||
let fallbackMsg = false;
|
||||
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
|
||||
|
||||
if (comp.with && !fallbackMsg) {
|
||||
const withArgs = comp.with.map(arg => kickparserText(arg));
|
||||
let usedReplacements = 0;
|
||||
|
||||
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
|
||||
translateString = translateString.replace(/%s/g, (match, offset, string) => {
|
||||
if (offset > 0 && string[offset - 1] === '%') {
|
||||
return 's';
|
||||
}
|
||||
|
||||
if (usedReplacements < withArgs.length) {
|
||||
if (translateString.length + withArgs[usedReplacements].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
return `thing__placeholder__${usedReplacements++}`;
|
||||
}
|
||||
|
||||
DefaultMsg = true;
|
||||
return "%s";
|
||||
});
|
||||
|
||||
translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => {
|
||||
const argIndex = parseInt(index, 10) - 1;
|
||||
|
||||
if (argIndex < 0 || argIndex >= withArgs.length) {
|
||||
DefaultMsg = true;
|
||||
return match;
|
||||
}
|
||||
|
||||
if (stringindex > 0 && string[stringindex - 1] === '%') {
|
||||
return match;
|
||||
}
|
||||
|
||||
if (translateString.length + withArgs[argIndex].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
return `thing__placeholder__${argIndex}`;
|
||||
});
|
||||
|
||||
for (let i = 0; i < withArgs.length; i++) {
|
||||
if (translateString.length + withArgs[i].length > 16384) return 'Translate Crash'; // Prevent translate crash
|
||||
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
|
||||
const formattedArg = withArgs[i];
|
||||
return formattedArg;
|
||||
});
|
||||
}
|
||||
translateString = translateString.replace(/default_thing__placeholder__/g, 'thing__placeholder__');
|
||||
}
|
||||
|
||||
if (DefaultMsg) {
|
||||
text += DefaultTranslateString;
|
||||
} else if (fallbackMsg) {
|
||||
text += comp.fallback;
|
||||
} else {
|
||||
text += translateString;
|
||||
}
|
||||
}
|
||||
|
||||
if (comp.extra) {
|
||||
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
||||
comp.extra.forEach(subComp => {
|
||||
text += kickparserText(subComp);
|
||||
});
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
return kickparserText(simplify(component));
|
||||
}
|
||||
|
||||
function cboutput(component) {
|
||||
if (component === undefined) return;
|
||||
|
||||
function extractText(comp) {
|
||||
|
||||
let text = '';
|
||||
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
if (typeof comp.text === 'string' || typeof comp.text === 'number') {
|
||||
text += comp.text;
|
||||
}
|
||||
if (comp && typeof comp === 'string' || typeof comp === 'number') {
|
||||
if (typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
}
|
||||
|
||||
|
@ -591,15 +601,15 @@ function cboutput(component) {
|
|||
});
|
||||
}
|
||||
|
||||
text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
|
||||
text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiMap['reset'];
|
||||
return text;
|
||||
|
||||
}
|
||||
|
||||
return extractText(component) + ansiFormatCodes['reset'];
|
||||
return extractText(component) + ansiMap['reset'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = { inject, kickparser, cboutput };
|
||||
module.exports = { inject, parseMinecraftMessage, parseMinecraftMessageNoColor, cboutput, simplify };
|
922
main/en_us.json
922
main/en_us.json
File diff suppressed because it is too large
Load diff
20
main/main.js
20
main/main.js
|
@ -1,4 +1,12 @@
|
|||
// 1.3 chatparser
|
||||
/**
|
||||
* Minecraft Version:
|
||||
* - 1.20.4
|
||||
* - 1.20.5
|
||||
* - 1.20.6
|
||||
*
|
||||
* ChatParser Version:
|
||||
* - 1.4T (Test) or 1.4
|
||||
*/
|
||||
|
||||
const mc = require('minecraft-protocol');
|
||||
const ChatParse = require("./chatparser.js");
|
||||
|
@ -13,6 +21,7 @@ const bot = mc.createClient({
|
|||
|
||||
ChatParse.inject(bot); // load chatparser function
|
||||
|
||||
|
||||
bot.on('custom_playerchat', (message, playerchat, packet) => {
|
||||
console.log(`[PlayerChat] ${message}`);
|
||||
});
|
||||
|
@ -20,11 +29,14 @@ bot.on('custom_playerchat', (message, playerchat, packet) => {
|
|||
bot.on('custom_profilelesschat', (message, profilelesschat, packet) => {
|
||||
console.log(`[ProfilelessChat] ${message}`);
|
||||
});
|
||||
|
||||
|
||||
bot.on('custom_systemchat', (message, systemchat, packet) => {
|
||||
if (systemchat?.jsonMsg?.translate === "advMode.setCommand.success") return; // if you have core and dont want see "Command set: %s"
|
||||
console.log(`[SystemChat] ${message}`);
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
bot.on('custom_actionbar', (message, actionbar, packet) => {
|
||||
console.log(`[ActionBar] ${message}`);
|
||||
});
|
||||
|
@ -79,10 +91,6 @@ bot.on('custom_allchat', (chatType, message, messagePacket, packet) => {
|
|||
|
||||
*/
|
||||
|
||||
bot.on('login', () => {
|
||||
console.log(`Bot Joined!`);
|
||||
});
|
||||
|
||||
bot.on('error', (err) => {
|
||||
console.error(err);
|
||||
});
|
Loading…
Reference in a new issue