Last active
March 5, 2024 22:38
-
-
Save Blocksnmore/9ff66a28d9e5cb079ceef705e84cb180 to your computer and use it in GitHub Desktop.
Convert bukkit color codes to /tellraw components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ChatJSON { | |
text?: string; | |
color?: string; | |
bold?: boolean; | |
italic?: boolean; | |
underlined?: boolean; | |
strikethrough?: boolean; | |
obfuscated?: boolean; | |
score?: { | |
name: string; | |
objective: string; | |
} | |
selector?: string; | |
keybind?: string; | |
} | |
const input = [ | |
"&b&lThis supports 򪊉&l&oAll &lColors!", | |
"&a&lAnd at least <MINEPARTY_INTERNAL:bar> scoreboard objective(s)!", | |
"&6&lAnd <@s>'s selector!", | |
"&dThis is your jump key: <key.jump>" | |
].join("\n"); | |
const colorCodes = { | |
"0": "black", | |
"1": "dark_blue", | |
"2": "dark_green", | |
"3": "dark_aqua", | |
"4": "dark_red", | |
"5": "dark_purple", | |
"6": "gold", | |
"7": "gray", | |
"8": "dark_gray", | |
"9": "blue", | |
"a": "green", | |
"b": "aqua", | |
"c": "red", | |
"d": "light_purple", | |
"e": "yellow", | |
"f": "white", | |
// Modifiers | |
"k": "obfuscated", | |
"l": "bold", | |
"m": "strikethrough", | |
"n": "underlined", | |
"o": "italic", | |
"r": "reset" | |
}; | |
const selectorRegex = /<@[aeprs](\[.+\])?>/g; | |
const objectiveRegex = /<[-a-zA-Z0-9_+.]+:.+>/g; | |
const textRegex = /<(key)\..+>/g | |
let outputArray: ChatJSON[] = []; | |
let currentOutput: ChatJSON = {}; | |
for (let str of `&r${input}`.split(/&(?! )/g)) { | |
const code = str.substring(0, 1); | |
if (currentOutput.text != null) { | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
currentOutput = {}; | |
} | |
if (code == "#") { | |
const hexStr = str.substring(1, 7); | |
currentOutput.color = `#${hexStr}`; | |
str = str.substring(7); | |
} | |
if (/[0-9a-f]/i.test(code)) { | |
currentOutput.color = colorCodes[code as keyof typeof colorCodes]; | |
str = str.substring(1); | |
} | |
if (/[k-o]/i.test(code)) { | |
currentOutput[colorCodes[code as keyof typeof colorCodes] as "obfuscated" | "bold" | "strikethrough" | "underlined" | "italic"] = true; | |
str = str.substring(1); | |
} | |
if (code == "r") { | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
currentOutput = {}; | |
str = str.substring(1); | |
} | |
if (selectorRegex.test(str)) { | |
const text = str.substring(0, str.search(selectorRegex)); | |
currentOutput.text = text; | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
str = str.substring(text.length); | |
delete currentOutput.text; | |
const selector = str.substring(1, str.indexOf(">")); | |
currentOutput.selector = selector; | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
delete currentOutput.selector; | |
const afterSelector = str.substring(str.indexOf(">") + 1); | |
str = afterSelector; | |
} | |
if (objectiveRegex.test(str)) { | |
const text = str.substring(0, str.search(objectiveRegex)); | |
currentOutput.text = text; | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
str = str.substring(text.length); | |
delete currentOutput.text; | |
const objectiveSelector = str.substring(1, str.indexOf(">")); | |
const [objective, selector] = objectiveSelector.split(":"); | |
currentOutput.score = { | |
name: selector, | |
objective: objective | |
} | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
delete currentOutput.score; | |
const afterSelector = str.substring(str.indexOf(">") + 1); | |
str = afterSelector; | |
} | |
if (textRegex.test(str)) { | |
const text = str.substring(0, str.search(textRegex)); | |
currentOutput.text = text; | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
str = str.substring(text.length); | |
delete currentOutput.text; | |
const key = str.substring(1, str.indexOf(">")); | |
const [type, ...args] = key.split("."); | |
const arg = args.join("."); | |
switch (type) { | |
case "key": { | |
currentOutput.keybind = `key.${arg}`; | |
break; | |
} | |
// I was planning on adding stuff like hover and command suggest but this code feels unoptimized for it | |
} | |
outputArray.push(JSON.parse(JSON.stringify(currentOutput))); | |
delete currentOutput.keybind; | |
const afterSelector = str.substring(str.indexOf(">") + 1); | |
str = afterSelector; | |
} | |
if (str == "") continue; | |
currentOutput.text = str; | |
} | |
outputArray.push(currentOutput); | |
outputArray = outputArray.filter((v) => JSON.stringify(v) != "{}" && (v.text != null || v.selector != null || v.score != null || v.keybind != null)); | |
console.log(JSON.stringify(outputArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment