Minecraft-protocol-1.20.4-k.../kick.js

141 lines
4 KiB
JavaScript
Raw Normal View History

2024-10-12 09:50:39 -04:00
const lang = require("./en_us.json"); // translate message
const nbt = require('prismarine-nbt');
function uuidFromIntArray (arr) {
const buf = Buffer.alloc(16)
arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) })
return buf.toString('hex')
}
2024-10-22 09:16:47 -04:00
function processNbtMessage (msg) {
2024-10-12 09:50:39 -04:00
try {
2024-10-22 09:16:47 -04:00
if (!msg || msg.type === 'end') return null;
if (typeof msg === 'string') return msg
2024-10-12 09:50:39 -04:00
2024-10-22 09:16:47 -04:00
const simplified = simplify(msg); // Ensure nbt is defined elsewhere
2024-10-12 09:50:39 -04:00
const json = JSON.stringify(simplified, (key, val) => {
if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val);
return val;
});
return json;
} catch (e) {
2024-10-22 09:16:47 -04:00
return e;
2024-10-12 09:50:39 -04:00
}
}
const msg = {
reason: '{"translate":"multiplayer.disconnect.unverified_username"}'
}
const msg2 = {
"reason": {
"type": "compound",
"value": {
"with": {
"type": "list",
"value": {
"type": "string",
"value": [ "Internal Exception: io.netty.handler.codec.EncoderException: java.io.UTFDataFormatException: encoded string (t§k腻腻腻腻腻...腻腻腻腻§rno) too long: 97869 bytes" ]
}
},
"translate": {
"type": "string",
"value": "disconnect.genericReason"
}
}
}
}
2024-10-22 09:17:14 -04:00
console.log(kickparser(msg.reason));
console.log(kickparser(msg2.reason));
2024-10-12 09:50:39 -04:00
// lazy to make it color
function kickparser(component) {
if (component === undefined) return;
2024-10-22 09:16:47 -04:00
if (typeof component === "string") return component;
2024-10-12 09:50:39 -04:00
function kickparserText(comp) {
let text = '';
2024-10-22 09:16:47 -04:00
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;
2024-10-12 09:50:39 -04:00
}
2024-10-22 09:16:47 -04:00
if (comp.extra) {
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
comp.extra.forEach(subComp => {
text += extractText(subComp);
});
}
2024-10-12 09:50:39 -04:00
if (comp.translate) {
let translateString = lang[comp.translate] || comp.translate;
2024-10-18 15:04:45 -04:00
let DefaultTranslateString = lang[comp.translate] || comp.translate;
let DefaultMsg = false;
2024-10-12 09:50:39 -04:00
if (comp.with) {
2024-10-18 15:04:45 -04:00
const withArgs = comp.with.map(arg => extractText(arg));
2024-10-12 09:50:39 -04:00
let usedReplacements = 0;
2024-10-18 15:04:45 -04:00
2024-10-22 09:16:47 -04:00
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
2024-10-18 15:04:45 -04:00
translateString = translateString.replace(/%s/g, (match, offset, string) => {
if (offset > 0 && string[offset - 1] === '%') {
return 's';
}
2024-10-22 09:16:47 -04:00
2024-10-12 09:50:39 -04:00
if (usedReplacements < withArgs.length) {
2024-10-22 09:16:47 -04:00
if (translateString.length + withArgs[usedReplacements].length > 2048) return 'Translate Crash'; // Prevent translate crash
return `thing__placeholder__${usedReplacements++}`;
2024-10-12 09:50:39 -04:00
}
2024-10-22 09:16:47 -04:00
2024-10-18 15:04:45 -04:00
DefaultMsg = true;
2024-10-12 09:50:39 -04:00
return "%s";
});
2024-10-22 09:16:47 -04:00
translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => {
const argIndex = parseInt(index, 10) - 1;
if (argIndex < 0 || argIndex >= withArgs.length) {
2024-10-18 15:04:45 -04:00
DefaultMsg = true;
return match;
}
if (stringindex > 0 && string[stringindex - 1] === '%') {
return match;
}
2024-10-22 09:16:47 -04:00
if (translateString.length + withArgs[argIndex].length > 2048) return 'Translate Crash'; // Prevent translate crash
return `thing__placeholder__${argIndex}`;
2024-10-12 09:50:39 -04:00
});
2024-10-18 15:04:45 -04:00
2024-10-22 09:16:47 -04:00
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) {
2024-10-18 15:04:45 -04:00
text += DefaultTranslateString;
} else {
text += translateString;
}
2024-10-12 09:50:39 -04:00
}
return text;
}
2024-10-22 09:16:47 -04:00
// console.log(simplify(component));
return kickparserText(simplify(component));
2024-10-12 08:49:18 -04:00
}