Info in chatparser.js
This commit is contained in:
parent
021fbf5006
commit
82576a1bea
2 changed files with 131 additions and 86 deletions
|
@ -1,50 +1,53 @@
|
|||
/**
|
||||
* Minecraft Version:
|
||||
* - 1.20.4
|
||||
* - 1.20.5
|
||||
* - 1.20.6
|
||||
* Minecraft-protocol version: 1.50.0
|
||||
*
|
||||
* Language File Version:
|
||||
* - 1.21.1
|
||||
* Minecraft Version: 1.20.4 (Basically should support 1.20.5 and 1.20.6)
|
||||
*
|
||||
* Version:
|
||||
* - 1.41
|
||||
* Language File Version: 1.21.1
|
||||
*
|
||||
* Version: 1.44
|
||||
*
|
||||
* Update:
|
||||
* - fix some issue
|
||||
* - Better ANSI
|
||||
* - return component if not object
|
||||
* - Add 0 type player chat and profileless chat, uh useless
|
||||
* - Fix Color and format
|
||||
*
|
||||
* Knowns Issues:
|
||||
* - Invalid Emojis
|
||||
* - 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
|
||||
|
||||
function simplify (data) {
|
||||
try { // Prevent RangeError
|
||||
function simplify(data) {
|
||||
try {
|
||||
if (data === undefined || data === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
|
||||
if (typeof data !== "object") return data;
|
||||
function transform (value, type) {
|
||||
function transform(value, type) {
|
||||
if (type === 'compound') {
|
||||
return Object.keys(value).reduce(function (acc, key) {
|
||||
acc[key] = simplify(value[key])
|
||||
return acc
|
||||
}, {})
|
||||
acc[key] = simplify(value[key]);
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
if (typeof color === 'string' && ansiMap[color] && !color.startsWith('#')) {
|
||||
return { color: ansiMap[color], have: true };
|
||||
|
@ -64,13 +67,13 @@ function parseMinecraftColor(color) {
|
|||
|
||||
function parseMinecraftFormat(format) {
|
||||
let result = '';
|
||||
if (!format) return { format: result, have: false };
|
||||
if (!format) return { format: '', 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: result, have: false };
|
||||
return result !== '' ? { format: result, have: true } : { format: '', have: false };
|
||||
}
|
||||
|
||||
function inject(bot) {
|
||||
|
@ -78,19 +81,24 @@ function inject(bot) {
|
|||
let playerchat = {};
|
||||
let systemchat = {};
|
||||
let profilelesschat = {};
|
||||
/*
|
||||
let actionbar = {};
|
||||
let bossbar = {};
|
||||
let 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));
|
||||
|
@ -173,7 +181,7 @@ bot.on('action_bar', (packet) => {
|
|||
bot.emit('custom_actionbar', actionbar.message, actionbar, packet);
|
||||
bot.emit('custom_allmessage', 'actionbar', actionbar.message, actionbar, packet);
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
bot.on('system_chat', (packet) => { // system
|
||||
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));
|
||||
|
||||
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
|
||||
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 ]});
|
||||
|
@ -256,33 +268,45 @@ bot.on('player_chat', (packet) => { // player
|
|||
playerchat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.networkTargetName));
|
||||
|
||||
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
|
||||
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.jsonMsg = { "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]}
|
||||
break;
|
||||
case 2: // player /minecraft:tell
|
||||
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.jsonMsg = { translate: "commands.message.display.incoming", with: [ playerchat.senderName, playerchat.plainMessage ], color: "gray", italic: true }
|
||||
break;
|
||||
case 3: // you /minecraft:tell
|
||||
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.jsonMsg = { translate: "commands.message.display.outgoing", with: [ playerchat.targetName, playerchat.plainMessage ], color: "gray", italic: true }
|
||||
break;
|
||||
case 4: // player chat
|
||||
playerchat.message = parseMinecraftMessage(playerchat.unsignedContent);
|
||||
playerchat.nocolor_message = parseMinecraftMessageNoColor(playerchat.unsignedContent);
|
||||
playerchat.jsonMsg = playerchat.unsignedContent
|
||||
break;
|
||||
case 5: // /say
|
||||
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.jsonMsg = { translate: 'chat.type.announcement', with: [ playerchat.senderName, playerchat.plainMessage ]}
|
||||
break;
|
||||
case 6: // player /minecraft:teammsg || /teammsg
|
||||
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.jsonMsg = { translate: 'chat.type.team.text', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}
|
||||
break;
|
||||
case 7: // you /minecraft:teammsg || /teammsg
|
||||
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.jsonMsg = { translate: 'chat.type.team.sent', with: [ playerchat.targetName, playerchat.senderName, playerchat.plainMessage ]}
|
||||
break;
|
||||
default:
|
||||
console.log(`Unknown player_chat packet. Type: ${playerchat.type}`);
|
||||
|
@ -295,8 +319,6 @@ bot.on('player_chat', (packet) => { // player
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const ansiMap = {
|
||||
'§0': '\x1B[30m',
|
||||
'§1': '\x1B[34m',
|
||||
|
@ -348,34 +370,18 @@ const ansiMap = {
|
|||
function parseMinecraftMessage(component) {
|
||||
if (component === undefined || component === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
|
||||
if (typeof component !== "object") return component;
|
||||
|
||||
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
|
||||
let text = '';
|
||||
|
||||
let color, format, shouldReset = false;
|
||||
|
||||
color = parseMinecraftColor(comp?.color);
|
||||
format = parseMinecraftFormat(comp);
|
||||
let shouldReset = false;
|
||||
let NowColor = parseMinecraftColor(comp?.color);
|
||||
let NowFormat = parseMinecraftFormat(comp);
|
||||
|
||||
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
|
||||
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;
|
||||
} 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.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;
|
||||
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
|
||||
|
||||
|
@ -455,29 +461,24 @@ function parseMinecraftMessage(component) {
|
|||
if (comp.extra) {
|
||||
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
||||
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 ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
|
||||
if (prevFormat.have && format.have) {
|
||||
text += prevFormat.format
|
||||
}
|
||||
|
||||
if (prevColor.have && color.have) {
|
||||
text += prevColor.color
|
||||
}
|
||||
if (shouldReset) {
|
||||
text += ansiMap['reset']
|
||||
} else {
|
||||
text += prevColor.color || ansiMap['white'];
|
||||
text += prevFormat.format || '';
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
return extractText(component);
|
||||
// return extractText(component) + ansiMap['reset']; ye if you need
|
||||
}
|
||||
|
||||
|
||||
function parseMinecraftMessageNoColor(component) {
|
||||
if (component === undefined || component === null) return '*** Component is undefined or missing ***';
|
||||
if (typeof component !== "object") return component;
|
||||
|
@ -573,34 +574,75 @@ function parseMinecraftMessageNoColor(component) {
|
|||
}
|
||||
|
||||
function cboutput(component) {
|
||||
if (component === undefined || component === null) return;
|
||||
|
||||
function extractText(comp) {
|
||||
if (component === undefined || component === null) return '\x1B[91m*** Component is undefined or missing ***\x1B[0m';
|
||||
if (typeof component !== "object") return component;
|
||||
|
||||
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
|
||||
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') {
|
||||
text += comp.text;
|
||||
}
|
||||
if (typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
text += comp;
|
||||
}
|
||||
|
||||
if (comp.extra) {
|
||||
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
||||
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'];
|
||||
return text;
|
||||
|
||||
if (shouldReset) {
|
||||
text += ansiMap['reset']
|
||||
} else {
|
||||
text += prevColor.color || ansiMap['white'];
|
||||
text += prevFormat.format || '';
|
||||
}
|
||||
|
||||
return extractText(component) + ansiMap['reset'];
|
||||
return text;
|
||||
}
|
||||
|
||||
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 };
|
13
main/main.js
13
main/main.js
|
@ -5,9 +5,8 @@
|
|||
* - 1.20.6
|
||||
*
|
||||
* ChatParser Version:
|
||||
* - 1.41, 1.4T (Test Version)
|
||||
* - 1.44
|
||||
*
|
||||
* - Adding Kick Parser
|
||||
*/
|
||||
|
||||
const mc = require('minecraft-protocol');
|
||||
|
@ -24,7 +23,6 @@ const bot = mc.createClient({
|
|||
|
||||
ChatParse.inject(bot); // load catparser function
|
||||
|
||||
|
||||
bot.on('custom_playerchat', (message, playerchat, packet) => {
|
||||
console.log(`[PlayerChat] ${message}`);
|
||||
});
|
||||
|
@ -33,13 +31,18 @@ bot.on('custom_profilelesschat', (message, profilelesschat, packet) => {
|
|||
console.log(`[ProfilelessChat] ${message}`);
|
||||
});
|
||||
|
||||
|
||||
bot.on('custom_systemchat', (message, systemchat, packet) => {
|
||||
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) => {
|
||||
console.log(`[ActionBar] ${message}`);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue