refactor: rewrite ComponentUtilities component parser (still a bit broken but at least a bit better on the codestyle side)

This commit is contained in:
Chayapak 2024-12-28 17:55:04 +07:00
parent 9b675bfed4
commit 052e57a799
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
6 changed files with 265 additions and 253 deletions

View file

@ -1 +1 @@
1303 1341

View file

@ -209,7 +209,7 @@ public class Bot {
// we also set other stuffs here // we also set other stuffs here
session.send( session.send(
new ServerboundClientInformationPacket( new ServerboundClientInformationPacket(
ComponentUtilities.language.getOrDefault("language.code", "en-us"), ComponentUtilities.LANGUAGE.getOrDefault("language.code", "en-us"),
16, 16,
ChatVisibility.FULL, ChatVisibility.FULL,
true, true,

View file

@ -339,7 +339,7 @@ public class ChatPlugin extends Bot.Listener {
if (bot.options.useChat) { if (bot.options.useChat) {
if (!targets.equals("@a")) return; // worst fix of all time!1! if (!targets.equals("@a")) return; // worst fix of all time!1!
final String stringified = ComponentUtilities.stringifyMotd(component).replace("§", "&"); final String stringified = ComponentUtilities.stringifySectionSign(component).replace("§", "&");
send(stringified); send(stringified);
} else { } else {
bot.core.run("minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(component)); bot.core.run("minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(component));

View file

@ -98,15 +98,10 @@ public class DiscordPlugin {
if (string.length() > 2000 - 12) { if (string.length() > 2000 - 12) {
sendMessage(CodeBlockUtilities.escape(string), channelId); sendMessage(CodeBlockUtilities.escape(string), channelId);
} else { } else {
final String ansi = ComponentUtilities.stringifyAnsi(component, true); final String ansi = ComponentUtilities.stringifyDiscordAnsi(component);
sendMessage( sendMessage(
CodeBlockUtilities.escape( CodeBlockUtilities.escape(ansi),
ansi
.replace(
"\u001b[9", "\u001b[3"
)
),
channelId channelId
); );
} }

View file

@ -278,7 +278,7 @@ public class NBSConverter implements Converter {
private static final Map<String, String> subtitles = new HashMap<>(); private static final Map<String, String> subtitles = new HashMap<>();
static { static {
for (Map.Entry<String, String> entry : ComponentUtilities.language.entrySet()) { for (Map.Entry<String, String> entry : ComponentUtilities.LANGUAGE.entrySet()) {
if (!entry.getKey().startsWith("subtitles.")) continue; if (!entry.getKey().startsWith("subtitles.")) continue;
subtitles.put(entry.getKey(), entry.getValue()); subtitles.put(entry.getKey(), entry.getValue());

View file

@ -20,45 +20,9 @@ import java.util.regex.Pattern;
// totallynotskidded from chipmunkbot and added colors (ignore the ohio code please,..,.) // totallynotskidded from chipmunkbot and added colors (ignore the ohio code please,..,.)
public class ComponentUtilities { public class ComponentUtilities {
// component parsing // component parsing
public static final Map<String, String> language = loadJsonStringMap("language.json"); public static final Map<String, String> LANGUAGE = loadJsonStringMap("language.json");
private static final Map<String, String> voiceChatLanguage = loadJsonStringMap("voiceChatLanguage.json"); public static final Map<String, String> VOICE_CHAT_LANGUAGE = loadJsonStringMap("voiceChatLanguage.json");
private static final Map<String, String> keybinds = loadJsonStringMap("keybinds.json"); public static final Map<String, String> KEYBINDINGS = loadJsonStringMap("keybinds.json");
public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])");
public static final int MAX_DEPTH = 16;
public static final Map<String, String> ansiMap = new HashMap<>();
static {
// map totallynotskidded from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L10
ansiMap.put("0", "\u001b[30m");
ansiMap.put("1", "\u001b[34m");
ansiMap.put("2", "\u001b[32m");
ansiMap.put("3", "\u001b[36m");
ansiMap.put("4", "\u001b[31m");
ansiMap.put("5", "\u001b[35m");
ansiMap.put("6", "\u001b[33m");
ansiMap.put("7", "\u001b[37m");
ansiMap.put("8", "\u001b[90m");
ansiMap.put("9", "\u001b[94m");
ansiMap.put("a", "\u001b[92m");
ansiMap.put("b", "\u001b[96m");
ansiMap.put("c", "\u001b[91m");
ansiMap.put("d", "\u001b[95m");
ansiMap.put("e", "\u001b[93m");
ansiMap.put("f", "\u001b[97m");
ansiMap.put("l", "\u001b[1m");
ansiMap.put("o", "\u001b[3m");
ansiMap.put("n", "\u001b[4m");
ansiMap.put("m", "\u001b[9m");
ansiMap.put("k", "\u001b[6m");
ansiMap.put("r", "\u001b[0m");
}
public record PartiallyStringified(
String output,
String lastColor
) {}
private static Map<String, String> loadJsonStringMap (String name) { private static Map<String, String> loadJsonStringMap (String name) {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
@ -74,186 +38,226 @@ public class ComponentUtilities {
return map; return map;
} }
private static String getOrReturnFallback (TranslatableComponent component) { public static String getOrReturnFallback (TranslatableComponent component) {
final String key = component.key(); final String key = component.key();
final String minecraftKey = language.get(key); final String minecraftKey = LANGUAGE.get(key);
final String voiceChatKey = voiceChatLanguage.get(key); final String voiceChatKey = VOICE_CHAT_LANGUAGE.get(key);
if (minecraftKey != null) return minecraftKey; if (minecraftKey != null) return minecraftKey;
else if (voiceChatKey != null) return voiceChatKey; else if (voiceChatKey != null) return voiceChatKey;
else return component.fallback() != null ? component.fallback() : key; else return component.fallback() != null ? component.fallback() : key;
} }
public static String stringify (Component message) { return stringify(message, null, 0); } public static String stringify (Component message) { return new ComponentParser().stringify(message, ComponentParser.ParseType.PLAIN); }
private static String stringify (Component message, String lastColor, int depth) { public static String stringifySectionSign (Component message) { return new ComponentParser().stringify(message, ComponentParser.ParseType.SECTION_SIGNS); }
if (depth > MAX_DEPTH) return ""; public static String stringifyAnsi (Component message) { return new ComponentParser().stringify(message, ComponentParser.ParseType.ANSI); }
public static String stringifyDiscordAnsi (Component message) { return new ComponentParser().stringify(message, ComponentParser.ParseType.DISCORD_ANSI); }
try { private static class ComponentParser {
final StringBuilder builder = new StringBuilder(); public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])");
final PartiallyStringified output = stringifyPartially(message, false, false, lastColor, false, depth); public static final int MAX_DEPTH = 16;
builder.append(output.output); public static final Map<String, String> ansiMap = new HashMap<>();
static {
for (Component child : message.children()) builder.append(stringify(child, lastColor != null ? lastColor : output.lastColor, depth)); // map totallynotskidded from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L10
ansiMap.put("0", "\u001b[30m");
return builder.toString(); ansiMap.put("1", "\u001b[34m");
} catch (Exception e) { ansiMap.put("2", "\u001b[32m");
return ""; ansiMap.put("3", "\u001b[36m");
} ansiMap.put("4", "\u001b[31m");
} ansiMap.put("5", "\u001b[35m");
ansiMap.put("6", "\u001b[33m");
public static String stringifyMotd (Component message) { return stringifyMotd(message, null, 0); } ansiMap.put("7", "\u001b[37m");
private static String stringifyMotd (Component message, String lastColor, int depth) { ansiMap.put("8", "\u001b[90m");
if (depth > MAX_DEPTH) return ""; ansiMap.put("9", "\u001b[94m");
ansiMap.put("a", "\u001b[92m");
try { ansiMap.put("b", "\u001b[96m");
final StringBuilder builder = new StringBuilder(); ansiMap.put("c", "\u001b[91m");
ansiMap.put("d", "\u001b[95m");
final PartiallyStringified output = stringifyPartially(message, true, false, lastColor, false, depth); ansiMap.put("e", "\u001b[93m");
ansiMap.put("f", "\u001b[97m");
builder.append(output.output); ansiMap.put("l", "\u001b[1m");
ansiMap.put("o", "\u001b[3m");
for (Component child : message.children()) builder.append(stringifyMotd(child, lastColor != null ? lastColor : output.lastColor, depth)); ansiMap.put("n", "\u001b[4m");
ansiMap.put("m", "\u001b[9m");
return builder.toString(); ansiMap.put("k", "\u001b[6m");
} catch (Exception e) { ansiMap.put("r", "\u001b[0m");
return "";
}
}
public static String stringifyAnsi (Component message) { return stringifyAnsi(message, null, false, 0); }
public static String stringifyAnsi (Component message, boolean noHex) { return stringifyAnsi(message, null, noHex, 0); }
private static String stringifyAnsi (Component message, String lastColor, boolean noHex, int depth) {
if (depth > MAX_DEPTH) return "";
try {
final StringBuilder builder = new StringBuilder();
final PartiallyStringified output = stringifyPartially(message, false, true, lastColor, noHex, depth);
builder.append(output.output);
for (Component child : message.children()) builder.append(stringifyAnsi(child, lastColor != null ? lastColor : output.lastColor, noHex, depth));
return builder.toString();
} catch (Exception e) {
return "";
}
}
public static PartiallyStringified stringifyPartially (Component message, boolean motd, boolean ansi, String lastColor, boolean noHex, int depth) {
return switch (message) {
case TextComponent t_component -> stringifyPartially(t_component, motd, ansi, lastColor, noHex);
case TranslatableComponent t_component -> stringifyPartially(t_component, motd, ansi, lastColor, noHex, depth);
case SelectorComponent t_component -> stringifyPartially(t_component, motd, ansi, lastColor, noHex);
case KeybindComponent t_component -> stringifyPartially(t_component, motd, ansi, lastColor, noHex);
default -> new PartiallyStringified("", null);
};
}
public static String getStyle (Style textStyle, boolean motd) {
if (textStyle == null) return null;
StringBuilder style = new StringBuilder();
for (Map.Entry<TextDecoration, TextDecoration.State> decorationEntry : textStyle.decorations().entrySet()) {
final TextDecoration decoration = decorationEntry.getKey();
final TextDecoration.State state = decorationEntry.getValue();
if (state == TextDecoration.State.NOT_SET || state == TextDecoration.State.FALSE) continue;
if (!motd) {
switch (decoration) {
case BOLD -> style.append(ansiMap.get("l"));
case ITALIC -> style.append(ansiMap.get("o"));
case OBFUSCATED -> style.append(ansiMap.get("k"));
case UNDERLINED -> style.append(ansiMap.get("n"));
case STRIKETHROUGH -> style.append(ansiMap.get("m"));
}
} else {
switch (decoration) {
case BOLD -> style.append("§l");
case ITALIC -> style.append("§o");
case OBFUSCATED -> style.append("§k");
case UNDERLINED -> style.append("§n");
case STRIKETHROUGH -> style.append("§m");
}
}
} }
return style.toString(); private ParseType type;
} private int formatsPlaceholdersCount = 0;
public static String getColor (TextColor color, boolean motd, boolean ansi, boolean noHex) { private String lastStyle = "";
if (color == null) return null;
private String stringify (Component message, ParseType type) {
this.type = type;
if (formatsPlaceholdersCount > MAX_DEPTH) return "";
// map totallynotskidded too from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L299
String code;
if (color == NamedTextColor.BLACK) code = "0";
else if (color == NamedTextColor.DARK_BLUE) code = "1";
else if (color == NamedTextColor.DARK_GREEN) code = "2";
else if (color == NamedTextColor.DARK_AQUA) code = "3";
else if (color == NamedTextColor.DARK_RED) code = "4";
else if (color == NamedTextColor.DARK_PURPLE) code = "5";
else if (color == NamedTextColor.GOLD) code = "6";
else if (color == NamedTextColor.GRAY) code = "7";
else if (color == NamedTextColor.DARK_GRAY) code = "8";
else if (color == NamedTextColor.BLUE) code = "9";
else if (color == NamedTextColor.GREEN) code = "a";
else if (color == NamedTextColor.AQUA) code = "b";
else if (color == NamedTextColor.RED) code = "c";
else if (color == NamedTextColor.LIGHT_PURPLE) code = "d";
else if (color == NamedTextColor.YELLOW) code = "e";
else if (color == NamedTextColor.WHITE) code = "f";
else {
try { try {
code = color.asHexString(); final StringBuilder builder = new StringBuilder();
} catch (NullPointerException e) {
code = ""; // mabe...,,.,.., final String color = getColor(message.color());
final String style = getStyle(message.style());
final String output = stringifyPartially(message, color, style);
builder.append(output);
for (Component child : message.children()) {
final ComponentParser parser = new ComponentParser();
parser.lastStyle = lastStyle + color + style;
parser.formatsPlaceholdersCount = formatsPlaceholdersCount;
builder.append(parser.stringify(child, type));
}
if (type == ParseType.DISCORD_ANSI) {
// as of the time writing this (2024-12-28) discord doesn't support the bright colors yet
return builder.toString().replace("\u001b[9", "\u001b[3");
} else {
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
return "";
} }
} }
if (motd) { public String stringifyPartially (Component message, String color, String style) {
return "§" + code; return switch (message) {
} else if (ansi) { case TextComponent t_component -> stringifyPartially(t_component, color, style);
String ansiCode = ansiMap.get(code); case TranslatableComponent t_component -> stringifyPartially(t_component, color, style);
if (ansiCode == null) { case SelectorComponent t_component -> stringifyPartially(t_component, color, style);
if (noHex) { case KeybindComponent t_component -> stringifyPartially(t_component, color, style);
final int rgb = Integer.parseInt(code.substring(1), 16); default -> "";
};
}
final String chatColor = ColorUtilities.getClosestChatColor(rgb); public String getStyle (Style textStyle) {
if (textStyle == null) return "";
ansiCode = ansiMap.get(chatColor); StringBuilder style = new StringBuilder();
} else {
ansiCode = "\u001b[38;2;" + for (Map.Entry<TextDecoration, TextDecoration.State> decorationEntry : textStyle.decorations().entrySet()) {
color.red() + final TextDecoration decoration = decorationEntry.getKey();
";" + final TextDecoration.State state = decorationEntry.getValue();
color.green() +
";" + if (state == TextDecoration.State.NOT_SET || state == TextDecoration.State.FALSE) continue;
color.blue() +
"m"; if (type == ParseType.ANSI) {
switch (decoration) {
case BOLD -> style.append(ansiMap.get("l"));
case ITALIC -> style.append(ansiMap.get("o"));
case OBFUSCATED -> style.append(ansiMap.get("k"));
case UNDERLINED -> style.append(ansiMap.get("n"));
case STRIKETHROUGH -> style.append(ansiMap.get("m"));
}
} else if (type == ParseType.SECTION_SIGNS) {
switch (decoration) {
case BOLD -> style.append("§l");
case ITALIC -> style.append("§o");
case OBFUSCATED -> style.append("§k");
case UNDERLINED -> style.append("§n");
case STRIKETHROUGH -> style.append("§m");
}
} }
} }
return ansiCode; return style.toString();
} else return null; }
}
public static PartiallyStringified stringifyPartially (TextComponent message, boolean motd, boolean ansi, String lastColor, boolean noHex) { public String getColor (TextColor color) {
if ((motd || ansi) && /* don't color big messages -> */ message.content().length() < 25_000) { if (color == null) return "";
final String color = getColor(message.color(), motd, ansi, noHex);
final String style = getStyle(message.style(), motd);
String replacedContent = message.content(); // map totallynotskidded too from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L299
// seems very mabe mabe String code;
if (ansi && replacedContent.contains("§")) { if (color == NamedTextColor.BLACK) code = "0";
// is try-catch a great idea? else if (color == NamedTextColor.DARK_BLUE) code = "1";
else if (color == NamedTextColor.DARK_GREEN) code = "2";
else if (color == NamedTextColor.DARK_AQUA) code = "3";
else if (color == NamedTextColor.DARK_RED) code = "4";
else if (color == NamedTextColor.DARK_PURPLE) code = "5";
else if (color == NamedTextColor.GOLD) code = "6";
else if (color == NamedTextColor.GRAY) code = "7";
else if (color == NamedTextColor.DARK_GRAY) code = "8";
else if (color == NamedTextColor.BLUE) code = "9";
else if (color == NamedTextColor.GREEN) code = "a";
else if (color == NamedTextColor.AQUA) code = "b";
else if (color == NamedTextColor.RED) code = "c";
else if (color == NamedTextColor.LIGHT_PURPLE) code = "d";
else if (color == NamedTextColor.YELLOW) code = "e";
else if (color == NamedTextColor.WHITE) code = "f";
else {
try {
code = color.asHexString();
} catch (NullPointerException e) {
code = ""; // mabe...,,.,..,
}
}
if (type == ParseType.SECTION_SIGNS) {
return "§" + code;
} else if (type == ParseType.ANSI || type == ParseType.DISCORD_ANSI) {
String ansiCode = ansiMap.get(code);
if (ansiCode == null) {
if (type == ParseType.DISCORD_ANSI) {
// gets the closest color to the hex
final int rgb = Integer.parseInt(code.substring(1), 16);
final String chatColor = ColorUtilities.getClosestChatColor(rgb);
ansiCode = ansiMap.get(chatColor);
} else {
ansiCode = "\u001b[38;2;" +
color.red() +
";" +
color.green() +
";" +
color.blue() +
"m";
}
}
return ansiCode;
} else {
return "";
}
}
private String getPartialResultAndSetLastColor (String originalResult, String color, String style) {
if (type == ParseType.PLAIN) return originalResult;
String resetCode;
if (type == ParseType.ANSI || type == ParseType.DISCORD_ANSI) resetCode = ansiMap.get("r");
else resetCode = "§r";
final String result =
lastStyle + color + style +
originalResult +
resetCode;
lastStyle = color + style;
return result;
}
private String stringifyPartially (String message, String color, String style) {
if (type == ParseType.PLAIN) return message;
final boolean isAllAnsi = type == ParseType.ANSI || type == ParseType.DISCORD_ANSI;
String replacedContent = message;
// processes section signs
// not processing invalid codes is INTENTIONAL and it is a FEATURE
if (isAllAnsi && replacedContent.contains("§")) {
// is try-catch a great idea for these?
try { try {
replacedContent = Pattern replacedContent = Pattern
.compile("(§.)") .compile("(§.)")
.matcher(message.content()) .matcher(message)
.replaceAll(m -> { .replaceAll(m -> {
final String code = m.group(0).substring(1); final String code = m.group(0).substring(1);
@ -263,69 +267,82 @@ public class ComponentUtilities {
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
// messy af return getPartialResultAndSetLastColor(replacedContent, color, style);
return new PartiallyStringified((lastColor != null ? lastColor : "") + (color != null ? color : "") + (style != null ? style : "") + replacedContent + (ansi ? ansiMap.get("r") : ""), color);
} }
return new PartiallyStringified(message.content(), null); private String stringifyPartially (TextComponent message, String color, String style) {
} return stringifyPartially(message.content(), color, style);
}
public static PartiallyStringified stringifyPartially (TranslatableComponent message, boolean motd, boolean ansi, String lastColor, boolean noHex, int depth) { private String stringifyPartially (TranslatableComponent message, String color, String style) {
String format = getOrReturnFallback(message); final String format = getOrReturnFallback(message);
// totallynotskidded from HBot (and changed a bit) // totallynotskiddedfrom HBot (and changed a bit)
Matcher matcher = ARG_PATTERN.matcher(format); Matcher matcher = ARG_PATTERN.matcher(format);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
final String style = getStyle(message.style(), motd); // not checking if arguments length equals input format length
final String _color = getColor(message.color(), motd, ansi, noHex); // is INTENTIONAL and is a FEATURE
String color; int i = 0;
if (_color == null) color = ""; while (matcher.find()) {
else color = _color; formatsPlaceholdersCount++;
if (matcher.group().equals("%%")) {
int i = 0; matcher.appendReplacement(sb, "%");
while (matcher.find()) {
depth++;
if (matcher.group().equals("%%")) {
matcher.appendReplacement(sb, "%");
} else {
String idxStr = matcher.group(1);
int idx = idxStr == null ? i++ : (Integer.parseInt(idxStr) - 1);
if (idx >= 0 && idx < message.arguments().size()) {
matcher.appendReplacement(
sb,
Matcher.quoteReplacement(
motd ?
stringifyMotd(message.arguments().get(idx).asComponent(), lastColor, depth + 1) + color :
(
ansi ?
stringifyAnsi(message.arguments().get(idx).asComponent(), lastColor, noHex, depth + 1) + color :
stringify(message.arguments().get(idx).asComponent(), lastColor, depth + 1)
)
)
);
} else { } else {
matcher.appendReplacement(sb, ""); final String idxStr = matcher.group(1);
int idx = idxStr == null ? i++ : (Integer.parseInt(idxStr) - 1);
if (idx >= 0 && idx < message.arguments().size()) {
final ComponentParser parser = new ComponentParser();
parser.lastStyle = lastStyle + color + style;
parser.formatsPlaceholdersCount = formatsPlaceholdersCount;
matcher.appendReplacement(
sb,
Matcher.quoteReplacement(
parser.stringify(
message.arguments()
.get(idx)
.asComponent(),
type
) + color // + color IMPORTANT!!!!
)
);
} else {
matcher.appendReplacement(sb, "");
}
} }
} }
matcher.appendTail(sb);
return getPartialResultAndSetLastColor(sb.toString(), color, style);
} }
matcher.appendTail(sb);
return new PartiallyStringified((lastColor != null ? lastColor : "") + color + (style != null && ansi ? style : "") + sb + (ansi ? ansiMap.get("r") : ""), _color); // on the client side, this acts just like TextComponent
} // and does NOT process any players stuff
private String stringifyPartially (SelectorComponent message, String color, String style) {
return stringifyPartially(message.pattern(), style, color);
}
public static PartiallyStringified stringifyPartially (SelectorComponent message, boolean motd, boolean ansi, String lastColor, boolean noHex) { public String stringifyPartially (KeybindComponent message, String color, String style) {
final String style = getStyle(message.style(), motd); final String keybind = message.keybind();
final String _color = getColor(message.color(), motd, ansi, noHex);
String color;
if (_color == null) color = "";
else color = _color;
return new PartiallyStringified((lastColor != null ? lastColor : "") + color + (style != null && ansi ? style : "") + message.pattern(), _color); // * Client-side selector components are equivalent to text ones, and do NOT list entities.
}
public static PartiallyStringified stringifyPartially (KeybindComponent message, boolean motd, boolean ansi, String lastColor, boolean noHex) { // FIXME: this isn't the correct way to parse keybinds
String keybind = message.keybind(); final Component component = KEYBINDINGS.containsKey(keybind) ?
Component component = keybinds.containsKey(keybind) ? Component.translatable(keybinds.get(keybind)) : Component.text(keybind); Component.translatable(KEYBINDINGS.get(keybind)) :
return stringifyPartially(component, motd, ansi, lastColor, noHex, 0); Component.text(keybind);
return stringifyPartially(component, color, style);
}
public enum ParseType {
PLAIN,
SECTION_SIGNS,
ANSI,
DISCORD_ANSI
}
} }
} }