Merge branch 'feature/text' into develop
This commit is contained in:
@@ -5,9 +5,11 @@ import lombok.Setter;
|
|||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Accessors(fluent = true)
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@ToString
|
@ToString
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
lombok.accessors.fluent=true
|
|
||||||
75
protocol/src/main/java/mc/protocol/model/text/Text.java
Normal file
75
protocol/src/main/java/mc/protocol/model/text/Text.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Text {
|
||||||
|
|
||||||
|
public static final Text EMPTY = of("");
|
||||||
|
|
||||||
|
private TextColor color;
|
||||||
|
private TextStyle style;
|
||||||
|
private String content;
|
||||||
|
private List<Text> children;
|
||||||
|
|
||||||
|
Text(TextColor color, TextStyle style, String content, List<Text> children) {
|
||||||
|
this.color = color;
|
||||||
|
this.style = style;
|
||||||
|
this.content = content;
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text of(String content) {
|
||||||
|
return new Text(null, null, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text of(TextColor color, String content) {
|
||||||
|
return new Text(color, null, content, 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 content) {
|
||||||
|
return new Text(color, style, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text.Builder builder() {
|
||||||
|
return new Text.Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private final LinkedList<Text> chain = new LinkedList<>();
|
||||||
|
|
||||||
|
public Builder append(Text text) {
|
||||||
|
if (text == null || EMPTY.equals(text)) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
chain.add(text);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
protocol/src/main/java/mc/protocol/model/text/TextColor.java
Normal file
31
protocol/src/main/java/mc/protocol/model/text/TextColor.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum TextColor {
|
||||||
|
//@formatter:off
|
||||||
|
BLACK ("black", '0'),
|
||||||
|
DARK_BLUE ("dark_blue", '1'),
|
||||||
|
DARK_GREEN ("dark_green", '2'),
|
||||||
|
DARK_AQUA ("dark_aqua", '3'),
|
||||||
|
DARK_RED ("dark_red", '4'),
|
||||||
|
DARK_PUEPLE("dark_purple", '5'),
|
||||||
|
GOLD ("gold", '6'),
|
||||||
|
GRAY ("gray", '7'),
|
||||||
|
DARK_GRAY ("dark_gray", '8'),
|
||||||
|
BLUE ("blue", '9'),
|
||||||
|
GREEN ("green", 'a'),
|
||||||
|
AQUA ("aqua", 'b'),
|
||||||
|
RED ("red", 'c'),
|
||||||
|
PUEPLE ("light_purple",'d'),
|
||||||
|
YELLOW ("yellow", 'e'),
|
||||||
|
WHITE ("white", 'f'),
|
||||||
|
RESET ("reset", 'r');
|
||||||
|
//@formatter:on
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final char code;
|
||||||
|
}
|
||||||
38
protocol/src/main/java/mc/protocol/model/text/TextStyle.java
Normal file
38
protocol/src/main/java/mc/protocol/model/text/TextStyle.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
@Builder(builderClassName = "Builder")
|
||||||
|
@Accessors(fluent = true)
|
||||||
|
@Getter
|
||||||
|
@Setter(AccessLevel.PACKAGE)
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
@SuppressWarnings("java:S1845")
|
||||||
|
public class TextStyle {
|
||||||
|
|
||||||
|
public static final TextStyle BOLD = new TextStyle(true, null, null, null, null);
|
||||||
|
public static final TextStyle ITALIC = new TextStyle(null, true, null, null, null);
|
||||||
|
public static final TextStyle UNDERLINE = new TextStyle(null, null, true, null, null);
|
||||||
|
public static final TextStyle STRIKETHOUGH = new TextStyle(null, null, null, true, null);
|
||||||
|
public static final TextStyle OBFUSCATED = new TextStyle(null, null, null, null, true);
|
||||||
|
|
||||||
|
public static final TextStyle RESET = new TextStyle(false, false, false, false, false);
|
||||||
|
public static final TextStyle NONE = new TextStyle(null, null, null, null, null);
|
||||||
|
|
||||||
|
private Boolean bold;
|
||||||
|
private Boolean italic;
|
||||||
|
private Boolean underline;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
protocol/src/test/java/mc/protocol/model/text/TextTest.java
Normal file
32
protocol/src/test/java/mc/protocol/model/text/TextTest.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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();
|
||||||
|
expected = new Text(null, null, "123", null);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
|
||||||
|
actual = Text.builder().append(Text.of("123")).append(Text.of("456")).build();
|
||||||
|
expected = new Text(null, null, "123", List.of(Text.of("456")));
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user