fix translate

This commit is contained in:
Yaode_owo 2024-10-30 22:07:36 -04:00
parent 03dfdbb2a8
commit dcb46d99d8

64
kick.js
View file

@ -1,3 +1,6 @@
// Minecaft Version 1.20.4
// Im not sure if it work on 1.21.1 minecraft version
const lang = require("./en_us.json"); // translate message
const nbt = require('prismarine-nbt');
@ -7,20 +10,24 @@ function uuidFromIntArray (arr) {
return buf.toString('hex')
}
function processNbtMessage (msg) {
function simplify (data) {
try {
if (!msg || msg.type === 'end') return null;
if (typeof msg === 'string') return msg
const simplified = simplify(msg); // Ensure nbt is defined elsewhere
const json = JSON.stringify(simplified, (key, val) => {
if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val);
return val;
});
return json;
if (data === undefined) return data;
function transform (value, type) {
if (type === 'compound') {
return Object.keys(value).reduce(function (acc, key) {
acc[key] = simplify(value[key])
return acc
}, {})
}
if (type === 'list') {
return value.value.map(function (v) { return transform(v, value.type) })
}
return value
}
return transform(data.value, data.type)
} catch (e) {
return e;
return e;
}
}
@ -61,27 +68,24 @@ function kickparser(component) {
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
text += comp.text;
}
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') {
text += comp[""];
}
if (comp && typeof comp === 'string' || typeof comp === 'number') {
return comp;
}
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
text += extractText(subComp);
});
}
if (comp.translate) {
let translateString = lang[comp.translate] || comp.translate;
let DefaultTranslateString = lang[comp.translate] || comp.translate;
let DefaultMsg = false;
let fallbackMsg = false;
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
if (comp.with && !fallbackMsg) {
const withArgs = comp.with.map(arg => kickparserText(arg));
let usedReplacements = 0;
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
@ -91,7 +95,7 @@ function kickparser(component) {
}
if (usedReplacements < withArgs.length) {
if (translateString.length + withArgs[usedReplacements].length > 2048) return 'Translate Crash'; // Prevent translate crash
if (translateString.length + withArgs[usedReplacements].length > 16384) return 'Translate Crash'; // Prevent translate crash
return `thing__placeholder__${usedReplacements++}`;
}
@ -111,12 +115,12 @@ function kickparser(component) {
return match;
}
if (translateString.length + withArgs[argIndex].length > 2048) return 'Translate Crash'; // Prevent translate crash
if (translateString.length + withArgs[argIndex].length > 16384) return 'Translate Crash'; // Prevent translate crash
return `thing__placeholder__${argIndex}`;
});
for (let i = 0; i < withArgs.length; i++) {
if (translateString.length + withArgs[i].length > 2048) return 'Translate Crash'; // Prevent translate crash
if (translateString.length + withArgs[i].length > 16384) return 'Translate Crash'; // Prevent translate crash
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
const formattedArg = withArgs[i];
return formattedArg;
@ -127,15 +131,21 @@ function kickparser(component) {
if (DefaultMsg) {
text += DefaultTranslateString;
} else {
} else if (fallbackMsg) {
text += comp.fallback;
} else {
text += translateString;
}
}
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
text += kickparserText(subComp);
});
}
return text;
}
// console.log(simplify(component));
return kickparserText(simplify(component));
}