diff --git a/protocol/src/main/java/mc/protocol/model/text/Text.java b/protocol/src/main/java/mc/protocol/model/text/Text.java index 11d1d50..0472114 100644 --- a/protocol/src/main/java/mc/protocol/model/text/Text.java +++ b/protocol/src/main/java/mc/protocol/model/text/Text.java @@ -1,79 +1,139 @@ package mc.protocol.model.text; -import lombok.EqualsAndHashCode; -import lombok.Getter; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.Accessors; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; @Accessors(fluent = true) -@Getter -@EqualsAndHashCode -@ToString +@AllArgsConstructor +@Data public class Text { - public static final Text EMPTY = of(""); - private TextColor color; private TextStyle style; private String content; private List children; - Text(TextColor color, TextStyle style, String content, List children) { - this.color = color; - this.style = style; - this.content = content; - this.children = children; + public static Text of(String string) { + return new Text(null, null, string, null); } - public static Text of(String content) { - return new Text(null, null, content, null); + public static Text of(TextColor color, String string) { + return new Text(color, null, string, null); } - public static Text of(TextColor color, String content) { - return new Text(color, null, content, null); + public static Text of(TextStyle style, String string) { + return new Text(null, style, string, null); } - public static Text of(TextStyle style, String content) { - return new Text(null, style, content, null); + public static Text of(TextColor color, TextStyle style, String string) { + return new Text(color, style, string, null); } - public static Text of(TextColor color, TextStyle style, String content) { - return new Text(color, style, content, null); - } - - public static Text.Builder builder() { - return new Text.Builder(); + public static Builder builder() { + return new Builder(); } + @NoArgsConstructor + @ToString public static class Builder { - private final LinkedList chain = new LinkedList<>(); + + private StringBuilder contentBuilder; + private TextStyle.Builder styleBuilder; + private TextColor color; + private List children; + + public Builder append(String content) { + if (this.contentBuilder == null) { + this.contentBuilder = new StringBuilder(content); + } else { + this.contentBuilder.append(content); + } + return this; + } public Builder append(Text text) { - if (text == null || EMPTY.equals(text)) { - return this; + if (children == null) { + children = new ArrayList<>(); } - chain.add(text); + children.add(text); + return this; + } + + public Builder style(TextStyle style) { + //@formatter:off + if (style.bold() != null) bold(style.bold()); + if (style.italic() != null) italic(style.italic()); + if (style.underline() != null) underline(style.underline()); + if (style.strikethrough() != null) strikethrough(style.strikethrough()); + if (style.obfuscated() != null) obfuscated(style.obfuscated()); + //@formatter:on + + return this; + } + + public Builder color(TextColor color) { + this.color = color; + return this; + } + + public Builder bold(Boolean bold) { + if (this.styleBuilder == null) { + this.styleBuilder = TextStyle.builder(); + } + + this.styleBuilder.bold(bold); + return this; + } + + public Builder italic(Boolean italic) { + if (this.styleBuilder == null) { + this.styleBuilder = TextStyle.builder(); + } + + this.styleBuilder.italic(italic); + return this; + } + + public Builder underline(Boolean underline) { + if (this.styleBuilder == null) { + this.styleBuilder = TextStyle.builder(); + } + + this.styleBuilder.underline(underline); + return this; + } + + public Builder strikethrough(Boolean strikethrough) { + if (this.styleBuilder == null) { + this.styleBuilder = TextStyle.builder(); + } + + this.styleBuilder.strikethrough(strikethrough); + return this; + } + + public Builder obfuscated(Boolean obfuscated) { + if (this.styleBuilder == null) { + this.styleBuilder = TextStyle.builder(); + } + + this.styleBuilder.obfuscated(obfuscated); return this; } public Text build() { - if (chain.isEmpty()) { - return EMPTY; - } - - Text rootText = chain.pollFirst(); - - if (!chain.isEmpty()) { - rootText.children = new ArrayList<>(); - rootText.children.addAll(chain); - } - - return rootText; + return new Text( + color, + styleBuilder == null ? null : styleBuilder.build(), + contentBuilder == null ? null : contentBuilder.toString(), + children); } } } diff --git a/protocol/src/main/java/mc/protocol/model/text/TextStyle.java b/protocol/src/main/java/mc/protocol/model/text/TextStyle.java index e4856e3..06c6040 100644 --- a/protocol/src/main/java/mc/protocol/model/text/TextStyle.java +++ b/protocol/src/main/java/mc/protocol/model/text/TextStyle.java @@ -4,12 +4,8 @@ import lombok.*; import lombok.experimental.Accessors; @AllArgsConstructor(access = AccessLevel.PRIVATE) -@Builder(builderClassName = "Builder") @Accessors(fluent = true) -@Getter -@Setter(AccessLevel.PACKAGE) -@EqualsAndHashCode -@ToString +@Data @SuppressWarnings("java:S1845") public class TextStyle { @@ -28,11 +24,59 @@ public class TextStyle { private Boolean strikethrough; private Boolean obfuscated; - void merge(TextStyle style) { - if (style.bold != null) this.bold = style.bold; - if (style.italic != null) this.italic = style.italic; - if (style.underline != null) this.underline = style.underline; - if (style.strikethrough != null) this.strikethrough = style.strikethrough; - if (style.obfuscated != null) this.obfuscated = style.obfuscated; + public static Builder builder() { + return new TextStyle.Builder(); + } + + @NoArgsConstructor + @ToString + public static class Builder { + + private Boolean bold; + private Boolean italic; + private Boolean underline; + private Boolean strikethrough; + private Boolean obfuscated; + + public Builder bold(Boolean bold) { + this.bold = bold; + return this; + } + + public Builder italic(Boolean italic) { + this.italic = italic; + return this; + } + + public Builder underline(Boolean underline) { + this.underline = underline; + return this; + } + + public Builder strikethrough(Boolean strikethrough) { + this.strikethrough = strikethrough; + return this; + } + + public Builder obfuscated(Boolean obfuscated) { + this.obfuscated = obfuscated; + return this; + } + + public Builder merge(TextStyle style) { + //@formatter:off + if (style.bold != null) this.bold = style.bold; + if (style.italic != null) this.italic = style.italic; + if (style.underline != null) this.underline = style.underline; + if (style.strikethrough != null) this.strikethrough = style.strikethrough; + if (style.obfuscated != null) this.obfuscated = style.obfuscated; + //@formatter:on + + return this; + } + + public TextStyle build() { + return new TextStyle(bold, italic, underline, strikethrough, obfuscated); + } } } diff --git a/protocol/src/test/java/mc/protocol/model/text/TextTest.java b/protocol/src/test/java/mc/protocol/model/text/TextTest.java index cbed53f..601576c 100644 --- a/protocol/src/test/java/mc/protocol/model/text/TextTest.java +++ b/protocol/src/test/java/mc/protocol/model/text/TextTest.java @@ -8,24 +8,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class TextTest { - @Test - void emptyTest() { - Text actual = Text.builder().build(); - Text expected = Text.EMPTY; - - assertEquals(expected, actual); - } - @Test void contentTest() { Text actual; Text expected; - actual = Text.builder().append(Text.of("123")).build(); + actual = Text.builder().append("123").build(); expected = new Text(null, null, "123", null); assertEquals(expected, actual); - actual = Text.builder().append(Text.of("123")).append(Text.of("456")).build(); + actual = Text.builder().append("123").append(Text.of("456")).build(); expected = new Text(null, null, "123", List.of(Text.of("456"))); assertEquals(expected, actual); }