some sort of caching

This commit is contained in:
Chayapak 2023-07-31 11:53:49 +07:00
parent 8ee782100a
commit e4f9f33a44

View file

@ -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<Component, String> stringCache = new HashMap<>();
private static final Map<Component, String> motdCache = new HashMap<>();
private static final Map<Component, String> ansiCache = new HashMap<>();
public static final Map<String, String> 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 "";