From d30d3e0e13a4642e3a192bb15d98f517e0161ca6 Mon Sep 17 00:00:00 2001 From: sergiobernalparrondo Date: Tue, 18 Jan 2022 18:12:14 +0100 Subject: [PATCH] Added unParse --- .../java/io/github/divios/jtext/JTextBuilder.java | 14 ++++++++++++++ .../divios/jtext/parsers/HexColorParser.java | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/divios/jtext/JTextBuilder.java b/src/main/java/io/github/divios/jtext/JTextBuilder.java index 5f73da1..fb6472d 100644 --- a/src/main/java/io/github/divios/jtext/JTextBuilder.java +++ b/src/main/java/io/github/divios/jtext/JTextBuilder.java @@ -188,6 +188,20 @@ public List parse(List collection, Player p) { return parsedList; } + public String unParse(String s) { + if (parseLegacyColors) s = legacyParser.unparse(s); + + return s; + } + + public List unParse(List list) { + List aux = new ArrayList<>(list.size()); + list.forEach(s -> aux.add(unParse(s))); + + return aux; + } + + private JTextBuilder copy() { JTextBuilder clone = new JTextBuilder(); diff --git a/src/main/java/io/github/divios/jtext/parsers/HexColorParser.java b/src/main/java/io/github/divios/jtext/parsers/HexColorParser.java index bb34944..f82b26c 100644 --- a/src/main/java/io/github/divios/jtext/parsers/HexColorParser.java +++ b/src/main/java/io/github/divios/jtext/parsers/HexColorParser.java @@ -30,7 +30,14 @@ public String parse(String s) { } public String unparse(String s) { - return "#" + ChatColor.stripColor(s); + char[] array = s.toCharArray(); + for (int i = 0; i < array.length - 1; i++) { + if (array[i] == ChatColor.COLOR_CHAR && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(array[i + 1]) != -1) { + array[i] = '&'; + array[i + 1] = Character.toLowerCase(array[i + 1]); + } + } + return new String(array); } public @Nullable Template parseAsTemplate(final String s) {