2023-06-26 23:26:15 -04:00
|
|
|
const language = require("minecraft-data")("1.16.1").language;
|
2022-11-12 20:32:35 -05:00
|
|
|
|
|
|
|
const colormap = {
|
2023-06-26 23:26:15 -04:00
|
|
|
black: "§0",
|
|
|
|
dark_blue: "§1",
|
|
|
|
dark_green: "§2",
|
|
|
|
dark_aqua: "§3",
|
|
|
|
dark_red: "§4",
|
|
|
|
dark_purple: "§5",
|
|
|
|
gold: "§6",
|
|
|
|
gray: "§7",
|
|
|
|
dark_gray: "§8",
|
|
|
|
blue: "§9",
|
|
|
|
green: "§a",
|
|
|
|
aqua: "§b",
|
|
|
|
red: "§c",
|
|
|
|
light_purple: "§d",
|
|
|
|
yellow: "§e",
|
|
|
|
white: "§f",
|
|
|
|
reset: "§r"
|
|
|
|
};
|
2022-11-12 20:32:35 -05:00
|
|
|
|
|
|
|
const ansimap = {
|
2023-06-26 23:26:15 -04:00
|
|
|
"§0": "\x1b[0m\x1b[30m",
|
|
|
|
"§1": "\x1b[0m\x1b[34m",
|
|
|
|
"§2": "\x1b[0m\x1b[32m",
|
|
|
|
"§3": "\x1b[0m\x1b[36m",
|
|
|
|
"§4": "\x1b[0m\x1b[31m",
|
|
|
|
"§5": "\x1b[0m\x1b[35m",
|
|
|
|
"§6": "\x1b[0m\x1b[33m",
|
|
|
|
"§7": "\x1b[0m\x1b[37m",
|
|
|
|
"§8": "\x1b[0m\x1b[90m",
|
|
|
|
"§9": "\x1b[0m\x1b[94m",
|
|
|
|
"§a": "\x1b[0m\x1b[92m",
|
|
|
|
"§b": "\x1b[0m\x1b[96m",
|
|
|
|
"§c": "\x1b[0m\x1b[91m",
|
|
|
|
"§d": "\x1b[0m\x1b[95m",
|
|
|
|
"§e": "\x1b[0m\x1b[93m",
|
|
|
|
"§f": "\x1b[0m\x1b[97m",
|
|
|
|
"§r": "\x1b[0m",
|
|
|
|
"§l": "\x1b[1m",
|
|
|
|
"§o": "\x1b[3m",
|
|
|
|
"§n": "\x1b[4m",
|
|
|
|
"§m": "\x1b[9m",
|
|
|
|
"§k": "\x1b[6m",
|
|
|
|
};
|
2022-11-12 20:32:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a native minecraft text component in string form.
|
2023-06-26 23:26:15 -04:00
|
|
|
* @param {string} json_string - A text component string, such as the chat packet's "message" property.
|
2022-11-12 20:32:35 -05:00
|
|
|
* @returns {object} Parsed message in { raw, clean, ansi } form.
|
|
|
|
*/
|
2023-06-26 23:26:15 -04:00
|
|
|
function parseText(json_string) {
|
|
|
|
const json = JSON.parse(json_string);
|
2022-11-12 20:32:35 -05:00
|
|
|
|
2023-06-26 23:26:15 -04:00
|
|
|
let raw = parseJson(json, {color:"reset"});
|
|
|
|
if (raw.startsWith("§r")) {
|
|
|
|
raw = raw.substring(2);
|
|
|
|
}
|
|
|
|
let clean = raw.replace(/§./g, "").replace(/§/g, "");
|
|
|
|
let ansi = raw.replace(/§[a-f0-9rlonmk]/g, (m) => {
|
|
|
|
return ansimap[m];
|
|
|
|
});
|
|
|
|
return { raw, clean, ansi };
|
2022-11-12 20:32:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-26 23:26:15 -04:00
|
|
|
* Parses a native minecraft text component in JSON form.
|
|
|
|
* @param {object} json - The json message.
|
|
|
|
* @param {object} parent - The parent json.
|
2022-11-12 20:32:35 -05:00
|
|
|
* @returns {string} The parsed raw string.
|
|
|
|
*/
|
2023-06-26 23:26:15 -04:00
|
|
|
function parseJson(json, parent) {
|
|
|
|
if (typeof json === "string") {
|
|
|
|
json = {text:json};
|
|
|
|
}
|
2022-11-12 20:32:35 -05:00
|
|
|
|
2023-06-26 23:26:15 -04:00
|
|
|
json.color = json.color || parent.color;
|
|
|
|
json.bold = json.bold || parent.bold;
|
|
|
|
json.italic = json.italic || parent.italic;
|
|
|
|
json.underlined = json.underlined || parent.underlined;
|
|
|
|
json.strikethrough = json.strikethrough || parent.strikethrough;
|
|
|
|
json.obfuscated = json.obfuscated || parent.obfuscated;
|
2022-11-12 20:32:35 -05:00
|
|
|
|
2023-06-26 23:26:15 -04:00
|
|
|
let raw = "";
|
|
|
|
//if (json.color.startsWith('#'))
|
|
|
|
// raw += '§' + color
|
|
|
|
//else
|
|
|
|
raw += colormap[json.color] || "";
|
|
|
|
if (json.bold) { raw += "§l"; }
|
|
|
|
if (json.italic) { raw += "§o"; }
|
|
|
|
if (json.underlined) { raw += "§n"; }
|
|
|
|
if (json.strikethrough) { raw += "§m"; }
|
|
|
|
if (json.obfuscated) { raw += "§k"; }
|
|
|
|
if (json.text) {
|
|
|
|
raw += json.text;
|
|
|
|
}
|
|
|
|
if (json.translate) { // I checked with the native minecraft code. This is how Minecraft does the matching and group indexing. -hhhzzzsss
|
|
|
|
if (language[json.translate]) {
|
|
|
|
let _with = json.with || [];
|
|
|
|
let i = 0;
|
|
|
|
raw += language[json.translate].replace(/%(?:(\\d+)\\$)?(s|%)/g, (g0, g1) => {
|
|
|
|
if (g0 === "%%") {
|
|
|
|
return "%";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let idx = g1 ? parseInt(g1) : i++;
|
|
|
|
if (_with[idx]) {
|
|
|
|
return parseJson(_with[idx], json);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-12 20:32:35 -05:00
|
|
|
}
|
2023-06-26 23:26:15 -04:00
|
|
|
else {
|
|
|
|
raw += json.translate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (json.extra) {
|
|
|
|
json.extra.forEach(function (extra) {
|
|
|
|
raw += parseJson(extra, json);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return raw;
|
2022-11-12 20:32:35 -05:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:26:15 -04:00
|
|
|
module.exports = parseText;
|