Text component
This commit is contained in:
91
core/src/main/java/mc/core/text/Text.java
Normal file
91
core/src/main/java/mc/core/text/Text.java
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-06-11
|
||||||
|
*/
|
||||||
|
package mc.core.text;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Text {
|
||||||
|
private String string;
|
||||||
|
private TextColor color;
|
||||||
|
private TextStyle style;
|
||||||
|
private List<Text> childs;
|
||||||
|
|
||||||
|
private Text() { }
|
||||||
|
|
||||||
|
public Builder toBuilder() {
|
||||||
|
return new Builder(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text of(String string) {
|
||||||
|
return of(string, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text of(String string, TextColor color) {
|
||||||
|
return of(string, color, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text of(String string, TextColor color, TextStyle style) {
|
||||||
|
Text text = new Text();
|
||||||
|
text.string = string;
|
||||||
|
text.color = color;
|
||||||
|
text.style = style;
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder(String string) {
|
||||||
|
return new Builder(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private Text currentText;
|
||||||
|
|
||||||
|
private Builder() {
|
||||||
|
this.currentText = new Text();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Builder(String string) {
|
||||||
|
this.currentText = new Text();
|
||||||
|
this.currentText.string = string;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Builder(Text text) {
|
||||||
|
this.currentText = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder color(TextColor color) {
|
||||||
|
this.currentText.color = color;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder style(TextStyle style) {
|
||||||
|
if (this.currentText.style == null) {
|
||||||
|
this.currentText.style = new TextStyle(null,null,null,null,null);
|
||||||
|
}
|
||||||
|
this.currentText.style.concat(style);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder append(Text text) {
|
||||||
|
if (this.currentText.childs == null) {
|
||||||
|
this.currentText.childs = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.currentText.childs.add(text);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Text build() {
|
||||||
|
return this.currentText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
core/src/main/java/mc/core/text/TextColor.java
Normal file
32
core/src/main/java/mc/core/text/TextColor.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-06-11
|
||||||
|
*/
|
||||||
|
package mc.core.text;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum TextColor {
|
||||||
|
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');
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final char code;
|
||||||
|
}
|
||||||
64
core/src/main/java/mc/core/text/TextStyle.java
Normal file
64
core/src/main/java/mc/core/text/TextStyle.java
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-06-11
|
||||||
|
*/
|
||||||
|
package mc.core.text;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
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);
|
||||||
|
|
||||||
|
private static class OptionalBoolean {
|
||||||
|
private static final Optional<Boolean> TRUE = Optional.of(true);
|
||||||
|
private static final Optional<Boolean> FALSE = Optional.of(false);
|
||||||
|
private static final Optional<Boolean> NONE = Optional.empty();
|
||||||
|
|
||||||
|
static Optional<Boolean> of(boolean bool) {
|
||||||
|
return bool ? TRUE : FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Optional<Boolean> of(@Nullable Boolean bool) {
|
||||||
|
if (bool != null) {
|
||||||
|
return of(bool.booleanValue());
|
||||||
|
}
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Boolean> bold;
|
||||||
|
private Optional<Boolean> italic;
|
||||||
|
private Optional<Boolean> underline;
|
||||||
|
private Optional<Boolean> strikethrough;
|
||||||
|
private Optional<Boolean> obfuscated;
|
||||||
|
|
||||||
|
public TextStyle(@Nullable Boolean bold,
|
||||||
|
@Nullable Boolean italic,
|
||||||
|
@Nullable Boolean underline,
|
||||||
|
@Nullable Boolean strikethrough,
|
||||||
|
@Nullable Boolean obfuscated) {
|
||||||
|
this.bold = OptionalBoolean.of(bold);
|
||||||
|
this.italic = OptionalBoolean.of(italic);
|
||||||
|
this.underline = OptionalBoolean.of(underline);
|
||||||
|
this.strikethrough = OptionalBoolean.of(strikethrough);
|
||||||
|
this.obfuscated = OptionalBoolean.of(obfuscated);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void concat(TextStyle style) {
|
||||||
|
if (style.bold.isPresent()) this.bold = style.bold;
|
||||||
|
if (style.italic.isPresent()) this.italic = style.italic;
|
||||||
|
if (style.underline.isPresent()) this.underline = style.underline;
|
||||||
|
if (style.strikethrough.isPresent()) this.strikethrough = style.strikethrough;
|
||||||
|
if (style.obfuscated.isPresent()) this.obfuscated = style.obfuscated;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user