This commit is contained in:
Yaode_owo 2024-11-19 09:42:06 -05:00
parent b0a422897b
commit 28b178cd8a
2 changed files with 38 additions and 43 deletions

View file

@ -4,20 +4,20 @@
* - 1.20.5 * - 1.20.5
* - 1.20.6 * - 1.20.6
* *
* Language Version: * Language File Version:
* - 1.21.1 * - 1.21.1
* *
* Version: * Version:
* - 1.4T (Test Version) * - 1.41
* *
* Update: * Update:
* - Keybind added * - fix some issue
* - Profileless chat with no color messages * - Better ANSI
* - Rewritten color and format handling * - return component if not object
* - Edit error output
* *
* Maybe Issues: * Knowns Issues:
* - Incorrect parsing colors or formats * - Invalid Emojis
* - Spawnpoint Number
*/ */
@ -25,7 +25,8 @@ const lang = require("./en_us.json"); // translate message
function simplify (data) { function simplify (data) {
try { // Prevent RangeError try { // Prevent RangeError
if (data === undefined) return data; 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') { if (type === 'compound') {
return Object.keys(value).reduce(function (acc, key) { return Object.keys(value).reduce(function (acc, key) {
@ -69,7 +70,7 @@ function parseMinecraftFormat(format) {
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: '', have: false }; return result !== '' ? { format: result, have: true } : { format: result, have: false };
} }
function inject(bot) { function inject(bot) {
@ -345,42 +346,34 @@ const ansiMap = {
}; };
function parseMinecraftMessage(component) { function parseMinecraftMessage(component) {
if (component === undefined || typeof component === "string") return component ? 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 }) { function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
let text = ''; let text = '';
let color, format, shouldReset = false; let color, format, shouldReset = false;
if (parseMinecraftColor(comp?.color).have) { color = parseMinecraftColor(comp?.color);
color = parseMinecraftColor(comp?.color) format = parseMinecraftFormat(comp);
} 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 ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
if (format.have || prevFormat.have) { if (format.have || prevFormat.have) {
if (format.have) { if (format.have) {
text += format.format text += format.format
shouldReset = true;
} else if (prevFormat.have) { } else if (prevFormat.have) {
text += prevFormat.format text += prevFormat.format
} }
shouldReset = true;
}; };
if (color.have || prevColor.have) { if (color.have || prevColor.have) {
if (color.have) { if (color.have) {
text += color.color text += color.color
shouldReset = true;
} else if (prevColor.have) { } else if (prevColor.have) {
text += prevColor.color text += prevColor.color
} }
shouldReset = true;
} else { } else {
text += ansiMap['white']; text += ansiMap['white'];
}; };
@ -407,7 +400,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, format)); const withArgs = comp.with.map(arg => extractText(arg, color.have === true ? color : prevColor, format.have === true ? format : 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__');
@ -462,19 +455,18 @@ 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, format); text += extractText(subComp, color.have === true ? color : prevColor, format.have === true ? format : 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 !== "")) { if ((comp || comp !== "") && (comp.text || comp.text !== "") && (comp[""] || comp[""] !== "") && (comp.keybind || comp.keybind !== "") && (comp.translate || comp.translate !== "")) {
if (prevFormat.have) { if (prevFormat.have && format.have) {
text += prevFormat.format text += prevFormat.format
shouldReset = true;
} }
if (prevColor.have) { if (prevColor.have && color.have) {
text += prevColor.color text += prevColor.color
} }
} }
@ -482,12 +474,13 @@ function parseMinecraftMessage(component) {
return text; return text;
} }
return extractText(component) + ansiMap['reset']; return extractText(component);
} }
function parseMinecraftMessageNoColor(component) { function parseMinecraftMessageNoColor(component) {
if (component === undefined || typeof component === "string") return component ? component : ''; if (component === undefined || component === null) return '*** Component is undefined or missing ***';
if (typeof component !== "object") return component;
function extractText(comp) { function extractText(comp) {
let text = ''; let text = '';
@ -523,7 +516,7 @@ function parseMinecraftMessageNoColor(component) {
} }
if (usedReplacements < withArgs.length) { if (usedReplacements < withArgs.length) {
if (translateString.length + withArgs[usedReplacements].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash if (translateString.length + withArgs[usedReplacements].length > 32768) return '*** Component has too many placeholders ***'; // Prevent translate crash
return `thing__placeholder__${usedReplacements++}`; return `thing__placeholder__${usedReplacements++}`;
} }
@ -543,12 +536,12 @@ function parseMinecraftMessageNoColor(component) {
return match; return match;
} }
if (translateString.length + withArgs[argIndex].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash if (translateString.length + withArgs[argIndex].length > 32768) return '*** Component has too many placeholders ***'; // Prevent translate crash
return `thing__placeholder__${argIndex}`; return `thing__placeholder__${argIndex}`;
}); });
for (let i = 0; i < withArgs.length; i++) { for (let i = 0; i < withArgs.length; i++) {
if (translateString.length + withArgs[i].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash if (translateString.length + withArgs[i].length > 32768) return '*** Component has too many placeholders ***'; // Prevent translate crash
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => { translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
const formattedArg = withArgs[i]; const formattedArg = withArgs[i];
return formattedArg; return formattedArg;
@ -580,7 +573,7 @@ function parseMinecraftMessageNoColor(component) {
} }
function cboutput(component) { function cboutput(component) {
if (component === undefined) return; if (component === undefined || component === null) return;
function extractText(comp) { function extractText(comp) {

View file

@ -5,7 +5,9 @@
* - 1.20.6 * - 1.20.6
* *
* ChatParser Version: * ChatParser Version:
* - 1.4T (Test) or 1.4 * - 1.41, 1.4T (Test Version)
*
* - Adding Kick Parser
*/ */
const mc = require('minecraft-protocol'); const mc = require('minecraft-protocol');
@ -14,12 +16,13 @@ const bot = mc.createClient({
// host: '95.216.192.50', // kaboom // host: '95.216.192.50', // kaboom
// host: 'chipmunk.land', // host: 'chipmunk.land',
// host: '168.100.225.224', // Neko // host: '168.100.225.224', // Neko
host: 'chayapak.chipmunk.land',
port: 25565, port: 25565,
username: 'catparser', // cat username: 'catparser', // cat
version: "1.20.4", // Not support 1.21 version: "1.20.4", // Not support 1.21
}); });
ChatParse.inject(bot); // load chatparser function ChatParse.inject(bot); // load catparser function
bot.on('custom_playerchat', (message, playerchat, packet) => { bot.on('custom_playerchat', (message, playerchat, packet) => {
@ -82,7 +85,6 @@ bot.on('custom_timetitle', (_, title, packet) => {
console.log(`[TitleTimes] fadeIn: ${title.fadeIn}\nstay: ${title.stay}\nfadeOut: ${title.fadeOut}`); console.log(`[TitleTimes] fadeIn: ${title.fadeIn}\nstay: ${title.stay}\nfadeOut: ${title.fadeOut}`);
}); });
/*
bot.on('custom_allchat', (chatType, message, messagePacket, packet) => { bot.on('custom_allchat', (chatType, message, messagePacket, packet) => {
if (chatType === "actionbar" || chatType === "bossbar" || chatType === "title" || chatType === "subtitle" || chatType === "timetitle") return; if (chatType === "actionbar" || chatType === "bossbar" || chatType === "title" || chatType === "subtitle" || chatType === "timetitle") return;
if (chatType === "systemchat" && messagePacket?.jsonMsg?.translate === "advMode.setCommand.success") return; // if you have core and dont want see "Command set: %s" if (chatType === "systemchat" && messagePacket?.jsonMsg?.translate === "advMode.setCommand.success") return; // if you have core and dont want see "Command set: %s"