Removed RichMessage format retention, should be done manually. + cleanup

This commit is contained in:
themode 2020-11-13 00:28:22 +01:00
parent d3d3cc6553
commit 13932a6ca3

View File

@ -42,18 +42,30 @@ public class RichMessage extends JsonMessage {
Check.notNull(coloredText, "ColoredText cannot be null");
RichMessage richMessage = new RichMessage();
appendText(richMessage, coloredText, FormatRetention.ALL);
appendText(richMessage, coloredText);
return richMessage;
}
private static void appendText(@NotNull RichMessage richMessage, @NotNull ColoredText coloredText,
@NotNull FormatRetention formatRetention) {
RichComponent component = new RichComponent(coloredText, formatRetention);
private static void appendText(@NotNull RichMessage richMessage, @NotNull ColoredText coloredText) {
RichComponent component = new RichComponent(coloredText);
richMessage.components.add(component);
richMessage.currentComponent = component;
}
/**
* Adds a new rich component to the message.
*
* @param coloredText the text composing the rich component
* @return the rich message
*/
public RichMessage append(@NotNull ColoredText coloredText) {
Check.notNull(coloredText, "ColoredText cannot be null");
appendText(this, coloredText);
return this;
}
/**
* Sets the click event of the current rich component.
*
@ -87,31 +99,6 @@ public class RichMessage extends JsonMessage {
return this;
}
/**
* Adds a new rich component to the message.
*
* @param coloredText the text composing the rich component
* @param formatRetention the format retention of the added component
* @return the rich message
*/
public RichMessage append(@NotNull ColoredText coloredText, @NotNull FormatRetention formatRetention) {
Check.notNull(coloredText, "ColoredText cannot be null");
appendText(this, coloredText, formatRetention);
return this;
}
/**
* Adds a new rich component to the message,
* the format retention is set to {@link FormatRetention#ALL}.
*
* @param coloredText the text composing the rich component
* @return the rich message
*/
public RichMessage append(@NotNull ColoredText coloredText) {
return append(coloredText, FormatRetention.ALL);
}
@NotNull
@Override
public JsonObject getJsonObject() {
@ -121,18 +108,14 @@ public class RichMessage extends JsonMessage {
if (cacheComponents.isEmpty())
return new JsonObject();
RichComponent firstComponent = cacheComponents.remove(0);
List<JsonObject> firstComponentObjects = getComponentObject(firstComponent);
JsonObject mainObject = firstComponentObjects.remove(0);
if (cacheComponents.isEmpty() && firstComponentObjects.isEmpty())
return mainObject;
// The main object contains the extra array, with an empty text to do not share its state with the others
JsonObject mainObject = new JsonObject();
mainObject.addProperty("text", "");
// The extra array contains all the components
JsonArray extraArray = new JsonArray();
for (JsonObject firstComponentObject : firstComponentObjects) {
extraArray.add(firstComponentObject);
}
// Add all the components
for (RichComponent component : cacheComponents) {
List<JsonObject> componentObjects = getComponentObject(component);
for (JsonObject componentObject : componentObjects) {
@ -205,24 +188,18 @@ public class RichMessage extends JsonMessage {
return eventObject;
}
public enum FormatRetention {
ALL, CLICK_EVENT, HOVER_EVENT, NONE
}
/**
* Represents a {@link ColoredText} with a click and hover event (can be null).
*/
private static class RichComponent {
private final ColoredText text;
private final FormatRetention formatRetention;
private ChatClickEvent clickEvent;
private ChatHoverEvent hoverEvent;
private String insertion;
private RichComponent(@NotNull ColoredText text, @NotNull FormatRetention formatRetention) {
private RichComponent(@NotNull ColoredText text) {
this.text = text;
this.formatRetention = formatRetention;
}
@NotNull
@ -230,11 +207,6 @@ public class RichMessage extends JsonMessage {
return text;
}
@NotNull
public FormatRetention getFormatRetention() {
return formatRetention;
}
@Nullable
public ChatClickEvent getClickEvent() {
return clickEvent;