diff --git a/core/src/main/java/mc/core/text/Text.java b/core/src/main/java/mc/core/text/Text.java new file mode 100644 index 0000000..bfb470d --- /dev/null +++ b/core/src/main/java/mc/core/text/Text.java @@ -0,0 +1,91 @@ +/* + * DmitriyMX + * 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 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; + } + } +} diff --git a/core/src/main/java/mc/core/text/TextColor.java b/core/src/main/java/mc/core/text/TextColor.java new file mode 100644 index 0000000..98c3214 --- /dev/null +++ b/core/src/main/java/mc/core/text/TextColor.java @@ -0,0 +1,32 @@ +/* + * DmitriyMX + * 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; +} diff --git a/core/src/main/java/mc/core/text/TextStyle.java b/core/src/main/java/mc/core/text/TextStyle.java new file mode 100644 index 0000000..1a90e8c --- /dev/null +++ b/core/src/main/java/mc/core/text/TextStyle.java @@ -0,0 +1,64 @@ +/* + * DmitriyMX + * 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 TRUE = Optional.of(true); + private static final Optional FALSE = Optional.of(false); + private static final Optional NONE = Optional.empty(); + + static Optional of(boolean bool) { + return bool ? TRUE : FALSE; + } + + static Optional of(@Nullable Boolean bool) { + if (bool != null) { + return of(bool.booleanValue()); + } + return NONE; + } + } + + private Optional bold; + private Optional italic; + private Optional underline; + private Optional strikethrough; + private Optional 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; + } +}