TextTemplate
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
150
core/src/main/java/mc/core/text/TextTemplate.java
Normal file
150
core/src/main/java/mc/core/text/TextTemplate.java
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 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<Object> elements;
|
||||
|
||||
private TextTemplate(ImmutableList<Object> elements) {
|
||||
this.elements = elements;
|
||||
}
|
||||
|
||||
public Text apply(Object... objects) {
|
||||
Map<String, Object> 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<String, Object> 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<Object> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user