mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
ColoredText compiled string can now be cached
This commit is contained in:
parent
7b947ba09b
commit
8499a2ceeb
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user