Totally not minecraft code 100%.
This commit is contained in:
parent
bedfabab1c
commit
0f7c65b5ac
2 changed files with 91 additions and 99 deletions
183
chatparser.js
183
chatparser.js
|
@ -5,10 +5,10 @@
|
|||
*
|
||||
* Language File Version: 1.21.3
|
||||
*
|
||||
* ChatParser Version: 1
|
||||
* ChatParser Version: 1359
|
||||
*
|
||||
* Update:
|
||||
* - 1.21.3
|
||||
* - fix translate
|
||||
*
|
||||
* Report Issue: https://code.chipmunk.land/Yaode_owo/Minecraft-protocol-1.21.3-chat-parser/issues/new
|
||||
*
|
||||
|
@ -128,7 +128,7 @@ let title = {
|
|||
|
||||
|
||||
bot.on('end', () => {
|
||||
title = []; delete title;
|
||||
title = []; delete title; // No idea but i think memory leak here
|
||||
});
|
||||
|
||||
|
||||
|
@ -533,6 +533,7 @@ const ansiMap = {
|
|||
function parseMinecraftMessage(component) {
|
||||
if (component === undefined || component === null) return undefined;
|
||||
if (typeof component !== "object") return component;
|
||||
let parts = []; // idk. but dont move it.
|
||||
function extractText(comp, prevColor = { color: '', have: false }, prevFormat = { format: '', have: false }) {
|
||||
let text = '';
|
||||
|
||||
|
@ -573,61 +574,56 @@ function parseMinecraftMessage(component) {
|
|||
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultTranslateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultMsg = false;
|
||||
let DefaultTranslateString = translateString;
|
||||
let fallbackMsg = false;
|
||||
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
|
||||
|
||||
if (comp.with && !fallbackMsg) {
|
||||
const withArgs = comp.with.map(arg => extractText(arg, NowColor.have ? NowColor : prevColor, NowFormat.have ? NowFormat : prevFormat) + `${VaildText(comp) ? NowFormat.have ? NowFormat.format : prevFormat.format : ""}${VaildText(comp) ? NowColor.have ? NowColor.color : prevColor.color : ""}`);
|
||||
let usedReplacements = 0;
|
||||
translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__');
|
||||
try {
|
||||
if (comp.with && !fallbackMsg) {
|
||||
const withArgs = comp.with.map(arg => extractText(arg, NowColor.have ? NowColor : prevColor, NowFormat.have ? NowFormat : prevFormat) + `${VaildText(comp) ? NowFormat.have ? NowFormat.format : prevFormat.format : ""}${VaildText(comp) ? NowColor.have ? NowColor.color : prevColor.color : ""}`);
|
||||
const matches = [...translateString.matchAll(/%(?:(\d+)\$)?([A-Za-z%]|$)/g)]; //
|
||||
if (matches.length === 0) throw new Error("NoPlaceholdersFoundError");
|
||||
|
||||
translateString = translateString.replace(/%s/g, (match, offset, string) => {
|
||||
if (offset > 0 && string[offset - 1] === '%') {
|
||||
return 's';
|
||||
}
|
||||
let usedReplacements = 0;
|
||||
let lastIndex = 0;
|
||||
|
||||
if (usedReplacements < withArgs.length) {
|
||||
if (translateString.length + withArgs[usedReplacements].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // 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, 10) - 1;
|
||||
for (const match of matches) {
|
||||
if (parts.join("").length > 32768) throw new Error("TextsHasTooMuch-OOHHHMMMYYYPPPCCC");
|
||||
let startIndex = match.index;
|
||||
|
||||
if (startIndex > lastIndex) {
|
||||
let textSegment = translateString.substring(lastIndex, startIndex);
|
||||
if (textSegment.indexOf('%') !== -1) throw new Error("UnexpectedPercentCharacterError");
|
||||
parts.push(textSegment);
|
||||
}
|
||||
|
||||
|
||||
if (match[2] === "%" && match[0] === "%%") {
|
||||
parts.push("%"); // For escaped %%
|
||||
} else if (match[2] === "s") {
|
||||
const argIndex = match[1] !== undefined ? parseInt(match[1], 10) - 1 : usedReplacements++;
|
||||
if (argIndex < 0 || argIndex >= withArgs.length) throw new Error("InvalidPlaceholderIndexError");
|
||||
parts.push(withArgs[argIndex]);
|
||||
|
||||
} else {
|
||||
throw new Error("UnsupportedPlaceholderTypeError");
|
||||
break;
|
||||
}
|
||||
|
||||
lastIndex = match.index + match[0].length;
|
||||
};
|
||||
|
||||
if (argIndex < 0 || argIndex >= withArgs.length) {
|
||||
DefaultMsg = true;
|
||||
return match;
|
||||
if (lastIndex < translateString.length) {
|
||||
let remainingText = translateString.substring(lastIndex);
|
||||
if (remainingText.indexOf('%') !== -1) throw new Error("UnmatchedTrailingPercentError");
|
||||
parts.push(remainingText);
|
||||
}
|
||||
|
||||
if (stringindex > 0 && string[stringindex - 1] === '%') {
|
||||
return match;
|
||||
}
|
||||
if (translateString.length + withArgs[argIndex].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
return `thing__placeholder__${argIndex}`;
|
||||
});
|
||||
|
||||
for (let i = 0; i < withArgs.length; i++) {
|
||||
if (translateString.length + withArgs[i].length > 32768) return '\x1B[91m*** Component has too many placeholders ***\x1B[0m'; // Prevent translate crash
|
||||
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
|
||||
const formattedArg = withArgs[i];
|
||||
return formattedArg;
|
||||
});
|
||||
translateString = parts.join("");
|
||||
}
|
||||
translateString = translateString.replace(/default_thing__placeholder__/g, 'thing__placeholder__');
|
||||
}
|
||||
|
||||
if (DefaultMsg) {
|
||||
|
||||
text += fallbackMsg ? comp.fallback : translateString;
|
||||
} catch (err) {
|
||||
text += DefaultTranslateString;
|
||||
} else if (fallbackMsg) {
|
||||
text += comp.fallback;
|
||||
} else {
|
||||
text += translateString;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -651,7 +647,7 @@ function parseMinecraftMessage(component) {
|
|||
function parseMinecraftMessageNoColor(component) {
|
||||
if (component === undefined || component === null) return undefined;
|
||||
if (typeof component !== "object") return component;
|
||||
|
||||
let parts = [];
|
||||
function extractText(comp) {
|
||||
let text = '';
|
||||
|
||||
|
@ -667,62 +663,55 @@ function parseMinecraftMessageNoColor(component) {
|
|||
|
||||
if (comp.translate) {
|
||||
let translateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultTranslateString = lang[comp.translate] || comp.translate;
|
||||
let DefaultMsg = false;
|
||||
let DefaultTranslateString = translateString;
|
||||
let fallbackMsg = false;
|
||||
if (comp.fallback && !lang[comp.translate]) fallbackMsg = true;
|
||||
|
||||
if (comp.with && !fallbackMsg) {
|
||||
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';
|
||||
}
|
||||
try {
|
||||
if (comp.with && !fallbackMsg) {
|
||||
const withArgs = comp.with.map(arg => extractText(arg));
|
||||
const matches = [...translateString.matchAll(/%(?:(\d+)\$)?([A-Za-z%]|$)/g)]; //
|
||||
if (matches.length === 0) throw new Error("NoPlaceholdersFoundError");
|
||||
|
||||
if (usedReplacements < withArgs.length) {
|
||||
if (translateString.length + withArgs[usedReplacements].length > 32768) return '*** Component has too many placeholders ***'; // 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, 10) - 1;
|
||||
let usedReplacements = 0;
|
||||
let lastIndex = 0;
|
||||
|
||||
if (argIndex < 0 || argIndex >= withArgs.length) {
|
||||
DefaultMsg = true;
|
||||
return match;
|
||||
}
|
||||
|
||||
if (stringindex > 0 && string[stringindex - 1] === '%') {
|
||||
return match;
|
||||
for (const match of matches) {
|
||||
if (parts.join("").length > 32768) throw new Error("TextsHasTooMuch-OOHHHMMMYYYPPPCCC");
|
||||
let startIndex = match.index;
|
||||
|
||||
if (startIndex > lastIndex) {
|
||||
let textSegment = translateString.substring(lastIndex, startIndex);
|
||||
if (textSegment.indexOf('%') !== -1) throw new Error("UnexpectedPercentCharacterError");
|
||||
parts.push(textSegment);
|
||||
}
|
||||
|
||||
|
||||
if (match[2] === "%" && match[0] === "%%") {
|
||||
parts.push("%"); // For escaped %%
|
||||
} else if (match[2] === "s") {
|
||||
const argIndex = match[1] !== undefined ? parseInt(match[1], 10) - 1 : usedReplacements++;
|
||||
if (argIndex < 0 || argIndex >= withArgs.length) throw new Error("InvalidPlaceholderIndexError");
|
||||
parts.push(withArgs[argIndex]);
|
||||
} else {
|
||||
throw new Error("UnsupportedPlaceholderTypeError");
|
||||
}
|
||||
|
||||
lastIndex = match.index + match[0].length;
|
||||
};
|
||||
|
||||
if (lastIndex < translateString.length) {
|
||||
let remainingText = translateString.substring(lastIndex);
|
||||
if (remainingText.indexOf('%') !== -1) throw new Error("UnmatchedTrailingPercentError");
|
||||
parts.push(remainingText);
|
||||
}
|
||||
|
||||
if (translateString.length + withArgs[argIndex].length > 32768) return '*** Component has too many placeholders ***'; // Prevent translate crash
|
||||
return `thing__placeholder__${argIndex}`;
|
||||
});
|
||||
|
||||
for (let i = 0; i < withArgs.length; i++) {
|
||||
if (translateString.length + withArgs[i].length > 32768) return '*** Component has too many placeholders ***'; // Prevent translate crash
|
||||
translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => {
|
||||
const formattedArg = withArgs[i];
|
||||
return formattedArg;
|
||||
});
|
||||
translateString = parts.join("");
|
||||
}
|
||||
translateString = translateString.replace(/default_thing__placeholder__/g, 'thing__placeholder__');
|
||||
}
|
||||
|
||||
if (DefaultMsg) {
|
||||
|
||||
text += fallbackMsg ? comp.fallback : translateString;
|
||||
} catch (err) {
|
||||
text += DefaultTranslateString;
|
||||
} else if (fallbackMsg) {
|
||||
text += comp.fallback;
|
||||
} else {
|
||||
text += translateString;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
7
main.js
7
main.js
|
@ -2,7 +2,7 @@
|
|||
* *Info in chat parser*
|
||||
*
|
||||
* Minecraft Version: 1.21.3
|
||||
* ChatParser Version: 1
|
||||
* ChatParser Version: 1, 1359
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -13,7 +13,7 @@ function createBot() {
|
|||
|
||||
const bot = mc.createClient({
|
||||
// host: '95.216.192.50', // kaboom
|
||||
// host: 'chipmunk.land', // Chipmunk
|
||||
host: 'chipmunk.land', // Chipmunk
|
||||
// host: '168.100.225.224', // Neko
|
||||
// host: 'chayapak.chipmunk.land', // chayapak
|
||||
port: 25565,
|
||||
|
@ -48,6 +48,9 @@ bot.on('custom_systemChat', (message, systemChat, packet) => {
|
|||
console.log(`[SystemChat] ${message}`);
|
||||
});
|
||||
|
||||
bot.on('login', () => {
|
||||
})
|
||||
|
||||
/*
|
||||
|
||||
// Kick message and disconnect message
|
||||
|
|
Loading…
Reference in a new issue