fix color

This commit is contained in:
Yaode_owo 2024-07-26 10:36:30 -04:00
parent 94f38309a2
commit 72dc02951a

View file

@ -14,6 +14,7 @@ function parseCommand(message) {
function inject(bot) { function inject(bot) {
bot.on('systemChat', (packet) => { bot.on('systemChat', (packet) => {
//console.log(packet)
const jsonmsg = JSON.parse(packet.formattedMessage); const jsonmsg = JSON.parse(packet.formattedMessage);
const msg = parseMinecraftMessage(packet.formattedMessage); const msg = parseMinecraftMessage(packet.formattedMessage);
const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage); const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage);
@ -46,10 +47,10 @@ bot.on('playerChat', (packet) => {
case 1: // /me text case 1: // /me text
msg = `* ${SenderName} ${formattedMessage}`; msg = `* ${SenderName} ${formattedMessage}`;
break; break;
case 2: // someone tell you text case 2: // someone /tell you text
msg = `${SenderName} whispers to you: ${formattedMessage}`; msg = `${SenderName} whispers to you: ${formattedMessage}`;
break; break;
case 3: // you tell someone text case 3: // you /tell someone text
msg = `You whisper to ${TargetName}: ${formattedMessage}`; msg = `You whisper to ${TargetName}: ${formattedMessage}`;
break; break;
case 4: // player chat case 4: // player chat
@ -73,20 +74,21 @@ bot.on('playerChat', (packet) => {
if (vmsg !== undefined) bot.emit('custom_systemChat', vmsg, "", ""); if (vmsg !== undefined) bot.emit('custom_systemChat', vmsg, "", "");
}); });
const ansiCodes = { // color code const ansiColorCodes = {
'§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m', '§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m',
'§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m', '§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m',
'§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m', '§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m',
'§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m', '§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m',
'§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m',
'§k': '\x1B[5m', '§r': '\x1B[0m',
'black': '\x1B[30m', 'dark_blue': '\x1B[34m', 'dark_green': '\x1B[32m', 'black': '\x1B[30m', 'dark_blue': '\x1B[34m', 'dark_green': '\x1B[32m',
'dark_aqua': '\x1B[36m', 'dark_red': '\x1B[31m', 'dark_purple': '\x1B[35m', 'dark_aqua': '\x1B[36m', 'dark_red': '\x1B[31m', 'dark_purple': '\x1B[35m',
'gold': '\x1B[33m', 'gray': '\x1B[37m', 'dark_gray': '\x1B[90m', 'blue': '\x1B[94m', 'gold': '\x1B[33m', 'gray': '\x1B[37m', 'dark_gray': '\x1B[90m', 'blue': '\x1B[94m',
'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m', 'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m',
'yellow': '\x1B[93m', 'white': '\x1B[97m', 'bold': '\x1B[1m', 'italic': '\x1B[3m', 'yellow': '\x1B[93m', 'white': '\x1B[97m'
'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m', // Color };
'undefined': '\x1B[0m'
const ansiFormatCodes = {
'§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m', '§k': '\x1B[5m', '§r': '\x1B[0m',
'bold': '\x1B[1m', 'italic': '\x1B[3m', 'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m',
}; };
function parseMinecraftMessage(component) { function parseMinecraftMessage(component) {
@ -99,7 +101,6 @@ function parseMinecraftMessage(component) {
function extractText(comp) { function extractText(comp) {
let text = ''; let text = '';
if (comp.text) { if (comp.text) {
text += comp.text; text += comp.text;
} }
@ -109,9 +110,26 @@ function parseMinecraftMessage(component) {
if (typeof comp === 'string' || typeof comp === 'number') { if (typeof comp === 'string' || typeof comp === 'number') {
text += comp; text += comp;
} }
if (comp.extra) { if (comp.extra) {
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
if (!comp.color) { // fix color
text += extractText(subComp); 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 += ansiColorCodes[comp.color] + extractText(subComp) + ansiFormatCodes['reset'];
}
}
}
}); });
} }
if (comp.translate) { if (comp.translate) {
@ -121,16 +139,15 @@ function parseMinecraftMessage(component) {
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);
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg); translateString = translateString.replace(placeholder, arg);
}); });
} }
text += translateString; text += translateString;
} }
if (comp.color && ansiColorCodes[comp.color] && !comp.color.startsWith('#')) {
if (comp.color && ansiCodes[comp.color] && !comp.color.startsWith('#')) { text = ansiColorCodes[comp.color] + text + ansiFormatCodes['reset'];
text = ansiCodes[comp.color] + text + ansiCodes['reset'];
} else if (comp.color && comp.color.startsWith('#')) { } 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 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(comp.color);
@ -139,33 +156,29 @@ function parseMinecraftMessage(component) {
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 + ansiCodes['reset']; text = ansiColor + text + ansiFormatCodes['reset'];
} }
} }
if (comp.bold && comp.bold === 1) { if (comp.bold && comp.bold === 1) {
text = ansiCodes['§l'] + text; text = ansiFormatCodes['bold'] + text;
} } // i dont know why add bold, some color dont show, maybe is my issue or code.
if (comp.italic && comp.italic === 1) { if (comp.italic && comp.italic === 1) { // useless
text = ansiCodes['§o'] + text; text = ansiFormatCodes['italic'] + text;
} }
if (comp.underlined && comp.underlined === 1) { if (comp.underlined && comp.underlined === 1) {
text = ansiCodes['§n'] + text; text = ansiFormatCodes['underlined'] + text;
} }
if (comp.strikethrough && comp.strikethrough === 1) { if (comp.strikethrough && comp.strikethrough === 1) { // useless
text = ansiCodes['§m'] + text; text = ansiFormatCodes['strikethrough'] + text;
} }
if (comp.obfuscated && comp.obfuscated === 1) { if (comp.obfuscated && comp.obfuscated === 1) { // useless
text = ansiCodes['§k'] + text; text = ansiFormatCodes['obfuscated'] + text;
} }
return text; return text;
} }
return extractText(jsonComponent) + ansiFormatCodes['reset'];
return extractText(jsonComponent) + ansiCodes['reset'];
} }
function parseMinecraftMessageNoColor(component) { function parseMinecraftMessageNoColor(component) {
let jsonComponent; let jsonComponent;
try { try {
@ -212,9 +225,6 @@ function parseMinecraftMessageNoColor(component) {
return extractText(jsonComponent); return extractText(jsonComponent);
} }
} }
module.exports = inject; module.exports = inject;