some sort of caching
This commit is contained in:
parent
8ee782100a
commit
e4f9f33a44
1 changed files with 18 additions and 5 deletions
|
@ -3,11 +3,7 @@ package land.chipmunk.chayapak.chomens_bot.util;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.*;
|
||||||
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.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import net.kyori.adventure.text.format.Style;
|
import net.kyori.adventure.text.format.Style;
|
||||||
import net.kyori.adventure.text.format.TextColor;
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
|
@ -35,6 +31,11 @@ public class ComponentUtilities {
|
||||||
|
|
||||||
public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])");
|
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<>();
|
public static final Map<String, String> ansiMap = new HashMap<>();
|
||||||
static {
|
static {
|
||||||
// map totallynotskidded™ from https://github.com/PrismarineJS/prismarine-chat/blob/master/index.js#L10
|
// 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); }
|
public static String stringify (Component message) { return stringify(message, null); }
|
||||||
private static String stringify (Component message, String lastColor) {
|
private static String stringify (Component message, String lastColor) {
|
||||||
try {
|
try {
|
||||||
|
if (stringCache.containsKey(message)) return stringCache.get(message);
|
||||||
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
final PartiallyStringified output = stringifyPartially(message, false, false, lastColor);
|
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));
|
for (Component child : message.children()) builder.append(stringify(child, output.lastColor));
|
||||||
|
|
||||||
|
stringCache.put(message, builder.toString());
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return "";
|
||||||
|
@ -113,6 +118,8 @@ public class ComponentUtilities {
|
||||||
public static String stringifyMotd (Component message) { return stringifyMotd(message, null); }
|
public static String stringifyMotd (Component message) { return stringifyMotd(message, null); }
|
||||||
private static String stringifyMotd (Component message, String lastColor) {
|
private static String stringifyMotd (Component message, String lastColor) {
|
||||||
try {
|
try {
|
||||||
|
if (motdCache.containsKey(message)) return motdCache.get(message);
|
||||||
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
final PartiallyStringified output = stringifyPartially(message, true, false, lastColor);
|
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));
|
for (Component child : message.children()) builder.append(stringifyMotd(child, output.lastColor));
|
||||||
|
|
||||||
|
motdCache.put(message, builder.toString());
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return "";
|
||||||
|
@ -130,6 +139,8 @@ public class ComponentUtilities {
|
||||||
public static String stringifyAnsi (Component message) { return stringifyAnsi(message, null); }
|
public static String stringifyAnsi (Component message) { return stringifyAnsi(message, null); }
|
||||||
private static String stringifyAnsi (Component message, String lastColor) {
|
private static String stringifyAnsi (Component message, String lastColor) {
|
||||||
try {
|
try {
|
||||||
|
if (ansiCache.containsKey(message)) return ansiCache.get(message);
|
||||||
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
final PartiallyStringified output = stringifyPartially(message, false, true, lastColor);
|
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));
|
for (Component child : message.children()) builder.append(stringifyAnsi(child, output.lastColor));
|
||||||
|
|
||||||
|
ansiCache.put(message, builder.toString());
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return "";
|
||||||
|
|
Loading…
Reference in a new issue