fix format.

This commit is contained in:
Yaode_owo 2024-07-29 15:23:26 -04:00
parent 02a93f0b21
commit 877919ddbb

View file

@ -14,7 +14,7 @@ function parseCommand(message) {
function inject(bot) {
bot.on('systemChat', (packet) => {
//console.log(packet)
// console.log(packet)
const jsonmsg = JSON.parse(packet.formattedMessage);
const msg = parseMinecraftMessage(packet.formattedMessage);
const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage);
@ -29,7 +29,7 @@ bot.on('systemChat', (packet) => {
bot.on('playerChat', (packet) => {
let msg, vmsg;
//console.log(packet)
// console.log(packet)
// other
const uuid = packet.sender ? packet.sender : undefined;
const plainMessage = packet.plainMessage ? packet.plainMessage : undefined;
@ -97,6 +97,7 @@ function parseMinecraftMessage(component) {
jsonComponent = JSON.parse(component);
} catch (e) {
console.error("Invalid JSON format:", component);
return '';
}
function extractText(comp) {
@ -105,80 +106,78 @@ function parseMinecraftMessage(component) {
text += comp.text;
}
if (comp[""]) {
text += extractText(comp[""]);
text += comp[""]; // after 1337 years, i found issue at here
}
if (typeof comp === 'string' || typeof comp === 'number') {
text += comp;
return comp;
}
if (comp.extra) {
if (comp.extra) {
comp.extra.forEach(subComp => {
if (!comp.color) { // fix color
text += extractText(subComp);
} else {
if (comp.color && ansiColorCodes[comp.color] && !comp.color.startsWith('#')) {
text += ansiColorCodes[comp.color] + extractText(subComp) + ansiFormatCodes['reset'];
} else if (comp.color && comp.color.startsWith('#')) {
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
const hexCodes = hexRegex.exec(comp.color);
if (hexCodes) {
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`;
text += ansiColor + extractText(subComp) + ansiFormatCodes['reset'];
}
}
}
});
}
text += ansiFormatCodes['reset'] + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(subComp) + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp); // it must have better way, but i lazy.
});
}
if (comp.translate) {
let translateString = lang[comp.translate] || comp.translate;
if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg);
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg);
});
}
if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp)); // i need make formatfunction2(comp, text) ?
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp));
});
}
text += translateString;
}
if (comp.bold && comp.bold === 1) {
text = ansiFormatCodes['bold'] + text;
} // i dont know why add bold, some color dont show, maybe is my issue or code.
if (comp.color && ansiColorCodes[comp.color] && !comp.color.startsWith('#')) {
text = ansiColorCodes[comp.color] + text + ansiFormatCodes['reset'];
} else if (comp.color && comp.color.startsWith('#')) {
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
const hexCodes = hexRegex.exec(comp.color);
if (hexCodes) {
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`;
text = ansiColor + text + ansiFormatCodes['reset'];
}
}
if (comp.italic && comp.italic === 1) { // useless
text = ansiFormatCodes['italic'] + text;
}
if (comp.underlined && comp.underlined === 1) {
text = ansiFormatCodes['underlined'] + text;
}
if (comp.strikethrough && comp.strikethrough === 1) { // useless
text = ansiFormatCodes['strikethrough'] + text;
}
if (comp.obfuscated && comp.obfuscated === 1) { // useless
text = ansiFormatCodes['obfuscated'] + text;
}
text = formatfunction(comp, text);
return text;
}
return extractText(jsonComponent) + ansiFormatCodes['reset'];
jsonComponent = extractText(jsonComponent);
return jsonComponent + ansiFormatCodes['reset'];
}
function formatfunction(comp, text) {
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
}
function parseMinecraftColor(color) {
if (color && ansiColorCodes[color] && !color.startsWith('#')) {
return ansiColorCodes[color];
} else if (color && color.startsWith('#')) {
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
const hexCodes = hexRegex.exec(color);
if (hexCodes) {
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;
}
} else {
return '';
}
}
function parseMinecraftFormat(format) {
let result = '';
console.log(format)
if (format.bold) result += ansiFormatCodes['bold'];
if (format.italic) result += ansiFormatCodes['italic'];
if (format.underlined) result += ansiFormatCodes['underlined'];
if (format.strikethrough) result += ansiFormatCodes['strikethrough'];
if (format.obfuscated) result += ansiFormatCodes['obfuscated'];
return result;
}
function parseMinecraftMessageNoColor(component) {
let jsonComponent;
try {