diff --git a/build.gradle.kts b/build.gradle.kts index 043b3db..4ef060d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "com.iridium" -version = "1.0.6" +version = "1.0.7" description = "IridiumColorAPI" java.sourceCompatibility = JavaVersion.VERSION_1_8 @@ -26,6 +26,10 @@ publishing { } } +tasks.withType().configureEach { + options.encoding = "UTF-8" +} + tasks { build { dependsOn(test) diff --git a/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java b/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java index 4d32031..9633735 100644 --- a/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java +++ b/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java @@ -11,7 +11,6 @@ import javax.annotation.Nonnull; import java.awt.*; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -34,7 +33,7 @@ public class IridiumColorAPI { * * @since 1.0.0 */ - private static final boolean SUPPORTS_RGB = VERSION >= 16; + private static final boolean SUPPORTS_RGB = VERSION >= 16 || VERSION == -1; private static final List SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m"); @@ -81,8 +80,7 @@ public static String process(@Nonnull String string) { string = pattern.process(string); } - string = ChatColor.translateAlternateColorCodes('&', string); - return string; + return ChatColor.translateAlternateColorCodes('&', string); } /** @@ -121,10 +119,8 @@ public static String color(@Nonnull String string, @Nonnull Color color) { */ @Nonnull public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) { - String originalString = string; - ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length()); - return apply(originalString, colors); + return apply(string, colors); } /** @@ -136,10 +132,8 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul */ @Nonnull public static String rainbow(@Nonnull String string, float saturation) { - String originalString = string; - ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation); - return apply(originalString, colors); + return apply(string, colors); } /** @@ -171,22 +165,22 @@ public static String stripColorFormatting(@Nonnull String string) { private static String apply(@Nonnull String source, ChatColor[] colors) { StringBuilder specialColors = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder(); - String[] characters = source.split(""); int outIndex = 0; - for (int i = 0; i < characters.length; i++) { - if (characters[i].equals("&") || characters[i].equals("§")) { - if (i + 1 < characters.length) { - if (characters[i + 1].equals("r")) { - specialColors.setLength(0); - } else { - specialColors.append(characters[i]); - specialColors.append(characters[i + 1]); - } - i++; - } else - stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]); - } else - stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]); + + for (int i = 0; i < source.length(); i++) { + char currentChar = source.charAt(i); + if (('&' != currentChar && '§' != currentChar) || i + 1 >= source.length()) { + stringBuilder.append(colors[outIndex++]).append(specialColors).append(currentChar); + continue; + } + + char nextChar = source.charAt(i + 1); + if ('r' == nextChar || 'R' == nextChar) { + specialColors.setLength(0); + } else { + specialColors.append(currentChar).append(nextChar); + } + i++; } return stringBuilder.toString(); } @@ -285,10 +279,14 @@ private static ChatColor getClosestColor(Color color) { * Gets a simplified major version (..., 9, 10, ..., 14). * In most cases, you shouldn't be using this method. * - * @return the simplified major version. + * @return the simplified major version, or -1 for bungeecord * @since 1.0.0 */ private static int getVersion() { + if (!classExists("org.bukkit.Bukkit") && classExists("net.md_5.bungee.api.ChatColor")) { + return -1; + } + String version = Bukkit.getVersion(); Validate.notEmpty(version, "Cannot get major Minecraft version from null or empty string"); @@ -301,7 +299,6 @@ private static int getVersion() { index = version.indexOf('-'); version = version.substring(0, index); } - // 1.13.2, 1.14.4, etc... int lastDot = version.lastIndexOf('.'); if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot); @@ -309,4 +306,20 @@ private static int getVersion() { return Integer.parseInt(version.substring(2)); } + /** + * Checks if a class exists in the current server + * + * @param path The path of that class + * @return true if the class exists, false if it doesn't + * @since 1.0.7 + */ + private static boolean classExists(final String path) { + try { + Class.forName(path); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + }