From 8499a2ceeb89e2de1cb8009bc87119823ced0701 Mon Sep 17 00:00:00 2001 From: Felix Cravic <themode@outlook.fr> Date: Fri, 31 Jul 2020 19:18:10 +0200 Subject: [PATCH] ColoredText compiled string can now be cached --- .../net/minestom/server/chat/ColoredText.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/net/minestom/server/chat/ColoredText.java b/src/main/java/net/minestom/server/chat/ColoredText.java index 148a731d7..e7da4652b 100644 --- a/src/main/java/net/minestom/server/chat/ColoredText.java +++ b/src/main/java/net/minestom/server/chat/ColoredText.java @@ -15,8 +15,14 @@ public class ColoredText { private String message; + // true if the compiled string is up-to-date, false otherwise + private boolean updated; + // the compiled json string of this colored text (can be outdated) + private String compiledJson; + private ColoredText(String message) { this.message = message; + refreshUpdate(); } public static ColoredText of(ChatColor color, String message) { @@ -39,6 +45,7 @@ public class ColoredText { public ColoredText append(ChatColor color, String message) { this.message += color + message; + refreshUpdate(); return this; } @@ -48,6 +55,7 @@ public class ColoredText { public ColoredText appendFormat(String message) { this.message += message; + refreshUpdate(); return this; } @@ -81,8 +89,19 @@ public class ColoredText { return message; } + /** + * Compile this text and cache it for further execution + * + * @return the raw json string of this colored text + */ @Override public String toString() { + if (updated) { + return compiledJson; + } + + this.compiledJson = getJsonObject().toString(); + this.updated = true; return getJsonObject().toString(); } @@ -275,6 +294,10 @@ public class ColoredText { return value ? "true" : "false"; } + private void refreshUpdate() { + this.updated = false; + } + private enum MessageType { RAW, KEYBIND, TRANSLATABLE }