diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 197c876..3b84186 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -2,8 +2,8 @@ 4.0.0 - parent org.zhdev.varioutil + parent 1.0-SNAPSHOT @@ -31,6 +31,10 @@ com.mysql mysql-connector-j + + com.googlecode.json-simple + json-simple + diff --git a/misc/pom.xml b/misc/pom.xml index fe5c904..0328263 100644 --- a/misc/pom.xml +++ b/misc/pom.xml @@ -10,6 +10,18 @@ misc + + com.googlecode.json-simple + json-simple + 1.1.1 + compile + + + junit + junit + + + ${project.groupId} io diff --git a/misc/src/main/java/org/zhdev/varioutil/color/ChatColor.java b/misc/src/main/java/org/zhdev/varioutil/color/ChatColor.java new file mode 100644 index 0000000..892885d --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/color/ChatColor.java @@ -0,0 +1,49 @@ +package org.zhdev.varioutil.color; + +public interface ChatColor { + char CHAR = 'ยง'; + + NamedChatColor BLACK = new NamedChatColor(0x000000, "black", '0'); + NamedChatColor DARK_BLUE = new NamedChatColor(0x0000aa, "dark_blue", '1'); + NamedChatColor DARK_GREEN = new NamedChatColor(0x00aa00, "dark_green", '2'); + NamedChatColor DARK_AQUA = new NamedChatColor(0x00aaaa, "dark_aqua", '3'); + NamedChatColor DARK_RED = new NamedChatColor(0xaa0000, "dark_red", '4'); + NamedChatColor DARK_PURPLE = new NamedChatColor(0xaa00aa, "dark_purple", '5'); + NamedChatColor GOLD = new NamedChatColor(0xffaa00, "gold", '6'); + NamedChatColor GRAY = new NamedChatColor(0xaaaaaa, "gray", '7'); + NamedChatColor DARK_GRAY = new NamedChatColor(0x555555, "dark_gray", '8'); + NamedChatColor BLUE = new NamedChatColor(0x5555ff, "blue", '9'); + NamedChatColor GREEN = new NamedChatColor(0x55ff55, "green", 'a'); + NamedChatColor AQUA = new NamedChatColor(0x55ffff, "aqua", 'b'); + NamedChatColor RED = new NamedChatColor(0xff5555, "red", 'c'); + NamedChatColor LIGHT_PURPLE = new NamedChatColor(0xff55ff, "light_purple", 'd'); + NamedChatColor YELLOW = new NamedChatColor(0xffff55, "yellow", 'e'); + NamedChatColor WHITE = new NamedChatColor(0xffffff, "white", 'f'); + + NamedChatColor OBFUSCATED = new NamedChatColor("obfuscated", 'k'); + NamedChatColor BOLD = new NamedChatColor("bold", 'l'); + NamedChatColor STRIKETHROUGH = new NamedChatColor("strikethrough", 'm'); + NamedChatColor UNDERLINED = new NamedChatColor("underlined", 'n'); + NamedChatColor ITALIC = new NamedChatColor("italic", 'o'); + NamedChatColor RESET = new NamedChatColor("reset", 'r'); + + int getValue(); + + String getName(); + + static ChatColor valueOf(int value) { + return ChatColorCache.BY_VALUE.computeIfAbsent(value, HexChatColor::new); + } + + static ChatColor valueOf(char code) { + return ChatColorCache.BY_CODE.get(code); + } + + static ChatColor valueOf(String name) { + return ChatColorCache.BY_NAME.get(name); + } + + static ChatColor[] values() { + return ChatColorCache.BY_NAME.values().toArray(new ChatColor[0]); + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/color/ChatColorCache.java b/misc/src/main/java/org/zhdev/varioutil/color/ChatColorCache.java new file mode 100644 index 0000000..1aa0be6 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/color/ChatColorCache.java @@ -0,0 +1,11 @@ +package org.zhdev.varioutil.color; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +final class ChatColorCache { + static final Map BY_VALUE = new ConcurrentHashMap<>(); + static final Map BY_CODE = new LinkedHashMap<>(21); + static final Map BY_NAME = new LinkedHashMap<>(21); +} diff --git a/misc/src/main/java/org/zhdev/varioutil/color/HexChatColor.java b/misc/src/main/java/org/zhdev/varioutil/color/HexChatColor.java new file mode 100644 index 0000000..df84c31 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/color/HexChatColor.java @@ -0,0 +1,48 @@ +package org.zhdev.varioutil.color; + +public class HexChatColor implements ChatColor { + final int value; + final String name; + + final String string; + + HexChatColor(int value, String name, String string) { + this.value = value; + this.name = name; + this.string = string; + } + + private HexChatColor(int value, String name) { + this(value, name, hexToCode(name)); + } + + HexChatColor(int value) { + this(value, "#" + Integer.toHexString(value)); + } + + public int getValue() { + return value; + } + + @Override + public String getName() { + return name; + } + + @Override + public String toString() { + return string; + } + + public static String hexToCode(String hexValue, char colorChar) { + StringBuilder builder = new StringBuilder().append(colorChar).append('x'); + for (int i = 0; i < hexValue.length(); i++) { + builder.append(colorChar).append(hexValue.charAt(i)); + } + return builder.toString(); + } + + public static String hexToCode(String hexValue) { + return hexToCode(hexValue, CHAR); + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/color/NamedChatColor.java b/misc/src/main/java/org/zhdev/varioutil/color/NamedChatColor.java new file mode 100644 index 0000000..8f650c5 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/color/NamedChatColor.java @@ -0,0 +1,22 @@ +package org.zhdev.varioutil.color; + +public final class NamedChatColor extends HexChatColor { + private final char code; + + NamedChatColor(int value, String name, char code) { + super(value, name, String.valueOf(CHAR) + code); + this.code = code; + + if (value != -1) ChatColorCache.BY_VALUE.put(value, this); + ChatColorCache.BY_CODE.put(code, this); + ChatColorCache.BY_NAME.put(name, this); + } + + NamedChatColor(String name, char code) { + this(-1, name, code); + } + + public char getCode() { + return code; + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/component/ClickEvent.java b/misc/src/main/java/org/zhdev/varioutil/component/ClickEvent.java new file mode 100755 index 0000000..3913d21 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/ClickEvent.java @@ -0,0 +1,20 @@ +package org.zhdev.varioutil.component; + +public enum ClickEvent { + OPEN_URL("open_url"), + RUN_COMMAND("run_command"), + SUGGEST_COMMAND("suggest_command"), + CHANGE_PAGE("change_page"), + COPY_TO_CLIPBOARD("copy_to_clipboard"); + + private final String value; + + ClickEvent(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/component/Component.java b/misc/src/main/java/org/zhdev/varioutil/component/Component.java new file mode 100755 index 0000000..b5f3a63 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/Component.java @@ -0,0 +1,400 @@ +package org.zhdev.varioutil.component; + +import org.zhdev.varioutil.color.ChatColor; + +import java.util.*; + +public final class Component { + public static final Component EMPTY = new Component(ComponentType.TEXT, ""); + + private final ComponentType type; + + private final String value; + + private final Boolean bold; + private final Boolean italic; + private final Boolean underlined; + private final Boolean strikethrough; + private final Boolean obfuscated; + + private final MinecraftFont font; + private final ChatColor color; + + private final String insertion; + + private final ClickEvent clickEvent; + private final String clickEventValue; + + private final HoverEvent hoverEvent; + private final Component[] hoverEventValue; + + private final Component[] extra; + private final Component[] with; + + public Component(ComponentType type, String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, MinecraftFont font, ChatColor color, String insertion, ClickEvent clickEvent, String clickEventValue, HoverEvent hoverEvent, Component[] hoverEventValue, Component[] extra, Component[] with) { + this.type = type; + this.value = value; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.strikethrough = strikethrough; + this.obfuscated = obfuscated; + this.font = font; + this.color = color; + this.insertion = insertion; + this.clickEvent = clickEvent; + this.clickEventValue = clickEventValue; + this.hoverEvent = hoverEvent; + this.hoverEventValue = hoverEventValue; + this.extra = extra; + this.with = with; + } + + public Component(String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, ChatColor color, ClickEvent clickEvent, String clickEventValue) { + this(ComponentType.TEXT, value, bold, italic, underlined, strikethrough, obfuscated, null, color, null, clickEvent, clickEventValue, null, null, null, null); + } + + public Component(String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, ChatColor color) { + this(value, bold, italic, underlined, strikethrough, obfuscated, color, null, null); + } + + public Component(ComponentType type, String value) { + this(type, value, null, null, null, null, null, null, null, null, null, null, null, null, null, null); + } + + public ComponentType getType() { + return type; + } + + public String getValue() { + return value; + } + + public Boolean getBold() { + return bold; + } + + public Boolean getItalic() { + return italic; + } + + public Boolean getUnderlined() { + return underlined; + } + + public Boolean getStrikethrough() { + return strikethrough; + } + + public Boolean getObfuscated() { + return obfuscated; + } + + public MinecraftFont getFont() { + return font; + } + + public ChatColor getColor() { + return color; + } + + public String getInsertion() { + return insertion; + } + + public ClickEvent getClickEvent() { + return clickEvent; + } + + public String getClickEventValue() { + return clickEventValue; + } + + public HoverEvent getHoverEvent() { + return hoverEvent; + } + + public Component[] getHoverEventValue() { + return hoverEventValue; + } + + public Component[] getExtra() { + return extra; + } + + public Component[] getWith() { + return with; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Component component = (Component) o; + + if (type != component.type) return false; + if (!Objects.equals(value, component.value)) return false; + if (!Objects.equals(bold, component.bold)) return false; + if (!Objects.equals(italic, component.italic)) return false; + if (!Objects.equals(underlined, component.underlined)) return false; + if (!Objects.equals(strikethrough, component.strikethrough)) + return false; + if (!Objects.equals(obfuscated, component.obfuscated)) return false; + if (font != component.font) return false; + if (!Objects.equals(color, component.color)) return false; + if (!Objects.equals(insertion, component.insertion)) return false; + if (clickEvent != component.clickEvent) return false; + if (!Objects.equals(clickEventValue, component.clickEventValue)) + return false; + if (hoverEvent != component.hoverEvent) return false; + if (!Arrays.equals(hoverEventValue, component.hoverEventValue)) return false; + if (!Arrays.equals(extra, component.extra)) return false; + return Arrays.equals(with, component.with); + } + + @Override + public int hashCode() { + int result = type != null ? type.hashCode() : 0; + result = 31 * result + (value != null ? value.hashCode() : 0); + result = 31 * result + (bold != null ? bold.hashCode() : 0); + result = 31 * result + (italic != null ? italic.hashCode() : 0); + result = 31 * result + (underlined != null ? underlined.hashCode() : 0); + result = 31 * result + (strikethrough != null ? strikethrough.hashCode() : 0); + result = 31 * result + (obfuscated != null ? obfuscated.hashCode() : 0); + result = 31 * result + (font != null ? font.hashCode() : 0); + result = 31 * result + (color != null ? color.hashCode() : 0); + result = 31 * result + (insertion != null ? insertion.hashCode() : 0); + result = 31 * result + (clickEvent != null ? clickEvent.hashCode() : 0); + result = 31 * result + (clickEventValue != null ? clickEventValue.hashCode() : 0); + result = 31 * result + (hoverEvent != null ? hoverEvent.hashCode() : 0); + result = 31 * result + Arrays.hashCode(hoverEventValue); + result = 31 * result + Arrays.hashCode(extra); + result = 31 * result + Arrays.hashCode(with); + return result; + } + + @Override + public String toString() { + return "Component{" + "type=" + type + ", value='" + value + "', bold=" + bold + ", italic=" + italic + ", underlined=" + underlined + ", strikethrough=" + strikethrough + ", obfuscated=" + obfuscated + ", font=" + font + ", color=" + color + ", insertion='" + insertion + "', clickEvent=" + clickEvent + ", clickEventValue='" + clickEventValue + "', hoverEvent=" + hoverEvent + ", hoverEventValue=" + Arrays.toString(hoverEventValue) + ", extra=" + Arrays.toString(extra) + ", with=" + Arrays.toString(with) + '}'; + } + + public static Builder of(String value) { + return new Builder(value); + } + + public static Builder textOf(String value) { + return new Builder(value).type(ComponentType.TEXT); + } + + public static Builder translateOf(String value) { + return of(value).type(ComponentType.TRANSLATE); + } + + public static Builder keybindOf(String value) { + return of(value).type(ComponentType.KEYBIND); + } + + public static Builder scoreOf(String value) { + return of(value).type(ComponentType.SCORE); + } + + public static Builder selectorOf(String value) { + return of(value).type(ComponentType.SELECTOR); + } + + public static final class Builder { + private final List components; + + private ComponentType type = ComponentType.TEXT; + private String value; + private Boolean bold; + private Boolean italic; + private Boolean underlined; + private Boolean strikethrough; + private Boolean obfuscated; + private MinecraftFont font; + private ChatColor color; + private String insertion; + private ClickEvent clickEvent; + private String clickEventValue; + private HoverEvent hoverEvent; + private Component[] hoverEventValue; + private Component[] extra; + private Component[] with; + + public Builder(String value, int initialCapacity) { + this.components = new ArrayList<>(initialCapacity); + this.value = value; + } + + public Builder(String value) { + this.components = new ArrayList<>(); + this.value = value; + } + + public Builder type(ComponentType type) { + this.type = type; + return this; + } + + public Builder value(String value) { + this.value = value; + return this; + } + + public Builder text(String value) { + this.type = ComponentType.TEXT; + this.value = value; + return this; + } + + public Builder translate(String value) { + this.type = ComponentType.TRANSLATE; + this.value = value; + return this; + } + + public Builder keybind(String value) { + this.type = ComponentType.KEYBIND; + this.value = value; + return this; + } + + public Builder score(String value) { + this.type = ComponentType.SCORE; + this.value = value; + return this; + } + + public Builder selector(String value) { + this.type = ComponentType.SELECTOR; + this.value = value; + return this; + } + + public Builder bold(Boolean bold) { + this.bold = bold; + return this; + } + + public Builder italic(Boolean italic) { + this.italic = italic; + return this; + } + + public Builder underlined(Boolean underlined) { + this.underlined = underlined; + return this; + } + + public Builder strikethrough(Boolean strikethrough) { + this.strikethrough = strikethrough; + return this; + } + + public Builder obfuscated(Boolean obfuscated) { + this.obfuscated = obfuscated; + return this; + } + + public Builder font(MinecraftFont font) { + this.font = font; + return this; + } + + public Builder color(ChatColor color) { + this.color = color; + return this; + } + + public Builder insertion(String insertion) { + this.insertion = insertion; + return this; + } + + public Builder click(ClickEvent clickEvent, String clickEventValue) { + this.clickEvent = clickEvent; + this.clickEventValue = clickEventValue; + return this; + } + + public Builder click(String clickEventValue) { + return click(ClickEvent.OPEN_URL, clickEventValue); + } + + public Builder hover(HoverEvent hoverEvent, Component... hoverEventValue) { + this.hoverEvent = hoverEvent; + this.hoverEventValue = hoverEventValue; + return this; + } + + public Builder hover(Component... hoverEventValue) { + return hover(HoverEvent.SHOW_TEXT, hoverEventValue); + } + + public Builder extra(Component... extra) { + this.extra = extra; + return this; + } + + public Builder with(Component... with) { + this.with = with; + return this; + } + + public Component create() { + return new Component(type, value, bold, italic, underlined, strikethrough, obfuscated, font, color, insertion, clickEvent, clickEventValue, hoverEvent, hoverEventValue, extra, with); + } + + public Builder push() { + Component component = create(); + components.add(component); + return this; + } + + public void add(Component component) { + this.components.add(component); + } + + public void addAll(Collection components) { + this.components.addAll(components); + } + + public void addAll(Component... components) { + Collections.addAll(this.components, components); + } + + public Builder append(String value) { + return push().value(value); + } + + public Builder appendText(String value) { + return push().type(ComponentType.TEXT).value(value); + } + + public Builder appendTranslate(String value) { + return push().type(ComponentType.TRANSLATE).value(value); + } + + public Builder appendKeybind(String value) { + return push().type(ComponentType.KEYBIND).value(value); + } + + public Builder appendScore(String value) { + return push().type(ComponentType.SCORE).value(value); + } + + public Builder appendSelector(String value) { + return push().type(ComponentType.SELECTOR).value(value); + } + + public Component[] result() { + return components.toArray(new Component[0]); + } + + public Component[] build() { + return push().result(); + } + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/component/ComponentSerializer.java b/misc/src/main/java/org/zhdev/varioutil/component/ComponentSerializer.java new file mode 100644 index 0000000..5d9075c --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/ComponentSerializer.java @@ -0,0 +1,385 @@ +package org.zhdev.varioutil.component; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.zhdev.varioutil.color.ChatColor; +import org.zhdev.varioutil.color.NamedChatColor; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ComponentSerializer { + private static final Pattern URL_PATTERN = Pattern.compile("^(https?://)?([-\\w_.]{2,}\\.[a-z]{2,63})(/\\S*)?$"); + + private static String readAsString(JSONObject object, String key) { + Object element = object.get(key); + return element instanceof String ? (String) element : null; + } + + private static Boolean readAsBoolean(JSONObject object, String key) { + Object element = object.get(key); + return element instanceof Boolean ? (Boolean) element : null; + } + + private static > T readAsEnum(JSONObject object, Class enumClass) { + Object element = object.get("action"); + if (!(element instanceof String)) { + return null; + } + + try { + return Enum.valueOf(enumClass, ((String) element).toUpperCase()); + } catch (IllegalArgumentException e) { + return null; + } + } + + private static ChatColor readAsColor(JSONObject object) { + Object element = object.get("color"); + if (!(element instanceof String)) { + return null; + } + + String value = (String) element; + try { + return value.startsWith("#") ? ChatColor.valueOf(Integer.decode(value)) : ChatColor.valueOf(value.toLowerCase()); + } catch (IllegalArgumentException e) { + return null; + } + } + + private static JSONObject readAsObject(JSONObject object, String key) { + Object element = object.get(key); + return element instanceof JSONObject ? (JSONObject) element : null; + } + + private static void writeRawProperty(StringBuilder builder, String key, String value) { + builder.append(',').append('"').append(JSONValue.escape(key)).append('"').append(':').append(value); + } + + private static void writeRawPropertyWithoutComma(StringBuilder builder, String key, String value) { + builder.append('"').append(JSONValue.escape(key)).append('"').append(':').append(value); + } + + private static void writeProperty(StringBuilder builder, String key, Object value) { + if (value != null) writeRawProperty(builder, key, JSONValue.toJSONString(value)); + } + + private static void writePropertyWithoutComma(StringBuilder builder, String key, Object value) { + if (value != null) writeRawPropertyWithoutComma(builder, key, JSONValue.toJSONString(value)); + } + + private static void writeProperty(StringBuilder builder, ChatColor value) { + if (value != null) writeRawProperty(builder, "color", JSONValue.toJSONString(value.getName())); + } + + private static Component deserialize0(JSONObject object) { + ComponentType type; + + Object element; + if ((element = object.get("text")) != null) { + type = ComponentType.TEXT; + } else if ((element = object.get("translate")) != null) { + type = ComponentType.TRANSLATE; + } else if ((element = object.get("keybind")) != null) { + type = ComponentType.KEYBIND; + } else if ((element = object.get("score")) != null) { + type = ComponentType.SCORE; + } else if ((element = object.get("selector")) != null) { + type = ComponentType.SELECTOR; + } else { + return Component.EMPTY; + } + + String value = element.toString(); + Boolean bold = readAsBoolean(object, "bold"); + Boolean italic = readAsBoolean(object, "italic"); + Boolean underlined = readAsBoolean(object, "underlined"); + Boolean strikethrough = readAsBoolean(object, "strikethrough"); + Boolean obfuscated = readAsBoolean(object, "obfuscated"); + MinecraftFont font = MinecraftFont.byNamespace(readAsString(object, "font")); + ChatColor color = readAsColor(object); + String insertion = readAsString(object, "insertion"); + + ClickEvent clickEvent; + String clickEventValue; + + JSONObject clickEventObject = readAsObject(object, "clickEvent"); + if (clickEventObject != null) { + clickEvent = readAsEnum(clickEventObject, ClickEvent.class); + clickEventValue = readAsString(clickEventObject, "value"); + } else { + clickEvent = null; + clickEventValue = null; + } + + HoverEvent hoverEvent; + Component[] hoverEventValue; + + JSONObject hoverEventObject = readAsObject(object, "hoverEvent"); + if (hoverEventObject != null) { + hoverEvent = readAsEnum(hoverEventObject, HoverEvent.class); + hoverEventValue = deserialize(readAsObject(hoverEventObject, "value")); + } else { + hoverEvent = null; + hoverEventValue = null; + } + + Component[] extra = null; + + Object extraElement = object.get("extra"); + if (extraElement != null) { + extra = deserialize(extraElement); + } + + Component[] with = null; + + Object withElement = object.get("with"); + if (withElement != null) { + with = deserialize(withElement); + } + + return new Component(type, value, bold, italic, underlined, strikethrough, obfuscated, font, color, insertion, + clickEvent, clickEventValue, hoverEvent, hoverEventValue, extra, with); + } + + public static Component[] deserialize(Object json) { + Component[] components; + if (json instanceof JSONArray) { + JSONArray array = (JSONArray) json; + components = new Component[array.size()]; + for (int i = 0; i < components.length; i++) { + Object e = array.get(i); + if (!(e instanceof JSONObject)) { + continue; + } + components[i] = deserialize0((JSONObject) e); + } + } else if (json instanceof JSONObject) { + components = new Component[] {deserialize0((JSONObject) json)}; + } else { + components = new Component[] {Component.EMPTY}; + } + + return components; + } + + public static Component[] deserialize(String json) { + return deserialize(JSONValue.parse(json)); + } + + public static Component[] deserializeLegacy(String value, char colorChar, ChatColor resetColor) { + List components = new ArrayList<>(); + + StringBuilder valueBuilder = new StringBuilder(); + int lastIndex = value.length() - 1; + Matcher urlMatcher = URL_PATTERN.matcher(value); + + ChatColor color = null; + Boolean bold = null; + Boolean italic = null; + Boolean underlined = null; + Boolean strikethrough = null; + Boolean obfuscated = null; + + for (int i = 0; i < value.length(); i++) { + char currentChar = value.charAt(i); + if (currentChar == colorChar) { + int j = i + 1; + if (j > lastIndex) { + valueBuilder.append(colorChar); + break; + } + + ChatColor nextColor; + char nextChar = Character.toLowerCase(value.charAt(j)); + if (nextChar == 'x') { + StringBuilder hexValueBuilder = new StringBuilder(7).append('#'); + j++; + int n = j + 12; + if (n > lastIndex) { + valueBuilder.append(colorChar); + continue; + } + for (; j < n; j++) { + nextChar = value.charAt(j); + if (nextChar == colorChar) { + continue; + } + hexValueBuilder.append(nextChar); + } + + if (hexValueBuilder.length() < 7) { + valueBuilder.append(colorChar); + continue; + } + + try { + nextColor = ChatColor.valueOf(Integer.decode(hexValueBuilder.toString())); + if (nextColor == null) { + valueBuilder.append(colorChar); + continue; + } + } catch (NumberFormatException e) { + valueBuilder.append(colorChar); + continue; + } + i = j - 1; + } else { + nextColor = ChatColor.valueOf(nextChar); + if (nextColor == null) { + valueBuilder.append(colorChar); + continue; + } + i++; + } + + if (valueBuilder.length() > 0) { + components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough, + obfuscated, color)); + valueBuilder.setLength(0); + } + + if (nextColor instanceof NamedChatColor) { + if (nextColor == NamedChatColor.BOLD) { + bold = true; + continue; + } else if (nextColor == NamedChatColor.ITALIC) { + italic = true; + continue; + } else if (nextColor == NamedChatColor.UNDERLINED) { + underlined = true; + continue; + } else if (nextColor == NamedChatColor.STRIKETHROUGH) { + strikethrough = true; + continue; + } else if (nextColor == NamedChatColor.OBFUSCATED) { + obfuscated = true; + continue; + } else if (nextColor == NamedChatColor.RESET) { + nextColor = resetColor; + } + } + + bold = null; + italic = null; + underlined = null; + strikethrough = null; + obfuscated = null; + color = nextColor; + continue; + } + + int nextIndex = value.indexOf(' ', i); + if (nextIndex == -1) { + nextIndex = value.length(); + } + + if (urlMatcher.region(i, nextIndex).find()) { + if (valueBuilder.length() > 0) { + components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough, + obfuscated, color)); + valueBuilder.setLength(0); + } + + String proto = urlMatcher.group(1); + + StringBuilder urlBuilder = new StringBuilder(); + if (proto == null || proto.length() == 0) { + urlBuilder.append("http://"); + } else { + urlBuilder.append(proto); + } + + String domain = urlMatcher.group(2); + urlBuilder.append(domain); + + String uri = urlMatcher.group(3); + if (uri != null && uri.length() > 0) { + urlBuilder.append(uri); + } + + String url = urlBuilder.toString(); + components.add(new Component(urlMatcher.group(), bold, italic, underlined, strikethrough, obfuscated, + color, ClickEvent.OPEN_URL, url)); + + i += nextIndex - i - 1; + continue; + } + + valueBuilder.append(currentChar); + } + + components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough, obfuscated, color)); + + return components.toArray(new Component[0]); + } + + public static Component[] deserializeLegacy(String value, char colorChar) { + return deserializeLegacy(value, colorChar, null); + } + + public static Component[] deserializeLegacy(String value) { + return deserializeLegacy(value, ChatColor.CHAR); + } + + public static String serialize(Component component) { + StringBuilder builder = new StringBuilder().append('{'); + String value = component.getValue(); + writePropertyWithoutComma(builder, component.getType().toString(), value); + writeProperty(builder, "bold", component.getBold()); + writeProperty(builder, "italic", component.getItalic()); + writeProperty(builder, "underlined", component.getUnderlined()); + writeProperty(builder, "strikethrough", component.getStrikethrough()); + writeProperty(builder, "obfuscated", component.getObfuscated()); + writeProperty(builder, "font", component.getFont()); + writeProperty(builder, component.getColor()); + writeProperty(builder, "insertion", component.getInsertion()); + + if (component.getClickEvent() != null) { + StringBuilder clickEventBuilder = new StringBuilder().append('{'); + writePropertyWithoutComma(clickEventBuilder, "action", component.getClickEvent().toString()); + writeProperty(clickEventBuilder, "value", component.getClickEventValue()); + writeRawProperty(builder, "clickEvent", clickEventBuilder.append('}').toString()); + } + + if (component.getHoverEvent() != null) { + StringBuilder hoverEventBuilder = new StringBuilder().append('{'); + writePropertyWithoutComma(hoverEventBuilder, "action", component.getHoverEvent().toString()); + writeRawProperty(hoverEventBuilder, "value", serialize(component.getHoverEventValue())); + writeRawProperty(builder, "hoverEvent", hoverEventBuilder.append('}').toString()); + } + + if (component.getExtra() != null) { + writeRawProperty(builder, "extra", serialize(component.getExtra())); + } + + if (component.getWith() != null) { + writeRawProperty(builder, "with", serialize(component.getWith())); + } + + return builder.append('}').toString(); + } + + public static String serialize(Component... components) { + if (components.length == 0) { + return "{\"text\":\"\"}"; + } else if (components.length == 1) { + return serialize(components[0]); + } else { + StringBuilder builder = new StringBuilder(); + for (Component component : components) { + if (builder.length() == 0) { + builder.append('['); + } else { + builder.append(','); + } + builder.append(serialize(component)); + } + return builder.append(']').toString(); + } + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/component/ComponentType.java b/misc/src/main/java/org/zhdev/varioutil/component/ComponentType.java new file mode 100755 index 0000000..b314ca3 --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/ComponentType.java @@ -0,0 +1,20 @@ +package org.zhdev.varioutil.component; + +public enum ComponentType { + TEXT("text"), + TRANSLATE("translate"), + KEYBIND("keybind"), + SCORE("score"), + SELECTOR("selector"); + + private final String key; + + ComponentType(String key) { + this.key = key; + } + + @Override + public String toString() { + return key; + } +} \ No newline at end of file diff --git a/misc/src/main/java/org/zhdev/varioutil/component/HoverEvent.java b/misc/src/main/java/org/zhdev/varioutil/component/HoverEvent.java new file mode 100755 index 0000000..a03ce3f --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/HoverEvent.java @@ -0,0 +1,18 @@ +package org.zhdev.varioutil.component; + +public enum HoverEvent { + SHOW_TEXT("show_text"), + SHOW_ITEM("show_item"), + SHOW_ENTITY("show_entity"); + + private final String value; + + HoverEvent(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/misc/src/main/java/org/zhdev/varioutil/component/MinecraftFont.java b/misc/src/main/java/org/zhdev/varioutil/component/MinecraftFont.java new file mode 100755 index 0000000..c4c8c9d --- /dev/null +++ b/misc/src/main/java/org/zhdev/varioutil/component/MinecraftFont.java @@ -0,0 +1,32 @@ +package org.zhdev.varioutil.component; + +import java.util.LinkedHashMap; +import java.util.Map; + +public enum MinecraftFont { + UNIFORM("minecraft:uniform"), + ALT("minecraft:alt"), + DEFAULT("minecraft:default"); + private static final Map BY_NAMESPACE = new LinkedHashMap<>(); + + private final String value; + + MinecraftFont(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } + + public static MinecraftFont byNamespace(String namespace) { + return BY_NAMESPACE.get(namespace); + } + + static { + for (MinecraftFont font : values()) { + BY_NAMESPACE.put(font.value, font); + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index fb93455..d605d45 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,6 @@ UTF-8 UTF-8 - ../target/${project.artifactId}/javadocs