Fix some %s issue
This commit is contained in:
parent
233ff9166b
commit
2b4bdbdf70
1 changed files with 42 additions and 31 deletions
|
@ -10,7 +10,7 @@ const bot = mc.createClient({
|
|||
|
||||
bot.on('system_chat', (packet) => {
|
||||
console.log(parseMinecraftMessage(packet));
|
||||
console.log(JSON.stringify(packet, null, 2))
|
||||
//console.log(JSON.stringify(packet, null, 2))
|
||||
});
|
||||
|
||||
const ansiCodes = {
|
||||
|
@ -64,10 +64,9 @@ function parseMinecraftMessage(component) {
|
|||
function extractText(comp) {
|
||||
let text = '';
|
||||
|
||||
if (typeof comp === 'string') {
|
||||
if (typeof comp === 'string' || typeof comp === 'number') {
|
||||
return comp;
|
||||
}
|
||||
|
||||
if (comp.text) {
|
||||
text += extractText(comp.text);
|
||||
}
|
||||
|
@ -96,39 +95,51 @@ function parseMinecraftMessage(component) {
|
|||
text += extractText(valueComp);
|
||||
});
|
||||
}
|
||||
if (comp.translate) { // anyone can help me ?
|
||||
let translateString = lang[comp.translate.value] || comp.translate.value;
|
||||
|
||||
if (typeof translateString === 'string') {
|
||||
if (comp.with) {
|
||||
const withArray = Array.isArray(comp.with.value) ? comp.with.value : [comp.with.value];
|
||||
let formattedString = translateString;
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate.value] || comp.translate.value;
|
||||
|
||||
const replacements = [];
|
||||
if (typeof translateString === 'string') {
|
||||
if (comp.with) {
|
||||
const withArray = Array.isArray(comp.with.value) ? comp.with.value : [comp.with.value];
|
||||
let formattedString = translateString;
|
||||
|
||||
withArray.forEach((replacementText) => {
|
||||
if (replacementText.value) {
|
||||
const items = Array.isArray(replacementText.value) ? replacementText.value : [replacementText.value];
|
||||
|
||||
items.forEach((item) => {
|
||||
//console.log(item);
|
||||
replacements.push(extractText(item));
|
||||
});
|
||||
}
|
||||
});
|
||||
const replacements = [];
|
||||
|
||||
let replacementIndex = 0;
|
||||
formattedString = formattedString.replace(/%s/g, () => {
|
||||
withArray.forEach((replacementText) => {
|
||||
if (replacementText.value) {
|
||||
const items = Array.isArray(replacementText.value) ? replacementText.value : [replacementText.value];
|
||||
|
||||
items.forEach((item) => {
|
||||
console.log(item)
|
||||
const itemText = extractText(item);
|
||||
if (itemText) replacements.push(itemText);
|
||||
if (item.with && item.with.value) {
|
||||
const nestedValues = Array.isArray(item.with.value) ? item.with.value : [item.with.value];
|
||||
nestedValues.forEach(nestedItem => {
|
||||
const nestedText = extractText(nestedItem);
|
||||
if (nestedText) replacements.push(nestedText);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return replacementIndex < replacements.length ? replacements[replacementIndex++] : '%s';
|
||||
});
|
||||
//console.log(formattedString)
|
||||
text += formattedString;
|
||||
}
|
||||
} else {
|
||||
console.warn(`Translation not found for: ${comp.translate.value}`);
|
||||
}
|
||||
}
|
||||
let replacementIndex = 0;
|
||||
formattedString = formattedString.replace(/%s/g, () => {
|
||||
return replacementIndex < replacements.length ? replacements[replacementIndex++] : '%s';
|
||||
});
|
||||
formattedString = formattedString.replace(/%s/g, () => {
|
||||
return replacementIndex < replacements.length ? replacements[replacementIndex++] : '%s';
|
||||
});
|
||||
text += formattedString;
|
||||
} else {
|
||||
text += extractText(translateString);
|
||||
}
|
||||
} else {
|
||||
console.warn(`Translation not found for: ${comp.translate.value}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (comp.color && ansiCodes[comp.color.value] && !comp.color.value.startsWith('#')) {
|
||||
text = ansiCodes[comp.color.value] + text;
|
||||
|
|
Reference in a new issue