Text: изменен дизайн
This commit is contained in:
@@ -1,79 +1,139 @@
|
|||||||
package mc.protocol.model.text;
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Accessors(fluent = true)
|
@Accessors(fluent = true)
|
||||||
@Getter
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode
|
@Data
|
||||||
@ToString
|
|
||||||
public class Text {
|
public class Text {
|
||||||
|
|
||||||
public static final Text EMPTY = of("");
|
|
||||||
|
|
||||||
private TextColor color;
|
private TextColor color;
|
||||||
private TextStyle style;
|
private TextStyle style;
|
||||||
private String content;
|
private String content;
|
||||||
private List<Text> children;
|
private List<Text> children;
|
||||||
|
|
||||||
Text(TextColor color, TextStyle style, String content, List<Text> children) {
|
public static Text of(String string) {
|
||||||
this.color = color;
|
return new Text(null, null, string, null);
|
||||||
this.style = style;
|
|
||||||
this.content = content;
|
|
||||||
this.children = children;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Text of(String content) {
|
public static Text of(TextColor color, String string) {
|
||||||
return new Text(null, null, content, null);
|
return new Text(color, null, string, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Text of(TextColor color, String content) {
|
public static Text of(TextStyle style, String string) {
|
||||||
return new Text(color, null, content, null);
|
return new Text(null, style, string, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Text of(TextStyle style, String content) {
|
public static Text of(TextColor color, TextStyle style, String string) {
|
||||||
return new Text(null, style, content, null);
|
return new Text(color, style, string, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Text of(TextColor color, TextStyle style, String content) {
|
public static Builder builder() {
|
||||||
return new Text(color, style, content, null);
|
return new Builder();
|
||||||
}
|
|
||||||
|
|
||||||
public static Text.Builder builder() {
|
|
||||||
return new Text.Builder();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private final LinkedList<Text> chain = new LinkedList<>();
|
|
||||||
|
|
||||||
public Builder append(Text text) {
|
private StringBuilder contentBuilder;
|
||||||
if (text == null || EMPTY.equals(text)) {
|
private TextStyle.Builder styleBuilder;
|
||||||
|
private TextColor color;
|
||||||
|
private List<Text> children;
|
||||||
|
|
||||||
|
public Builder append(String content) {
|
||||||
|
if (this.contentBuilder == null) {
|
||||||
|
this.contentBuilder = new StringBuilder(content);
|
||||||
|
} else {
|
||||||
|
this.contentBuilder.append(content);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
chain.add(text);
|
public Builder append(Text text) {
|
||||||
|
if (children == null) {
|
||||||
|
children = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Text build() {
|
public Text build() {
|
||||||
if (chain.isEmpty()) {
|
return new Text(
|
||||||
return EMPTY;
|
color,
|
||||||
}
|
styleBuilder == null ? null : styleBuilder.build(),
|
||||||
|
contentBuilder == null ? null : contentBuilder.toString(),
|
||||||
Text rootText = chain.pollFirst();
|
children);
|
||||||
|
|
||||||
if (!chain.isEmpty()) {
|
|
||||||
rootText.children = new ArrayList<>();
|
|
||||||
rootText.children.addAll(chain);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rootText;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,8 @@ import lombok.*;
|
|||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
@Builder(builderClassName = "Builder")
|
|
||||||
@Accessors(fluent = true)
|
@Accessors(fluent = true)
|
||||||
@Getter
|
@Data
|
||||||
@Setter(AccessLevel.PACKAGE)
|
|
||||||
@EqualsAndHashCode
|
|
||||||
@ToString
|
|
||||||
@SuppressWarnings("java:S1845")
|
@SuppressWarnings("java:S1845")
|
||||||
public class TextStyle {
|
public class TextStyle {
|
||||||
|
|
||||||
@@ -28,11 +24,59 @@ public class TextStyle {
|
|||||||
private Boolean strikethrough;
|
private Boolean strikethrough;
|
||||||
private Boolean obfuscated;
|
private Boolean obfuscated;
|
||||||
|
|
||||||
void merge(TextStyle style) {
|
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.bold != null) this.bold = style.bold;
|
||||||
if (style.italic != null) this.italic = style.italic;
|
if (style.italic != null) this.italic = style.italic;
|
||||||
if (style.underline != null) this.underline = style.underline;
|
if (style.underline != null) this.underline = style.underline;
|
||||||
if (style.strikethrough != null) this.strikethrough = style.strikethrough;
|
if (style.strikethrough != null) this.strikethrough = style.strikethrough;
|
||||||
if (style.obfuscated != null) this.obfuscated = style.obfuscated;
|
if (style.obfuscated != null) this.obfuscated = style.obfuscated;
|
||||||
|
//@formatter:on
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextStyle build() {
|
||||||
|
return new TextStyle(bold, italic, underline, strikethrough, obfuscated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,24 +8,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
class TextTest {
|
class TextTest {
|
||||||
|
|
||||||
@Test
|
|
||||||
void emptyTest() {
|
|
||||||
Text actual = Text.builder().build();
|
|
||||||
Text expected = Text.EMPTY;
|
|
||||||
|
|
||||||
assertEquals(expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contentTest() {
|
void contentTest() {
|
||||||
Text actual;
|
Text actual;
|
||||||
Text expected;
|
Text expected;
|
||||||
|
|
||||||
actual = Text.builder().append(Text.of("123")).build();
|
actual = Text.builder().append("123").build();
|
||||||
expected = new Text(null, null, "123", null);
|
expected = new Text(null, null, "123", null);
|
||||||
assertEquals(expected, actual);
|
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")));
|
expected = new Text(null, null, "123", List.of(Text.of("456")));
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user