diff --git a/core/src/main/java/mc/core/text/Text.java b/core/src/main/java/mc/core/text/Text.java index 1108343..2c3b4ec 100644 --- a/core/src/main/java/mc/core/text/Text.java +++ b/core/src/main/java/mc/core/text/Text.java @@ -4,132 +4,212 @@ */ package mc.core.text; +import com.google.common.collect.ImmutableList; +import lombok.EqualsAndHashCode; import lombok.Getter; -import lombok.Setter; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.StringJoiner; @Getter +@EqualsAndHashCode public class Text { - @Setter - private String string; - @Setter - private TextColor color; - @Setter - private TextStyle style; - private List childs; + private static final Text EMPTY = new Text(); + private static final Text NEW_LINE = new Text("\n", null, null, null); - private Text() { } + private final String content; + private final TextColor color; + private final TextStyle style; + private final ImmutableList children; - public Builder toBuilder() { - return new Builder(this); + private Text() { + content = ""; + color = null; + style = null; + children = null; } - public static Text of(String string) { - return of(string, null, null); - } - - public static Text of(Object... objects) { - Text text = new Text(); - - for(Object obj : objects) { - if (obj instanceof String) { - if (text.string == null) { - text.string = (String) obj; - } else { - text.string = text.string.concat((String) obj); - } - } else if (obj instanceof TextStyle) { - if (text.style == null) { - text.style = (TextStyle) obj; - } else { - text.style.concat((TextStyle) obj); - } - } else if (obj instanceof TextColor) { - text.color = (TextColor) obj; - } - } - - return text; - } - - public static Builder builder() { - return new Builder(); - } - - public static Builder builder(String string) { - return new Builder(string); - } - - public static Builder builder(Object... objects) { - return new Builder(objects); - } - - public static class Builder { - private Text text; - - private Builder() { - this.text = new Text(); - } - - private Builder(String string) { - this.text = new Text(); - this.text.string = string; - } - - private Builder(Object... objects) { - this.text = Text.of(objects); - } - - public Builder color(TextColor color) { - this.text.color = color; - return this; - } - - public Builder style(TextStyle style) { - if (this.text.style == null) { - this.text.style = new TextStyle(null,null,null,null,null); - } - this.text.style.concat(style); - return this; - } - - public Builder append(Text text) { - if (this.text.childs == null) { - this.text.childs = new ArrayList<>(); - } - - this.text.childs.add(text); - return this; - } - - public Text build() { - return this.text; - } + private Text(String content, TextColor color, TextStyle style, ImmutableList children) { + this.content = content; + this.color = color; + this.style = style; + this.children = children; } public boolean isEmpty() { - boolean result = string.isEmpty(); + boolean result = content.isEmpty(); - if (childs != null) { - for(Text child : childs) { - result = child.isEmpty(); + if (children != null && !children.isEmpty()) { + for (Text child : children) { + result = result && child.isEmpty(); } } return result; } - public String toPlainText() { - if (childs != null) { + public String toPlain() { + if (children != null && !children.isEmpty()) { final StringJoiner sj = new StringJoiner(""); - sj.add(string); - childs.forEach(child -> sj.add(child.toPlainText())); + children.forEach(child -> sj.add(child.toPlain())); return sj.toString(); } else { - return string; + return content; + } + } + + public static class Builder { + @Getter + private String content; + @Getter + private TextColor color; + @Getter + private TextStyle style; + private List children; + + public Builder() { + this(""); + } + + public Builder(String content) { + this.content = content; + this.color = null; + this.style = null; + this.children = new ArrayList<>(); + } + + public Builder(Object... objects) { + for(Object obj : objects) { + if (obj instanceof String) { + if (this.content == null) { + this.content = (String) obj; + } else { + this.content = this.content.concat((String) obj); + } + } else if (obj instanceof TextStyle) { + if (this.style == null) { + this.style = new TextStyle(null,null,null,null,null); + } else { + this.style.merge((TextStyle) obj); + } + } else if (obj instanceof TextColor) { + this.color = (TextColor) obj; + } + } + + this.children = new ArrayList<>(); + } + + public List getChildren() { + return Collections.unmodifiableList(children); + } + + public Builder color(TextColor color) { + this.color = color; + return this; + } + + public Builder style(TextStyle style) { + if (this.style == null) { + this.style = new TextStyle(null,null,null,null,null);; + } else { + this.style.merge(style); + } + + return this; + } + + public Builder style(TextStyle... styles) { + if (this.style == null) { + this.style = new TextStyle(null, null, null, null, null); + } + + for(TextStyle style : styles) { + this.style.merge(style); + } + + return this; + } + + public Builder append(Text child) { + this.children.add(child); + return this; + } + + public Builder append(Text... children) { + Collections.addAll(this.children, children); + return this; + } + + public Text build() { + if (children.isEmpty()) { + return Text.EMPTY; + } + + return new Text( + content, + color, + style, + ImmutableList.copyOf(children) + ); + } + } + + public static Builder builder() { + return new Builder(); + } + + public static Builder builder(String content) { + return new Builder(content); + } + + public static Builder builder(Object... objects) { + return new Builder(objects); + } + + public static Text of() { + return EMPTY; + } + + public static Text of(String string) { + if (string == null || string.isEmpty()) { + return EMPTY; + } else if (string.equals("\n")) { + return NEW_LINE; + } else { + return new Text(string, null, null, null); + } + } + + public static Text of(Object... objects) { + TextColor color = null; + TextStyle style = null; + String content = null; + + for(Object obj : objects) { + if (obj instanceof String) { + if (content == null) { + content = (String) obj; + } else { + content = content.concat((String) obj); + } + } else if (obj instanceof TextStyle) { + if (style == null) { + style = (TextStyle) obj; + } else { + style.merge((TextStyle) obj); + } + } else if (obj instanceof TextColor) { + color = (TextColor) obj; + } + } + + if (content == null || content.isEmpty()) { + return EMPTY; + } else { + return new Text(content, color, style, null); } } }