From 168b0520fea475bd1f96d659d63cdd8cdfe451e1 Mon Sep 17 00:00:00 2001 From: Yaode_owo Date: Tue, 22 Oct 2024 09:16:47 -0400 Subject: [PATCH] fix some issue --- kick.js | 96 +++++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/kick.js b/kick.js index 2b06aaf..986a13d 100644 --- a/kick.js +++ b/kick.js @@ -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,33 +53,28 @@ 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 (typeof comp.value === 'string') { - text += comp.value; - } - if (comp.with) { - text += kickparserText(comp.with); - } - - if (comp.value) { - [comp.value].forEach(subComp => { - if (typeof subComp === 'string') text += subComp; - text += kickparserText(subComp); - }); + 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; @@ -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; - - if (argIndex >= withArgs.length) { + translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => { + const argIndex = parseInt(index, 10) - 1; + + if (argIndex < 0 || argIndex >= withArgs.length) { DefaultMsg = true; return match; } @@ -124,13 +111,21 @@ function kickparser(component) { return match; } - const formattedArg = withArgs[argIndex]; - if (translateString.length + formattedArg.length > 2048) return 'Translate Crash'; - return formattedArg; + if (translateString.length + withArgs[argIndex].length > 2048) return 'Translate Crash'; // Prevent translate crash + return `thing__placeholder__${argIndex}`; }); - } - if (DefaultMsg) { + 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; } else { text += translateString; @@ -141,7 +136,6 @@ function kickparser(component) { return text; } - - - return kickparserText(jsonComponent); + // console.log(simplify(component)); + return kickparserText(simplify(component)); } \ No newline at end of file