Gets the ChatColors used at the end of the given input string. @param input Input string to retrieve the colors from. @return Any remaining ChatColors to pass onto the next line.
(@NotNull String input)
| 372 | * @return Any remaining ChatColors to pass onto the next line. |
| 373 | */ |
| 374 | @NotNull |
| 375 | public static String getLastColors(@NotNull String input) { |
| 376 | Preconditions.checkArgument(input != null, "Cannot get last colors from null text"); |
| 377 | |
| 378 | String result = ""; |
| 379 | int length = input.length(); |
| 380 | |
| 381 | // Search backwards from the end as it is faster |
| 382 | for (int index = length - 1; index > -1; index--) { |
| 383 | char section = input.charAt(index); |
| 384 | if (section == COLOR_CHAR && index < length - 1) { |
| 385 | |
| 386 | String hexColor = getHexColor(input, index); |
| 387 | if (hexColor != null) { |
| 388 | // We got a hex color |
| 389 | result = hexColor + result; |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | // It is not a hex color, check normal color |
| 394 | char c = input.charAt(index + 1); |
| 395 | ChatColor color = getByChar(c); |
| 396 | |
| 397 | if (color != null) { |
| 398 | result = color.toString() + result; |
| 399 | |
| 400 | // Once we find a color or reset we can stop searching |
| 401 | if (color.isColor() || color.equals(RESET)) { |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | @Nullable |
| 412 | private static String getHexColor(@NotNull String input, int index) { |