fix some issue

This commit is contained in:
Yaode_owo 2024-10-22 09:16:47 -04:00
parent 063291c3e2
commit 168b0520fe

74
kick.js
View file

@ -7,18 +7,12 @@ function uuidFromIntArray (arr) {
return buf.toString('hex')
}
function processNbtMessage(msg) {
function processNbtMessage (msg) {
try {
if (typeof msg === 'string') { // just create simple nbt structure
msg = {
id: 'string',
value: msg
};
}
if (!msg || msg.type === 'end') return null;
if (typeof msg === 'string') return msg
if (!msg || msg.type === 'end') return msg;
const simplified = nbt.simplify(msg); // Ensure nbt is defined elsewhere
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;
@ -26,7 +20,7 @@ function processNbtMessage(msg) {
return json;
} catch (e) {
return '{"text":""}';
return e;
}
}
@ -59,30 +53,25 @@ console.log(kickparser(processNbtMessage(msg2.reason)));
// lazy to make it color
function kickparser(component) {
if (component === undefined) return;
try {
jsonComponent = JSON.parse(component);
} catch (e) {
console.error("Invalid JSON format:", component);
return '';
}
if (typeof component === "string") return component;
function kickparserText(comp) {
let text = '';
if (typeof comp === 'string') {
text += comp;
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') {
text += comp.text;
}
if (typeof comp.value === 'string') {
text += comp.value;
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') {
text += comp[""];
}
if (comp.with) {
text += kickparserText(comp.with);
if (comp && typeof comp === 'string' || typeof comp === 'number') {
return comp;
}
if (comp.value) {
[comp.value].forEach(subComp => {
if (typeof subComp === 'string') text += subComp;
text += kickparserText(subComp);
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
text += extractText(subComp);
});
}
@ -93,29 +82,27 @@ function kickparser(component) {
if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
let usedReplacements = 0;
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
translateString = translateString.replace(/%s/g, (match, offset, string) => {
if (offset > 0 && string[offset - 1] === '%') {
return 's';
}
if (usedReplacements < withArgs.length) {
const formattedArg = withArgs[usedReplacements];
if (translateString.length + formattedArg.length > 2048) return 'Translate Crash';
usedReplacements++;
return formattedArg;
if (translateString.length + withArgs[usedReplacements].length > 2048) return 'Translate Crash'; // Prevent translate crash
return `thing__placeholder__${usedReplacements++}`;
}
DefaultMsg = true;
return "%s";
});
translateString = translateString.replace(/%(\d+)\$s/g, (match, index, stringindex, string) => {
const argIndex = parseInt(index) - 1;
translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => {
const argIndex = parseInt(index, 10) - 1;
if (argIndex >= withArgs.length) {
if (argIndex < 0 || argIndex >= withArgs.length) {
DefaultMsg = true;
return match;
}
@ -124,11 +111,19 @@ function kickparser(component) {
return match;
}
const formattedArg = withArgs[argIndex];
if (translateString.length + formattedArg.length > 2048) return 'Translate Crash';
if (translateString.length + withArgs[argIndex].length > 2048) 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
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
const formattedArg = withArgs[i];
return formattedArg;
});
}
translateString = translateString.replace(/default_thing__placeholder__/g, 'thing__placeholder__');
}
if (DefaultMsg) {
text += DefaultTranslateString;
@ -141,7 +136,6 @@ function kickparser(component) {
return text;
}
return kickparserText(jsonComponent);
// console.log(simplify(component));
return kickparserText(simplify(component));
}