@@ -21,7 +21,8 @@ public class IridiumColorAPI {
2121
2222 /**
2323 * The current version of the server in the a form of a major version.
24- * If the static initialization for this fails, you know something's wrong with the server software.
24+ * If the static initialization for this fails, you know something's wrong with
25+ * the server software.
2526 *
2627 * @since 1.0.0
2728 */
@@ -34,7 +35,7 @@ public class IridiumColorAPI {
3435 */
3536 private static final boolean SUPPORTS_RGB = VERSION >= 16 ;
3637
37- private static final List <String > SPECIAL_COLORS = Arrays .asList ("&l" , "&n" , "&o" , "&k" , "&m" ,"§l" , "§n" , "§o" , "§k" , "§m" );
38+ private static final List <String > SPECIAL_COLORS = Arrays .asList ("&l" , "&n" , "&o" , "&k" , "&m" , "§l" , "§n" , "§o" , "§k" , "§m" );
3839
3940 /**
4041 * Cached result of all legacy colors.
@@ -78,7 +79,7 @@ public static String process(@Nonnull String string) {
7879 for (Pattern pattern : PATTERNS ) {
7980 string = pattern .process (string );
8081 }
81-
82+
8283 string = ChatColor .translateAlternateColorCodes ('&' , string );
8384 return string ;
8485 }
@@ -93,8 +94,8 @@ public static String process(@Nonnull String string) {
9394 @ Nonnull
9495 public static List <String > process (@ Nonnull List <String > strings ) {
9596 return strings .stream ()
96- .map (IridiumColorAPI ::process )
97- .collect (Collectors .toList ());
97+ .map (IridiumColorAPI ::process )
98+ .collect (Collectors .toList ());
9899 }
99100
100101 /**
@@ -119,22 +120,10 @@ public static String color(@Nonnull String string, @Nonnull Color color) {
119120 */
120121 @ Nonnull
121122 public static String color (@ Nonnull String string , @ Nonnull Color start , @ Nonnull Color end ) {
122- StringBuilder specialColors = new StringBuilder ();
123- for (String color : SPECIAL_COLORS ) {
124- if (string .contains (color )) {
125- specialColors .append (color );
126- string = string .replace (color , "" );
127- }
128- }
123+ String originalString = string ;
129124
130- StringBuilder stringBuilder = new StringBuilder ();
131- ChatColor [] colors = createGradient (start , end , string .length ());
132- String [] characters = string .split ("" );
133- for (int i = 0 ; i < string .length (); i ++) {
134- stringBuilder .append (colors [i ]).append (specialColors ).append (characters [i ]);
135- }
136-
137- return stringBuilder .toString ();
125+ ChatColor [] colors = createGradient (start , end , withoutSpecialChar (string ).length ());
126+ return apply (originalString , colors );
138127 }
139128
140129 /**
@@ -146,22 +135,10 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul
146135 */
147136 @ Nonnull
148137 public static String rainbow (@ Nonnull String string , float saturation ) {
149- StringBuilder specialColors = new StringBuilder ();
150- for (String color : SPECIAL_COLORS ) {
151- if (string .contains (color )) {
152- specialColors .append (color );
153- string = string .replace (color , "" );
154- }
155- }
156-
157- StringBuilder stringBuilder = new StringBuilder ();
158- ChatColor [] colors = createRainbow (string .length (), saturation );
159- String [] characters = string .split ("" );
160- for (int i = 0 ; i < string .length (); i ++) {
161- stringBuilder .append (colors [i ]).append (specialColors ).append (characters [i ]);
162- }
163-
164- return stringBuilder .toString ();
138+ String originalString = string ;
139+
140+ ChatColor [] colors = createRainbow (withoutSpecialChar (string ).length (), saturation );
141+ return apply (originalString , colors );
165142 }
166143
167144 /**
@@ -172,21 +149,58 @@ public static String rainbow(@Nonnull String string, float saturation) {
172149 */
173150 @ Nonnull
174151 public static ChatColor getColor (@ Nonnull String string ) {
175- return SUPPORTS_RGB ? ChatColor .of (new Color (Integer .parseInt (string , 16 ))) : getClosestColor (new Color (Integer .parseInt (string , 16 )));
152+ return SUPPORTS_RGB ? ChatColor .of (new Color (Integer .parseInt (string , 16 )))
153+ : getClosestColor (new Color (Integer .parseInt (string , 16 )));
176154 }
177155
178156 /**
179- * Removes all color codes from the provided String, including IridiumColorAPI patterns.
157+ * Removes all color codes from the provided String, including IridiumColorAPI
158+ * patterns.
180159 *
181- * @param string The String which should be stripped
182- * @return The stripped string without color codes
160+ * @param string The String which should be stripped
161+ * @return The stripped string without color codes
183162 * @since 1.0.5
184163 */
185164 @ Nonnull
186165 public static String stripColorFormatting (@ Nonnull String string ) {
187166 return string .replaceAll ("<#[0-9A-F]{6}>|[&§][a-f0-9lnokm]|<[/]?[A-Z]{5,8}(:[0-9A-F]{6})?[0-9]*>" , "" );
188167 }
189168
169+ @ Nonnull
170+ private static String apply (@ Nonnull String source , ChatColor [] colors ) {
171+ StringBuilder specialColors = new StringBuilder ();
172+ StringBuilder stringBuilder = new StringBuilder ();
173+ String [] characters = source .split ("" );
174+ int outIndex = 0 ;
175+ for (int i = 0 ; i < characters .length ; i ++) {
176+ if (characters [i ].equals ("&" ) || characters [i ].equals ("§" )) {
177+ if (i + 1 < characters .length ) {
178+ if (characters [i + 1 ].equals ("r" )) {
179+ specialColors .setLength (0 );
180+ } else {
181+ specialColors .append (characters [i ]);
182+ specialColors .append (characters [i + 1 ]);
183+ }
184+ i ++;
185+ } else
186+ stringBuilder .append (colors [outIndex ++]).append (specialColors ).append (characters [i ]);
187+ } else
188+ stringBuilder .append (colors [outIndex ++]).append (specialColors ).append (characters [i ]);
189+ }
190+ return stringBuilder .toString ();
191+ }
192+
193+ @ Nonnull
194+ private static String withoutSpecialChar (@ Nonnull String source ) {
195+ String workingString = source ;
196+ for (String color : SPECIAL_COLORS ) {
197+ if (workingString .contains (color )) {
198+ workingString = workingString .replace (color , "" );
199+ }
200+ }
201+ return workingString ;
202+ }
203+
190204 /**
191205 * Returns a rainbow array of chat colors.
192206 *
@@ -199,7 +213,7 @@ public static String stripColorFormatting(@Nonnull String string) {
199213 private static ChatColor [] createRainbow (int step , float saturation ) {
200214 ChatColor [] colors = new ChatColor [step ];
201215 double colorStep = (1.00 / step );
202-
216+
203217 for (int i = 0 ; i < step ; i ++) {
204218 Color color = Color .getHSBColor ((float ) (colorStep * i ), saturation , saturation );
205219 if (SUPPORTS_RGB ) {
@@ -208,7 +222,7 @@ private static ChatColor[] createRainbow(int step, float saturation) {
208222 colors [i ] = getClosestColor (color );
209223 }
210224 }
211-
225+
212226 return colors ;
213227 }
214228
@@ -241,11 +255,10 @@ private static ChatColor[] createGradient(@Nonnull Color start, @Nonnull Color e
241255 colors [i ] = getClosestColor (color );
242256 }
243257 }
244-
258+
245259 return colors ;
246260 }
247261
248-
249262 /**
250263 * Returns the closest legacy color from an rgb color
251264 *
0 commit comments