fix format.

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

View file

@ -97,6 +97,7 @@ function parseMinecraftMessage(component) {
jsonComponent = JSON.parse(component); jsonComponent = JSON.parse(component);
} catch (e) { } catch (e) {
console.error("Invalid JSON format:", component); console.error("Invalid JSON format:", component);
return '';
} }
function extractText(comp) { function extractText(comp) {
@ -105,80 +106,78 @@ function parseMinecraftMessage(component) {
text += comp.text; text += comp.text;
} }
if (comp[""]) { if (comp[""]) {
text += extractText(comp[""]); text += comp[""]; // after 1337 years, i found issue at here
} }
if (typeof comp === 'string' || typeof comp === 'number') { if (typeof comp === 'string' || typeof comp === 'number') {
text += comp; return comp;
} }
if (comp.extra) { if (comp.extra) {
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
if (!comp.color) { // fix color text += ansiFormatCodes['reset'] + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(subComp) + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp); // it must have better way, but i lazy.
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'];
}
}
}
}); });
} }
if (comp.translate) { if (comp.translate) {
let translateString = lang[comp.translate] || comp.translate; let translateString = lang[comp.translate] || comp.translate;
if (comp.with) { if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg)); const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => { withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg); 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 const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg); translateString = translateString.replace(placeholder, arg + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp));
}); });
} }
text += translateString; text += translateString;
} }
if (comp.bold && comp.bold === 1) {
text = ansiFormatCodes['bold'] + text; text = formatfunction(comp, 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('#')) { return text;
text = ansiColorCodes[comp.color] + text + ansiFormatCodes['reset']; }
} else if (comp.color && comp.color.startsWith('#')) {
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 hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
const hexCodes = hexRegex.exec(comp.color); const hexCodes = hexRegex.exec(color);
if (hexCodes) { if (hexCodes) {
const red = parseInt(hexCodes[1], 16); const red = parseInt(hexCodes[1], 16);
const green = parseInt(hexCodes[2], 16); const green = parseInt(hexCodes[2], 16);
const blue = parseInt(hexCodes[3], 16); const blue = parseInt(hexCodes[3], 16);
const ansiColor = `\u001b[38;2;${red};${green};${blue}m`; const ansiColor = `\u001b[38;2;${red};${green};${blue}m`;
text = ansiColor + text + ansiFormatCodes['reset']; return ansiColor;
}
} else {
return '';
} }
} }
if (comp.italic && comp.italic === 1) { // useless
text = ansiFormatCodes['italic'] + text; function parseMinecraftFormat(format) {
} let result = '';
if (comp.underlined && comp.underlined === 1) { console.log(format)
text = ansiFormatCodes['underlined'] + text; if (format.bold) result += ansiFormatCodes['bold'];
} if (format.italic) result += ansiFormatCodes['italic'];
if (comp.strikethrough && comp.strikethrough === 1) { // useless if (format.underlined) result += ansiFormatCodes['underlined'];
text = ansiFormatCodes['strikethrough'] + text; if (format.strikethrough) result += ansiFormatCodes['strikethrough'];
} if (format.obfuscated) result += ansiFormatCodes['obfuscated'];
if (comp.obfuscated && comp.obfuscated === 1) { // useless return result;
text = ansiFormatCodes['obfuscated'] + text;
}
return text;
}
return extractText(jsonComponent) + ansiFormatCodes['reset'];
} }
function parseMinecraftMessageNoColor(component) { function parseMinecraftMessageNoColor(component) {
let jsonComponent; let jsonComponent;
try { try {