|
1 | 1 | package me.byteful.plugin.leveltools.util; |
2 | 2 |
|
3 | | -import org.apache.commons.lang3.ArrayUtils; |
| 3 | +import java.util.*; |
4 | 4 | import org.bukkit.ChatColor; |
5 | 5 | import org.jetbrains.annotations.NotNull; |
| 6 | +import redempt.redlib.misc.FormatUtils; |
6 | 7 |
|
7 | | -import java.util.*; |
8 | | -import java.util.stream.Collectors; |
9 | | - |
10 | | -// Borrowed from RedLib (https://github.com/boxbeam/RedCommands/blob/master/src/redempt/redlib/misc/FormatUtils.java) |
11 | 8 | public final class Text { |
12 | 9 | @NotNull |
13 | 10 | public static String decolorize(@NotNull String string) { |
14 | 11 | return colorize(string).replace("" + ChatColor.COLOR_CHAR, "&"); |
15 | 12 | } |
16 | 13 |
|
17 | | - private static Set<Character> colorChars = "4c6e2ab319d5f780rlonmk".chars().mapToObj(i -> (char) i).collect(Collectors.toSet()); |
18 | | - |
19 | 14 | @NotNull |
20 | | - // Author: boxbeam |
21 | 15 | public static String colorize(String input) { |
22 | | - StringBuilder builder = new StringBuilder(); |
23 | | - for (int i = 0; i < input.length(); i++) { |
24 | | - char c = input.charAt(i); |
25 | | - if (i + 1 >= input.length()) { |
26 | | - builder.append(c); |
27 | | - continue; |
28 | | - } |
29 | | - char n = input.charAt(i + 1); |
30 | | - if (c == '\\' && (n == '&' || n == '\\')) { |
31 | | - i++; |
32 | | - builder.append(n); |
33 | | - continue; |
34 | | - } |
35 | | - if (c != '&') { |
36 | | - builder.append(c); |
37 | | - continue; |
38 | | - } |
39 | | - if (colorChars.contains(n)) { |
40 | | - builder.append(ChatColor.COLOR_CHAR); |
41 | | - continue; |
42 | | - } |
43 | | - if (n == '#' && i + 7 <= input.length()) { |
44 | | - String hexCode = input.substring(i + 2, i + 8).toUpperCase(Locale.ROOT); |
45 | | - if (hexCode.chars().allMatch(ch -> (ch <= '9' && ch >= '0') || (ch <= 'F' && ch >= 'A'))) { |
46 | | - hexCode = Arrays.stream(hexCode.split("")).map(s -> ChatColor.COLOR_CHAR + s).collect(Collectors.joining()); |
47 | | - builder.append(ChatColor.COLOR_CHAR).append("x").append(hexCode); |
48 | | - i += 7; |
49 | | - continue; |
50 | | - } |
51 | | - } |
52 | | - builder.append(c); |
53 | | - } |
54 | | - return builder.toString(); |
| 16 | + return FormatUtils.color(input); |
55 | 17 | } |
56 | 18 |
|
57 | 19 | // From Apache lang library |
58 | 20 | public static String[] substringsBetween(String str, String open, String close) { |
59 | 21 | int strLen = str.length(); |
60 | | - if (strLen == 0) { |
61 | | - return ArrayUtils.EMPTY_STRING_ARRAY; |
62 | | - } else { |
63 | | - int closeLen = close.length(); |
64 | | - int openLen = open.length(); |
65 | | - List<String> list = new ArrayList(); |
66 | | - |
67 | | - int end; |
68 | | - for(int pos = 0; pos < strLen - closeLen; pos = end + closeLen) { |
69 | | - int start = str.indexOf(open, pos); |
70 | | - if (start < 0) { |
71 | | - break; |
72 | | - } |
73 | | - |
74 | | - start += openLen; |
75 | | - end = str.indexOf(close, start); |
76 | | - if (end < 0) { |
77 | | - break; |
78 | | - } |
| 22 | + int closeLen = close.length(); |
| 23 | + int openLen = open.length(); |
| 24 | + List<String> list = new ArrayList(); |
| 25 | + |
| 26 | + int end; |
| 27 | + for (int pos = 0; pos < strLen - closeLen; pos = end + closeLen) { |
| 28 | + int start = str.indexOf(open, pos); |
| 29 | + if (start < 0) { |
| 30 | + break; |
| 31 | + } |
79 | 32 |
|
80 | | - list.add(str.substring(start, end)); |
| 33 | + start += openLen; |
| 34 | + end = str.indexOf(close, start); |
| 35 | + if (end < 0) { |
| 36 | + break; |
81 | 37 | } |
82 | 38 |
|
83 | | - return list.isEmpty() ? null : (String[])list.toArray(ArrayUtils.EMPTY_STRING_ARRAY); |
| 39 | + list.add(str.substring(start, end)); |
84 | 40 | } |
| 41 | + |
| 42 | + return list.isEmpty() ? null : list.toArray(new String[0]); |
85 | 43 | } |
86 | 44 | } |
0 commit comments