Info in chatparser.js

This commit is contained in:
Yaode_owo 2024-12-03 09:15:23 -05:00
parent 021fbf5006
commit 82576a1bea
2 changed files with 131 additions and 86 deletions

View file

@ -1,50 +1,53 @@
/** /**
* Minecraft Version: * Minecraft-protocol version: 1.50.0
* - 1.20.4
* - 1.20.5
* - 1.20.6
* *
* Language File Version: * Minecraft Version: 1.20.4 (Basically should support 1.20.5 and 1.20.6)
* - 1.21.1
* *
* Version: * Language File Version: 1.21.1
* - 1.41 *
* Version: 1.44
* *
* Update: * Update:
* - fix some issue * - Add 0 type player chat and profileless chat, uh useless
* - Better ANSI * - Fix Color and format
* - return component if not object
* *
* Knowns Issues: * Knowns Issues:
* - Invalid Emojis * - Invalid Emojis
* - Spawnpoint Number * - Spawnpoint Number
*
* Report Issue: https://code.chipmunk.land/Yaode_owo/Minecraft-protocol-1.20.4-chat-parser/issues/new
*/ */
const lang = require("./en_us.json"); // translate message const lang = require("./en_us.json"); // translate message
function simplify (data) { function simplify(data) {
try { // Prevent RangeError try {
if (data === undefined || data === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m'; if (data === undefined || data === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
if (typeof data !== "object") return data; if (typeof data !== "object") return data;
function transform (value, type) { function transform(value, type) {
if (type === 'compound') { if (type === 'compound') {
return Object.keys(value).reduce(function (acc, key) { return Object.keys(value).reduce(function (acc, key) {
acc[key] = simplify(value[key]) acc[key] = simplify(value[key]);
return acc return acc;
}, {}) }, {});
} }
if (type === 'list') { if (type === 'list') {
return value.value.map(function (v) { return transform(v, value.type) }) return value.value.map(function (v) {
return transform(v, value.type);
});
} }
return value return value;
} }
return transform(data.value, data.type) return transform(data.value, data.type);
} catch (e) { } catch (e) {
return '\x1B[91m*** Component is too complex ***\x1B[0m'; console.log(e);
return '\x1B[91m*** An unexpected error occurred while processing the component ***\x1B[0m';
// return '\x1B[90m*** When is component is sus ***\x1B[0m';
} }
} }
function parseMinecraftColor(color) { function parseMinecraftColor(color) {
if (typeof color === 'string' && ansiMap[color] && !color.startsWith('#')) { if (typeof color === 'string' && ansiMap[color] && !color.startsWith('#')) {
return { color: ansiMap[color], have: true }; return { color: ansiMap[color], have: true };
@ -64,13 +67,13 @@ function parseMinecraftColor(color) {
function parseMinecraftFormat(format) { function parseMinecraftFormat(format) {
let result = ''; let result = '';
if (!format) return { format: result, have: false }; if (!format) return { format: '', have: false };
if (format.bold === 1) result += ansiMap['bold']; if (format.bold === 1) result += ansiMap['bold'];
if (format.italic === 1) result += ansiMap['italic']; if (format.italic === 1) result += ansiMap['italic'];
if (format.underlined === 1) result += ansiMap['underlined']; if (format.underlined === 1) result += ansiMap['underlined'];
if (format.strikethrough === 1) result += ansiMap['strikethrough']; if (format.strikethrough === 1) result += ansiMap['strikethrough'];
if (format.obfuscated === 1) result += ansiMap['obfuscated']; if (format.obfuscated === 1) result += ansiMap['obfuscated'];
return result !== '' ? { format: result, have: true } : { format: result, have: false }; return result !== '' ? { format: result, have: true } : { format: '', have: false };
} }
function inject(bot) { function inject(bot) {
@ -78,19 +81,24 @@ function inject(bot) {
let playerchat = {}; let playerchat = {};
let systemchat = {}; let systemchat = {};
let profilelesschat = {}; let profilelesschat = {};
/*
let actionbar = {}; let actionbar = {};
let bossbar = {}; let bossbar = {};
let title = {}; let title = {};
*/
bot.on('end', () => { bot.on('end', () => {
playerChat = {}; playerChat = {};
systemChat = {}; systemChat = {};
profilelessChat = {}; profilelessChat = {};
/*
actionbar = {}; actionbar = {};
bossbar = {}; bossbar = {};
title = {}; title = {};
*/
}); });
/*
bot.on('set_title_text', (packet) => { bot.on('set_title_text', (packet) => {
title.message = parseMinecraftMessage(simplify(packet.text)); title.message = parseMinecraftMessage(simplify(packet.text));
title.noColor_message = parseMinecraftMessageNoColor(simplify(packet.text)); title.noColor_message = parseMinecraftMessageNoColor(simplify(packet.text));
@ -173,7 +181,7 @@ bot.on('action_bar', (packet) => {
bot.emit('custom_actionbar', actionbar.message, actionbar, packet); bot.emit('custom_actionbar', actionbar.message, actionbar, packet);
bot.emit('custom_allmessage', 'actionbar', actionbar.message, actionbar, packet); bot.emit('custom_allmessage', 'actionbar', actionbar.message, actionbar, packet);
}); });
*/
bot.on('system_chat', (packet) => { // system bot.on('system_chat', (packet) => { // system
if (packet.isActionBar) return; // useless i guess if (packet.isActionBar) return; // useless i guess
@ -201,6 +209,10 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat
profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.target)); profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.target));
switch (profilelesschat.type) { switch (profilelesschat.type) {
case 0: // normal profileless chat message
profilelesschat.message = parseMinecraftMessage({ "translate": "chat.type.text", "with": [ profilelesschat.senderName, playerchat.formattedMessage ]});
profilelesschat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.text", "with": [ profilelesschat.senderName, profilelesschat.plainMessage ]});
break;
case 1: // /me case 1: // /me
profilelesschat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]});
profilelesschat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]});
@ -256,33 +268,45 @@ bot.on('player_chat', (packet) => { // player
playerchat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.networkTargetName)); playerchat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.networkTargetName));
switch (playerchat.type) { // vanish off switch (playerchat.type) { // vanish off
case 0: // normal vanilla chat message
playerchat.message = parseMinecraftMessage({ "translate": "chat.type.text", "with": [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.text", "with": [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.jsonMsg = { "translate": "chat.type.text", "with": [ playerchat.senderName, playerchat.plainMessage ]};
break;
case 1: // /minecraft:me case 1: // /minecraft:me
playerchat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]}); playerchat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]}); playerchat.nocolor_message = parseMinecraftMessageNoColor({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.jsonMsg = { "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]}
break; break;
case 2: // player /minecraft:tell case 2: // player /minecraft:tell
playerchat.message = parseMinecraftMessage({ translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true }); playerchat.message = parseMinecraftMessage({ translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true });
playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true }); playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true });
playerchat.jsonMsg = { translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true }
break; break;
case 3: // you /minecraft:tell case 3: // you /minecraft:tell
playerchat.message = parseMinecraftMessage({ translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true }); playerchat.message = parseMinecraftMessage({ translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true });
playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true }); playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true });
playerchat.jsonMsg = { translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true }
break; break;
case 4: // player chat case 4: // player chat
playerchat.message = parseMinecraftMessage(playerchat.unsignedContent); playerchat.message = parseMinecraftMessage(playerchat.unsignedContent);
playerchat.nocolor_message = parseMinecraftMessageNoColor(playerchat.unsignedContent); playerchat.nocolor_message = parseMinecraftMessageNoColor(playerchat.unsignedContent);
playerchat.jsonMsg = playerchat.unsignedContent
break; break;
case 5: // /say case 5: // /say
playerchat.message = parseMinecraftMessage({ translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]}); playerchat.message = parseMinecraftMessage({ translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]}); playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]});
playerchat.jsonMsg = { translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]}
break; break;
case 6: // player /minecraft:teammsg || /teammsg case 6: // player /minecraft:teammsg || /teammsg
playerchat.message = parseMinecraftMessage({ translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}); playerchat.message = parseMinecraftMessage({ translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]});
playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}); playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]});
playerchat.jsonMsg = { translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}
break; break;
case 7: // you /minecraft:teammsg || /teammsg case 7: // you /minecraft:teammsg || /teammsg
playerchat.message = parseMinecraftMessage({ translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}); playerchat.message = parseMinecraftMessage({ translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]});
playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}); playerchat.nocolor_message = parseMinecraftMessageNoColor({ translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]});
playerchat.jsonMsg = { translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}
break; break;
default: default:
console.log(`Unknown player_chat packet. Type: ${playerchat.type}`); console.log(`Unknown player_chat packet. Type: ${playerchat.type}`);
@ -295,8 +319,6 @@ bot.on('player_chat', (packet) => { // player
} }
const ansiMap = { const ansiMap = {
'§0': '\x1B[30m', '§0': '\x1B[30m',
'§1': '\x1B[34m', '§1': '\x1B[34m',
@ -348,34 +370,18 @@ const ansiMap = {
function parseMinecraftMessage(component) { function parseMinecraftMessage(component) {
if (component === undefined || component === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m'; if (component === undefined || component === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
if (typeof component !== "object") return component; if (typeof component !== "object") return component;
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) { function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
let text = ''; let text = '';
let color, format, shouldReset = false; let shouldReset = false;
let NowColor = parseMinecraftColor(comp?.color);
let NowFormat = parseMinecraftFormat(comp);
color = parseMinecraftColor(comp?.color); if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
format = parseMinecraftFormat(comp); text += NowColor.have ? NowColor.color : prevColor.have ? prevColor.color : ansiMap['white'];
if (NowFormat.have || prevFormat.have) {
if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) { text += NowFormat.have ? NowFormat.format : prevFormat.format;
if (format.have || prevFormat.have) { shouldReset = true;
if (format.have) {
text += format.format
shouldReset = true;
} else if (prevFormat.have) {
text += prevFormat.format
}
};
if (color.have || prevColor.have) {
if (color.have) {
text += color.color
shouldReset = true;
} else if (prevColor.have) {
text += prevColor.color
}
} else {
text += ansiMap['white'];
}; };
} }
@ -400,7 +406,7 @@ function parseMinecraftMessage(component) {
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true; if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
if (comp.with && !fallbackMsg) { if (comp.with && !fallbackMsg) {
const withArgs = comp.with.map(arg => extractText(arg, color.have === true ? color : prevColor, format.have === true ? format : prevFormat)); const withArgs = comp.with.map(arg => extractText(arg, NowColor.have ? NowColor : prevColor, NowFormat.have ? NowFormat : prevFormat));
let usedReplacements = 0; let usedReplacements = 0;
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__'); translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
@ -455,29 +461,24 @@ function parseMinecraftMessage(component) {
if (comp.extra) { if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
text += extractText(subComp, color.have === true ? color : prevColor, format.have === true ? format : prevFormat); text += extractText(subComp, NowColor.have ? NowColor : prevColor, NowFormat.have ? NowFormat : prevFormat);
}); });
} }
if (shouldReset) text += ansiMap['reset']; if (shouldReset) {
text += ansiMap['reset']
if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) { } else {
if (prevFormat.have && format.have) { text += prevColor.color || ansiMap['white'];
text += prevFormat.format text += prevFormat.format || '';
}
if (prevColor.have && color.have) {
text += prevColor.color
}
} }
return text; return text;
} }
return extractText(component); return extractText(component);
// return extractText(component) + ansiMap['reset']; ye if you need
} }
function parseMinecraftMessageNoColor(component) { function parseMinecraftMessageNoColor(component) {
if (component === undefined || component === null) return '*** Component is undefined or missing ***'; if (component === undefined || component === null) return '*** Component is undefined or missing ***';
if (typeof component !== "object") return component; if (typeof component !== "object") return component;
@ -573,34 +574,75 @@ function parseMinecraftMessageNoColor(component) {
} }
function cboutput(component) { function cboutput(component) {
if (component === undefined || component === null) return; if (component === undefined || component === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
if (typeof component !== "object") return component;
function extractText(comp) {
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
let text = ''; let text = '';
let shouldReset = false;
let NowColor = parseMinecraftColor(comp?.color);
let NowFormat = parseMinecraftFormat(comp);
if ((comp || comp !== "") && (comp.text || comp.text !== "")) {
text += NowColor.have ? NowColor.color : prevColor.have ? prevColor.color : ansiMap['white'];
if (NowFormat.have || prevFormat.have) {
text += NowFormat.have ? NowFormat.format : prevFormat.format;
shouldReset = true;
};
}
if (typeof comp.text === 'string' || typeof comp.text === 'number') { if (typeof comp.text === 'string' || typeof comp.text === 'number') {
text += comp.text; text += comp.text;
} }
if (typeof comp === 'string' || typeof comp === 'number') { if (typeof comp === 'string' || typeof comp === 'number') {
return comp; text += comp;
} }
if (comp.extra) { if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
text += formatfunction(comp, extractText(subComp)); text += extractText(subComp, NowColor.have === true ? NowColor : prevColor, NowFormat.have === true ? NowFormat : prevFormat);
}); });
} }
text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiMap['reset']; if (shouldReset) {
return text; text += ansiMap['reset']
} else {
text += prevColor.color || ansiMap['white'];
text += prevFormat.format || '';
}
return text;
} }
return extractText(component) + ansiMap['reset']; return extractText(component);
} }
function cboutputNoColor(component) {
if (component === undefined || component === null) return '*** Component is undefined or missing ***';
if (typeof component !== "object") return component;
function extractText(comp) {
let text = '';
if (typeof comp.text === 'string' || typeof comp.text === 'number') {
text += comp.text;
}
if (typeof comp === 'string' || typeof comp === 'number') {
text += comp;
}
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
text += extractText(subComp);
});
}
return text;
}
return extractText(component);
}
module.exports = { inject, parseMinecraftMessage, parseMinecraftMessageNoColor, cboutput, simplify }; module.exports = { inject, parseMinecraftMessage, parseMinecraftMessageNoColor, cboutput, simplify };

View file

@ -5,9 +5,8 @@
* - 1.20.6 * - 1.20.6
* *
* ChatParser Version: * ChatParser Version:
* - 1.41, 1.4T (Test Version) * - 1.44
* *
* - Adding Kick Parser
*/ */
const mc = require('minecraft-protocol'); const mc = require('minecraft-protocol');
@ -24,22 +23,26 @@ const bot = mc.createClient({
ChatParse.inject(bot); // load catparser function ChatParse.inject(bot); // load catparser function
bot.on('custom_playerchat', (message, playerchat, packet) => { bot.on('custom_playerchat', (message, playerchat, packet) => {
console.log(`[PlayerChat] ${message}`); console.log(`[PlayerChat] ${message}`);
}); });
bot.on('custom_profilelesschat', (message, profilelesschat, packet) => { bot.on('custom_profilelesschat', (message, profilelesschat, packet) => {
console.log(`[ProfilelessChat] ${message}`); console.log(`[ProfilelessChat] ${message}`);
}); });
bot.on('custom_systemchat', (message, systemchat, packet) => { bot.on('custom_systemchat', (message, systemchat, packet) => {
console.log(`[SystemChat] ${message}`); console.log(`[SystemChat] ${message}`);
}); });
/* bot.on('kick_disconnect', (kick) => {
console.log(ChatParse.parseMinecraftMessage(kicked.reason));
})
bot.on('disconnect', (kick) => {
console.log(ChatParse.parseMinecraftMessage(kicked.reason));
})
/*
bot.on('custom_actionbar', (message, actionbar, packet) => { bot.on('custom_actionbar', (message, actionbar, packet) => {
console.log(`[ActionBar] ${message}`); console.log(`[ActionBar] ${message}`);
}); });