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.6
*
* Language Version:
* Language File Version:
* - 1.21.1
*
* Version:
* - 1.4T (Test Version)
* - 1.41
*
* Update:
* - fix some issue
* - Better ANSI
* - return component if not object
*
* Update:
* - Keybind added
* - Profileless chat with no color messages
* - Rewritten color and format handling
* - Edit error output
*
* Maybe Issues:
* - Incorrect parsing colors or formats
* Knowns Issues:
* - Invalid Emojis
* - Spawnpoint Number
*/
@ -25,7 +25,8 @@ const lang = require("./en_us.json"); // translate message
function simplify (data) {
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) {
if (type === 'compound') {
return Object.keys(value).reduce(function (acc, key) {
@ -69,7 +70,7 @@ function parseMinecraftFormat(format) {
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 };
return result !== '' ? { format: result, have: true } : { format: result, have: false };
}
function inject(bot) {
@ -345,42 +346,34 @@ const ansiMap = {
};
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 }) {
let text = '';
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
}
color = parseMinecraftColor(comp?.color);
format = 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
shouldReset = true;
} else if (prevFormat.have) {
text += prevFormat.format
}
shouldReset = true;
};
if (color.have || prevColor.have) {
if (color.have) {
text += color.color
shouldReset = true;
} else if (prevColor.have) {
text += prevColor.color
}
shouldReset = true;
} else {
text += ansiMap['white'];
};
@ -407,7 +400,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, format));
const withArgs = comp.with.map(arg => extractText(arg, color.have === true ? color : prevColor, format.have === true ? format : prevFormat));
let usedReplacements = 0;
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
@ -462,33 +455,33 @@ function parseMinecraftMessage(component) {
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
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 ((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
shouldReset = true;
}
if (prevColor.have) {
if (prevColor.have && color.have) {
text += prevColor.color
}
}
return text;
}
return extractText(component) + ansiMap['reset'];
return extractText(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) {
let text = '';
@ -523,7 +516,7 @@ function parseMinecraftMessageNoColor(component) {
}
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++}`;
}
@ -543,12 +536,12 @@ function parseMinecraftMessageNoColor(component) {
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}`;
});
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) => {
const formattedArg = withArgs[i];
return formattedArg;
@ -580,7 +573,7 @@ function parseMinecraftMessageNoColor(component) {
}
function cboutput(component) {
if (component === undefined) return;
if (component === undefined || component === null) return;
function extractText(comp) {

View file

@ -5,7 +5,9 @@
* - 1.20.6
*
* ChatParser Version:
* - 1.4T (Test) or 1.4
* - 1.41, 1.4T (Test Version)
*
* - Adding Kick Parser
*/
const mc = require('minecraft-protocol');
@ -14,12 +16,13 @@ const bot = mc.createClient({
// host: '95.216.192.50', // kaboom
// host: 'chipmunk.land',
// host: '168.100.225.224', // Neko
host: 'chayapak.chipmunk.land',
port: 25565,
username: 'catparser', // cat
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) => {
@ -82,7 +85,6 @@ bot.on('custom_timetitle', (_, title, packet) => {
console.log(`[TitleTimes] fadeIn: ${title.fadeIn}\nstay: ${title.stay}\nfadeOut: ${title.fadeOut}`);
});
/*
bot.on('custom_allchat', (chatType, message, messagePacket, packet) => {
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"