diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/ComponentUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/ComponentUtilities.java index 45f62a7..b5230c9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/ComponentUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/ComponentUtilities.java @@ -3,11 +3,7 @@ package land.chipmunk.chayapak.chomens_bot.util; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.TranslatableComponent; -import net.kyori.adventure.text.SelectorComponent; -import net.kyori.adventure.text.KeybindComponent; +import net.kyori.adventure.text.*; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; @@ -35,6 +31,11 @@ public class ComponentUtilities { public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])"); + // is having each map for each type a great idea? + private static final Map stringCache = new HashMap<>(); + private static final Map motdCache = new HashMap<>(); + private static final Map ansiCache = new HashMap<>(); + public static final Map ansiMap = new HashMap<>(); static { // map totallynotskidded™ from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L10 @@ -96,6 +97,8 @@ public class ComponentUtilities { public static String stringify (Component message) { return stringify(message, null); } private static String stringify (Component message, String lastColor) { try { + if (stringCache.containsKey(message)) return stringCache.get(message); + final StringBuilder builder = new StringBuilder(); final PartiallyStringified output = stringifyPartially(message, false, false, lastColor); @@ -104,6 +107,8 @@ public class ComponentUtilities { for (Component child : message.children()) builder.append(stringify(child, output.lastColor)); + stringCache.put(message, builder.toString()); + return builder.toString(); } catch (Exception e) { return ""; @@ -113,6 +118,8 @@ public class ComponentUtilities { public static String stringifyMotd (Component message) { return stringifyMotd(message, null); } private static String stringifyMotd (Component message, String lastColor) { try { + if (motdCache.containsKey(message)) return motdCache.get(message); + final StringBuilder builder = new StringBuilder(); final PartiallyStringified output = stringifyPartially(message, true, false, lastColor); @@ -121,6 +128,8 @@ public class ComponentUtilities { for (Component child : message.children()) builder.append(stringifyMotd(child, output.lastColor)); + motdCache.put(message, builder.toString()); + return builder.toString(); } catch (Exception e) { return ""; @@ -130,6 +139,8 @@ public class ComponentUtilities { public static String stringifyAnsi (Component message) { return stringifyAnsi(message, null); } private static String stringifyAnsi (Component message, String lastColor) { try { + if (ansiCache.containsKey(message)) return ansiCache.get(message); + final StringBuilder builder = new StringBuilder(); final PartiallyStringified output = stringifyPartially(message, false, true, lastColor); @@ -138,6 +149,8 @@ public class ComponentUtilities { for (Component child : message.children()) builder.append(stringifyAnsi(child, output.lastColor)); + ansiCache.put(message, builder.toString()); + return builder.toString(); } catch (Exception e) { return "";