ColoredText compiled string can now be cached

This commit is contained in:
Felix Cravic 2020-07-31 19:18:10 +02:00
parent 7b947ba09b
commit 8499a2ceeb

View File

@ -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
}