fix %n$s
This commit is contained in:
parent
988048783d
commit
4fab8457f6
1 changed files with 48 additions and 47 deletions
|
@ -1,4 +1,4 @@
|
|||
const lang = require("./en_us.json"); // translate message
|
||||
const lang = require("../util/en_us.json"); // translate message
|
||||
const nbt = require('prismarine-nbt');
|
||||
|
||||
function uuidFromIntArray (arr) {
|
||||
|
@ -26,13 +26,13 @@ function processNbtMessage(msg) {
|
|||
|
||||
return json;
|
||||
} catch (e) {
|
||||
return '{"text":""}';
|
||||
return `{"text":"${e}"}`;
|
||||
}
|
||||
}
|
||||
|
||||
let profilelesschat = {};
|
||||
let systemchat = {};
|
||||
let playerchat = {};
|
||||
|
||||
function inject(bot) {
|
||||
|
||||
bot.on('system_chat', (packet) => { // system
|
||||
|
@ -114,6 +114,8 @@ bot.on('player_chat', (packet) => { // player
|
|||
bot.emit('Custom_AllChat', msg, playerchat)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
const ansiColorCodes = {
|
||||
'§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m',
|
||||
'§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m',
|
||||
|
@ -164,7 +166,14 @@ function parseMinecraftMessage(component) {
|
|||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
if (comp.with) {
|
||||
const withArgs = comp.with.map(arg => { return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(arg) });
|
||||
const withArgs = comp.with.map(arg => {
|
||||
return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(arg);
|
||||
});
|
||||
|
||||
withArgs.forEach((arg, index) => {
|
||||
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
||||
translateString = translateString.replace(regex, formatfunction(comp, arg));
|
||||
});
|
||||
|
||||
let usedReplacements = 0;
|
||||
translateString = translateString.replace(/%s/g, () => {
|
||||
|
@ -173,11 +182,6 @@ function parseMinecraftMessage(component) {
|
|||
}
|
||||
return "%s";
|
||||
});
|
||||
|
||||
withArgs.forEach((arg, index) => {
|
||||
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
||||
translateString = translateString.replace(regex, formatfunction(comp, arg));
|
||||
});
|
||||
}
|
||||
text += formatfunction(comp, translateString);
|
||||
}
|
||||
|
@ -188,39 +192,6 @@ function parseMinecraftMessage(component) {
|
|||
return extractText(jsonComponent) + ansiFormatCodes['reset'];
|
||||
}
|
||||
|
||||
function formatfunction(comp, text) {
|
||||
if (text === undefined) return '';
|
||||
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
|
||||
}
|
||||
|
||||
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 = '';
|
||||
if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
|
||||
if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
|
||||
if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
|
||||
if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
|
||||
if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function parseMinecraftMessageNoColor(component) {
|
||||
if (component === undefined) return;
|
||||
|
@ -255,6 +226,11 @@ function parseMinecraftMessageNoColor(component) {
|
|||
if (comp.with) {
|
||||
const withArgs = comp.with.map(arg => extractText(arg));
|
||||
|
||||
withArgs.forEach((arg, index) => {
|
||||
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
||||
translateString = translateString.replace(regex, arg);
|
||||
});
|
||||
|
||||
let usedReplacements = 0;
|
||||
translateString = translateString.replace(/%s/g, () => {
|
||||
if (usedReplacements < withArgs.length) {
|
||||
|
@ -262,11 +238,6 @@ function parseMinecraftMessageNoColor(component) {
|
|||
}
|
||||
return "%s";
|
||||
});
|
||||
|
||||
withArgs.forEach((arg, index) => {
|
||||
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
||||
translateString = translateString.replace(regex, arg);
|
||||
});
|
||||
}
|
||||
text += translateString;
|
||||
}
|
||||
|
@ -279,7 +250,37 @@ function parseMinecraftMessageNoColor(component) {
|
|||
return msgText;
|
||||
}
|
||||
|
||||
function formatfunction(comp, text) {
|
||||
if (text === undefined) return '';
|
||||
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
|
||||
}
|
||||
|
||||
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 = '';
|
||||
if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
|
||||
if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
|
||||
if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
|
||||
if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
|
||||
if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = { inject };
|
Loading…
Reference in a new issue