diff --git a/build.gradle b/build.gradle index 2487a30..88a1e7f 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,7 @@ dependencies { implementation 'de.tr7zw:item-nbt-api:2.12.4' implementation 'com.github.Redempt:RedLib:6.6.1' implementation 'com.github.Redempt:Crunch:2.0.3' - implementation 'com.github.cryptomorin:XSeries:10.0.0' + implementation 'com.github.cryptomorin:XSeries:11.0.0' implementation 'com.github.Revxrsal.Lamp:common:3.2.1' implementation 'com.github.Revxrsal.Lamp:bukkit:3.2.1' implementation 'com.github.Sven65:Item-Names:1.0.2' @@ -43,11 +43,11 @@ dependencies { compileOnly 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT' compileOnly 'org.jetbrains:annotations:24.1.0' - compileOnly 'me.clip:placeholderapi:2.11.5' + compileOnly 'me.clip:placeholderapi:2.11.6' } group = 'me.byteful.plugin' -version = '1.3.7' +version = '1.3.8' description = 'LevelTools' java.sourceCompatibility = JavaVersion.VERSION_1_8 diff --git a/src/main/java/me/byteful/plugin/leveltools/util/Text.java b/src/main/java/me/byteful/plugin/leveltools/util/Text.java index 44e52fb..db313a9 100644 --- a/src/main/java/me/byteful/plugin/leveltools/util/Text.java +++ b/src/main/java/me/byteful/plugin/leveltools/util/Text.java @@ -3,18 +3,55 @@ import org.bukkit.ChatColor; import org.jetbrains.annotations.NotNull; -/** - * Pulled utility out of lucko's helper library. Since this is pretty much all that was used from - * helper, I removed the lib to minimize the size of LevelTools' jar. - */ +import java.util.Arrays; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; + +// Borrowed from RedLib (https://github.com/boxbeam/RedCommands/blob/master/src/redempt/redlib/misc/FormatUtils.java) public final class Text { @NotNull - public static String colorize(@NotNull String string) { - return ChatColor.translateAlternateColorCodes('&', string); + public static String decolorize(@NotNull String string) { + return colorize(string).replace("" + ChatColor.COLOR_CHAR, "&"); } + private static Set colorChars = "4c6e2ab319d5f780rlonmk".chars().mapToObj(i -> (char) i).collect(Collectors.toSet()); + @NotNull - public static String decolorize(@NotNull String string) { - return colorize(string).replace("" + ChatColor.COLOR_CHAR, "&"); + // Author: boxbeam + public static String colorize(String input) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < input.length(); i++) { + char c = input.charAt(i); + if (i + 1 >= input.length()) { + builder.append(c); + continue; + } + char n = input.charAt(i + 1); + if (c == '\\' && (n == '&' || n == '\\')) { + i++; + builder.append(n); + continue; + } + if (c != '&') { + builder.append(c); + continue; + } + if (colorChars.contains(n)) { + builder.append(ChatColor.COLOR_CHAR); + continue; + } + if (n == '#' && i + 7 <= input.length()) { + String hexCode = input.substring(i + 2, i + 8).toUpperCase(Locale.ROOT); + if (hexCode.chars().allMatch(ch -> (ch <= '9' && ch >= '0') || (ch <= 'F' && ch >= 'A'))) { + hexCode = Arrays.stream(hexCode.split("")).map(s -> ChatColor.COLOR_CHAR + s).collect(Collectors.joining()); + builder.append(ChatColor.COLOR_CHAR).append("x").append(hexCode); + i += 7; + continue; + } + } + builder.append(c); + } + return builder.toString(); } }