diff --git a/core/src/main/java/mc/core/text/Text.java b/core/src/main/java/mc/core/text/Text.java index 2c3b4ec..57a44fb 100644 --- a/core/src/main/java/mc/core/text/Text.java +++ b/core/src/main/java/mc/core/text/Text.java @@ -90,7 +90,7 @@ public class Text { } } else if (obj instanceof TextStyle) { if (this.style == null) { - this.style = new TextStyle(null,null,null,null,null); + this.style = TextStyle.none(); } else { this.style.merge((TextStyle) obj); } @@ -113,7 +113,7 @@ public class Text { public Builder style(TextStyle style) { if (this.style == null) { - this.style = new TextStyle(null,null,null,null,null);; + this.style = TextStyle.none(); } else { this.style.merge(style); } @@ -123,7 +123,7 @@ public class Text { public Builder style(TextStyle... styles) { if (this.style == null) { - this.style = new TextStyle(null, null, null, null, null); + this.style = TextStyle.none(); } for(TextStyle style : styles) { diff --git a/core/src/main/java/mc/core/text/TextStyle.java b/core/src/main/java/mc/core/text/TextStyle.java index 24b8552..f0afe98 100644 --- a/core/src/main/java/mc/core/text/TextStyle.java +++ b/core/src/main/java/mc/core/text/TextStyle.java @@ -62,4 +62,8 @@ public class TextStyle { if (style.strikethrough.isPresent()) this.strikethrough = style.strikethrough; if (style.obfuscated.isPresent()) this.obfuscated = style.obfuscated; } + + public static TextStyle none() { + return new TextStyle(null,null,null,null, null); + } } diff --git a/core/src/main/java/mc/core/text/TextTemplate.java b/core/src/main/java/mc/core/text/TextTemplate.java new file mode 100644 index 0000000..1e9600f --- /dev/null +++ b/core/src/main/java/mc/core/text/TextTemplate.java @@ -0,0 +1,150 @@ +/* + * DmitriyMX + * 2018-06-13 + */ +package mc.core.text; + +import com.google.common.collect.ImmutableList; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +import java.util.*; + +public class TextTemplate { + private final ImmutableList elements; + + private TextTemplate(ImmutableList elements) { + this.elements = elements; + } + + public Text apply(Object... objects) { + Map variableMap = new HashMap<>((objects.length % 2) == 1 ? (objects.length / 2) + 1 : (objects.length / 2)); + + boolean skipValue = false; + String key = null; + for (Object obj : objects) { + if (skipValue) { + skipValue = false; + continue; + } + + if (key == null) { + if (obj == null || obj.toString().trim().isEmpty()) { + skipValue = true; + continue; + } + + key = obj.toString().trim(); + } else { + variableMap.put(key, obj); + key = null; + } + } + + if (key != null) { + variableMap.put(key, ""); + } + + return apply(variableMap); + } + + public Text apply(Map variables) { + Text.Builder textBuilder = Text.builder(); + + for(Object obj : elements) { + if (obj instanceof Text) { + textBuilder.append((Text) obj); + } else if (obj instanceof Arg) { + Arg arg = (Arg) obj; + if (variables.containsKey(arg.getKey())) { + Object valueObj = variables.get(arg.getKey()); + + if (valueObj instanceof Text) { + textBuilder.append((Text) valueObj); + } else { + textBuilder.append(Text.of(valueObj, arg.getColor(), arg.getStyle())); + } + } else { + textBuilder.append(Text.of(arg.getDefaultValue(), arg.getColor(), arg.getStyle())); + } + } + } + + return textBuilder.build(); + } + + @RequiredArgsConstructor + @Getter + public static class Arg { + private final String key; + private final String defaultValue; + @Setter + private TextColor color; + @Setter + private TextStyle style; + } + + public static class Builder { + private List elements = new ArrayList<>(); + + public Builder append(Text element) { + this.elements.add(element); + return this; + } + + public Builder append(Text... elements) { + Collections.addAll(this.elements, elements); + return this; + } + + public Builder arg(String name) { + this.elements.add(new Arg(name, null)); + return this; + } + + public Builder arg(String name, String defaultValue) { + this.elements.add(new Arg(name, defaultValue)); + return this; + } + + public Builder arg(Object... objects) { + String key = null, + defaultValue = null; + TextColor color = null; + TextStyle style = null; + + for(Object obj : objects) { + if (obj instanceof String) { + if (key == null) { + key = (String) obj; + } else { + defaultValue = (String) obj; + } + } else if (obj instanceof TextColor) { + color = (TextColor) obj; + } else if (obj instanceof TextStyle) { + if (style == null) { + style = TextStyle.none(); + } + style.merge((TextStyle) obj); + } + } + + Arg arg = new Arg(key, defaultValue); + arg.setColor(color); + arg.setStyle(style); + this.elements.add(arg); + + return this; + } + + public TextTemplate build() { + return new TextTemplate(ImmutableList.copyOf(elements)); + } + } + + public static Builder builder() { + return new Builder(); + } +}