diff --git a/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java b/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java index 73e27ec..3e96f0e 100644 --- a/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java +++ b/src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java @@ -21,7 +21,8 @@ public class IridiumColorAPI { /** * The current version of the server in the a form of a major version. - * If the static initialization for this fails, you know something's wrong with the server software. + * If the static initialization for this fails, you know something's wrong with + * the server software. * * @since 1.0.0 */ @@ -34,7 +35,7 @@ public class IridiumColorAPI { */ private static final boolean SUPPORTS_RGB = VERSION >= 16; - private static final List SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m","§l", "§n", "§o", "§k", "§m"); + private static final List SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m"); /** * Cached result of all legacy colors. @@ -78,7 +79,7 @@ public static String process(@Nonnull String string) { for (Pattern pattern : PATTERNS) { string = pattern.process(string); } - + string = ChatColor.translateAlternateColorCodes('&', string); return string; } @@ -93,8 +94,8 @@ public static String process(@Nonnull String string) { @Nonnull public static List process(@Nonnull List strings) { return strings.stream() - .map(IridiumColorAPI::process) - .collect(Collectors.toList()); + .map(IridiumColorAPI::process) + .collect(Collectors.toList()); } /** @@ -119,22 +120,10 @@ public static String color(@Nonnull String string, @Nonnull Color color) { */ @Nonnull public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) { - StringBuilder specialColors = new StringBuilder(); - for (String color : SPECIAL_COLORS) { - if (string.contains(color)) { - specialColors.append(color); - string = string.replace(color, ""); - } - } + String originalString = string; - StringBuilder stringBuilder = new StringBuilder(); - ChatColor[] colors = createGradient(start, end, string.length()); - String[] characters = string.split(""); - for (int i = 0; i < string.length(); i++) { - stringBuilder.append(colors[i]).append(specialColors).append(characters[i]); - } - - return stringBuilder.toString(); + ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length()); + return apply(originalString, colors); } /** @@ -146,22 +135,10 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul */ @Nonnull public static String rainbow(@Nonnull String string, float saturation) { - StringBuilder specialColors = new StringBuilder(); - for (String color : SPECIAL_COLORS) { - if (string.contains(color)) { - specialColors.append(color); - string = string.replace(color, ""); - } - } - - StringBuilder stringBuilder = new StringBuilder(); - ChatColor[] colors = createRainbow(string.length(), saturation); - String[] characters = string.split(""); - for (int i = 0; i < string.length(); i++) { - stringBuilder.append(colors[i]).append(specialColors).append(characters[i]); - } - - return stringBuilder.toString(); + String originalString = string; + + ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation); + return apply(originalString, colors); } /** @@ -172,14 +149,16 @@ public static String rainbow(@Nonnull String string, float saturation) { */ @Nonnull public static ChatColor getColor(@Nonnull String string) { - return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16))) : getClosestColor(new Color(Integer.parseInt(string, 16))); + return SUPPORTS_RGB ? ChatColor.of(new Color(Integer.parseInt(string, 16))) + : getClosestColor(new Color(Integer.parseInt(string, 16))); } /** - * Removes all color codes from the provided String, including IridiumColorAPI patterns. + * Removes all color codes from the provided String, including IridiumColorAPI + * patterns. * - * @param string The String which should be stripped - * @return The stripped string without color codes + * @param string The String which should be stripped + * @return The stripped string without color codes * @since 1.0.5 */ @Nonnull @@ -187,6 +166,41 @@ public static String stripColorFormatting(@Nonnull String string) { return string.replaceAll("<#[0-9A-F]{6}>|[&§][a-f0-9lnokm]|<[/]?[A-Z]{5,8}(:[0-9A-F]{6})?[0-9]*>", ""); } + @Nonnull + 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]); + } + return stringBuilder.toString(); + } + + @Nonnull + private static String withoutSpecialChar(@Nonnull String source) { + String workingString = source; + for (String color : SPECIAL_COLORS) { + if (workingString.contains(color)) { + workingString = workingString.replace(color, ""); + } + } + return workingString; + } + /** * Returns a rainbow array of chat colors. * @@ -199,7 +213,7 @@ public static String stripColorFormatting(@Nonnull String string) { private static ChatColor[] createRainbow(int step, float saturation) { ChatColor[] colors = new ChatColor[step]; double colorStep = (1.00 / step); - + for (int i = 0; i < step; i++) { Color color = Color.getHSBColor((float) (colorStep * i), saturation, saturation); if (SUPPORTS_RGB) { @@ -208,7 +222,7 @@ private static ChatColor[] createRainbow(int step, float saturation) { colors[i] = getClosestColor(color); } } - + return colors; } @@ -241,11 +255,10 @@ private static ChatColor[] createGradient(@Nonnull Color start, @Nonnull Color e colors[i] = getClosestColor(color); } } - + return colors; } - /** * Returns the closest legacy color from an rgb color * diff --git a/src/test/java/com/iridium/iridiumcolorapi/IridiumColorAPITest.java b/src/test/java/com/iridium/iridiumcolorapi/IridiumColorAPITest.java index 0168420..3111d59 100644 --- a/src/test/java/com/iridium/iridiumcolorapi/IridiumColorAPITest.java +++ b/src/test/java/com/iridium/iridiumcolorapi/IridiumColorAPITest.java @@ -25,9 +25,13 @@ static void setup() { @Test void process() { + assertEquals("§x§1§2§3§4§5§6Test", IridiumColorAPI.process("Test")); + assertEquals("§x§1§2§3§4§5§6Te§kst", IridiumColorAPI.process("Te&kst")); assertEquals("§x§2§c§0§8§b§a§lT§x§1§e§3§3§a§f§le§x§1§0§5§e§a§4§ls§x§0§2§8§9§9§9§lt", IridiumColorAPI.process("&lTest")); + assertEquals("§x§2§c§0§8§b§a§lT§x§1§e§3§3§a§f§le§x§1§0§5§e§a§4§ls§x§0§2§8§9§9§9§l§kt", IridiumColorAPI.process("&lTes§kt")); assertEquals("§x§f§f§0§0§0§0§lT§x§8§0§f§f§0§0§le§x§0§0§f§f§f§f§ls§x§8§0§0§0§f§f§lt", IridiumColorAPI.process("&lTest")); + assertEquals("§x§f§f§0§0§0§0§lT§x§8§0§f§f§0§0§le§x§0§0§f§f§f§f§ls§x§8§0§0§0§f§f§l§kt", IridiumColorAPI.process("&lTes&kt")); assertEquals("§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*§x§1§2§3§4§5§6*", IridiumColorAPI.process("******************************************************************************************************************************************************************************************************")); }