-
Notifications
You must be signed in to change notification settings - Fork 8
/
StringUtils.java
390 lines (355 loc) · 12.5 KB
/
StringUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package de.pxav.kelp.core.common;
import com.google.common.collect.Lists;
import com.google.inject.Singleton;
import net.md_5.bungee.api.ChatColor;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* This class contains useful methods to work with strings
* including normal char processing but also minecraft color codes.
*
* @author pxav
*/
public class StringUtils {
// an array containing all color codes in alphabetical order.
private static char[] colorCodes = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'
};
// an array containing all style codes in alphabetical order.
private static char[] styleCodes = new char[] {
'k', 'l', 'm', 'n', 'o', 'r'
};
/**
* Removes the last char of the given text.
*
* @param text The text you want to remove the char of.
* @return The given text without the last char.
*/
public static String removeLastChar(String text) {
// build a substring excluding the last char.
return text.substring(0, text.length() - 1);
}
/**
* Searches for all formatting codes inside the given
* string and removes them so that you only
* have the raw text without any formatting left.
*
* @param text The text you want to remove the color codes of.
* @return The final string without color codes.
*/
public static String removeFormattingCodes(String text) {
List<String> codes = extractFormattingCodes(text);
for (String current : codes) {
text = text.replace(current, "");
}
return text;
}
/**
* Searches for all color codes in the given text.
* This excludes style codes like §l, §k, etc.
*
* @param text The text you want to get the color codes of.
* @return The color codes in chronological order.
*/
public static List<String> extractColorCodes(String text) {
List<String> output = Lists.newArrayList();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '§' && isColorCode(text.charAt(i + 1))) {
output.add("§" + text.charAt(i + 1));
}
}
return output;
}
/**
* Searches for all style codes in the given text.
* This excludes color codes like §3, §b, etc.
*
* @param text The text you want to get the style codes of.
* @return The style codes in chronological order.
*/
public static List<String> extractStyleCodes(String text) {
List<String> output = Lists.newArrayList();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '§' && isStyleCode(text.charAt(i + 1))) {
output.add("§" + text.charAt(i + 1));
}
}
return output;
}
/**
* Searches for all formatting codes in a string
* no matter if they are style or color.
*
* @param text The text you want to get the codes of.
* @return The formatting codes in chronological order.
*/
public static List<String> extractFormattingCodes(String text) {
List<String> output = Lists.newArrayList();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) != '§') {
continue;
}
if (text.length() == i + 1) {
break;
}
if (isStyleCode(text.charAt(i + 1))
|| isColorCode(text.charAt(i + 1))) {
output.add("§" + text.charAt(i + 1));
}
}
return output;
}
/**
* Extracts all color codes from the given text
* and checks which of these have an impact on the last char(s)
* of it. So for example for the text '§6Hello §7You' it
* would return '§7' because this is the last color code
* in the string.
*
* This also includes style codes. The method returns the (one!)
* last color code and all of the following style codes,
* which come after it. So for example the text
* '§6Hello §7§oYou' would return
* '§7§o'.
*
* Furthermore this method searches for uncompleted color codes.
* If your text ends with a '§' this will also be appended to
* the final output. If you do not want this to happen,
* please use {@code #lastFullFormattingCodesOf(text)}.
*
* @param text The text you want to check.
* @return The last color and style codes.
*/
public static String lastFormattingCodesOf(String text) {
// get all formatting codes of the text and reverse their order.
List<String> reversed = extractFormattingCodes(text);
Collections.reverse(reversed);
List<String> codes = Lists.newArrayList();
/*
This loop iterates through all color codes of the text.
If it finds a style code it will simply add it to the list
and continue until it finds a color codes. Then it will
add this color code to the list and break immediately.
*/
for (String current : reversed) {
if (isStyleCode(current.charAt(1))) {
codes.add(current);
continue;
}
if (isColorCode(current.charAt(1))) {
codes.add(current);
break;
}
}
Collections.reverse(codes);
// check for uncompleted color codes and optionally add them
if (text.length() >= 16 && text.substring(0, 16).endsWith("§")) {
codes.add("§");
}
return codes.toString()
.replace("[", "")
.replace("]", "")
.replace(",", "")
.replace(" ", "");
}
/**
* Extracts all color codes from the given text
* and checks which of these have an impact on the last char(s)
* of it. So for example for the text '§6Hello §7You' it
* would return '§7' because this is the last color code
* in the string.
*
* This also includes style codes. The method returns the (one!)
* last color code and all of the following style codes,
* which come after it. So for example the text
* '§6Hello §7§oYou' would return
* '§7§o'.
*
* Please not that this method will only give you
* full formatting codes like '§a' and not '§'.
* If you want this to happen use {@code lastFormattingCodesOf(text)}.
*
* @param text The text you want to check.
* @return The last color and style codes.
*/
public static String lastFullFormattingCodesOf(String text) {
// check if the color codes have a single '§' at the end.
// this has to be removed as we only want to have full
// color codes.
if (lastFormattingCodesOf(text).endsWith("§")) {
String colorCodes = lastFormattingCodesOf(text);
return removeLastChar(colorCodes);
}
return lastFormattingCodesOf(text);
}
/**
* Checks the given indicator for a formatting code
* and decides whether it's a color code.
* Color codes are codes which do not affect the style
* of a string by changing font type, etc. but only
* the color.
*
* @param indicator The indicator symbol of the code you want to check
* e. g. '1' for dark blue
* @return {@code true} if the given indicator is
*/
public static boolean isColorCode(char indicator) {
for (char current : colorCodes) {
if (current == indicator) {
return true;
}
}
return false;
}
/**
* Checks the given indicator for a formatting code
* and decides whether it's a style code.
* Style codes are codes that do not affect the color
* of the given string, but for example the font type (bold).
*
* @param indicator The indicator symbol of the code you want to check
* e. g. 'l' for bold
* @return {@code true} if the given indicator is a style code.
*/
public static boolean isStyleCode(char indicator) {
for (char current : styleCodes) {
if (current == indicator) {
return true;
}
}
return false;
}
/**
* Takes a formatting code char (without the {@code '§'} in front)
* and converts it to a {@link ChatColor} object of the md_5 library.
* If there could be no chat color found, {@link ChatColor#WHITE WHITE}
* will be returned.
*
* @param formattingCode The formatting code to be converted.
* @return The corresponding md_5 chat color.
*/
public static ChatColor getChatColor(char formattingCode) {
return isFormattingCode(formattingCode)
? ChatColor.getByChar(formattingCode)
: ChatColor.WHITE;
}
/**
* Takes a formatting code char (without the {@code '§'} in front)
* and converts it to a {@link org.bukkit.ChatColor} object of the bukkit library.
* If there could be no chat color found, {@link org.bukkit.ChatColor#WHITE WHITE}
* will be returned.
*
* @param formattingCode The formatting code to be converted.
* @return The corresponding bukkit chat color.
*/
public static org.bukkit.ChatColor getBukkitChatColor(char formattingCode) {
return isFormattingCode(formattingCode)
? org.bukkit.ChatColor.getByChar(formattingCode)
: org.bukkit.ChatColor.WHITE;
}
/**
* Takes a formatting code with syntax {@code '§x'} and converts
* it to a {@link ChatColor} object of spigot/md_5 library. If the
* given code could not be found, {@link org.bukkit.ChatColor#WHITE WHITE}
* is returned.
*
* @param formattingCode The formatting code to be converted.
* @return The final {@link ChatColor} to be returned. {@code null} if the
* color was not found.
*/
public static ChatColor getChatColor(String formattingCode) {
char code = formattingCode.charAt(1);
return isFormattingCode(code) && formattingCode.charAt(0) == '§'
? ChatColor.getByChar(code)
: ChatColor.WHITE;
}
/**
* Takes a formatting code with syntax {@code '§x'} and converts
* it to a {@link org.bukkit.ChatColor} object of bukkit. If the
* given code could not be found, {@link org.bukkit.ChatColor#WHITE WHITE}
* is returned.
*
* @param formattingCode The formatting code to be converted.
* @return The final {@link org.bukkit.ChatColor} to be returned.
*/
public static org.bukkit.ChatColor getBukkitChatColor(String formattingCode) {
char code = formattingCode.charAt(1);
return isFormattingCode(code) && formattingCode.charAt(0) == '§'
? org.bukkit.ChatColor.getByChar(code)
: org.bukkit.ChatColor.WHITE;
}
/**
* Checks whether the given text ends with a color code.
* This does not check for style codes.
*
* @param text The text to be checked for color codes.
* @return The last color code of the text. {@code null} if there
* was no color code to be detected.
*/
public static String endsWithColorCode(String text) {
if (text.length() < 2) {
return null;
}
String code = text.substring(text.length() - 2);
if (code.charAt(0) == '§' && isColorCode(code.charAt(1))) {
return code;
}
return null;
}
/**
* Checks whether the given text ends with any formatting code
* (no matter if style or color).
*
* @param text The text to be checked for formatting codes.
* @return The last formatting code of the text. {@code null} if there
* was no color code to be detected.
*/
public static String endsWithFormattingCode(String text) {
// if the text is less than 2 chars long there can
// be no formatting code, so return immediately.
if (text.length() < 2) {
return null;
}
String code = text.substring(text.length() - 2);
if (code.charAt(0) == '§' && isFormattingCode(code.charAt(1))) {
return code;
}
return null;
}
/**
* Picks a random color code id. This means the result will only be
* something like {@code '1', 'b', ...} without a {@code '§'} in front.
*
* @return Any random bukkit color code.
*/
public static char randomColorCode() {
return colorCodes[ThreadLocalRandom.current().nextInt(colorCodes.length - 1)];
}
/**
* Picks a random style code. This means the result will only be
* something like {@code 'k', 'o', ...} without a {@code '§'} in front.
*
* @return
*/
public static char randomStyleCode() {
return styleCodes[ThreadLocalRandom.current().nextInt(colorCodes.length - 1)];
}
/**
* Picks any random formatting code from the list. This can either be
* a result of {@link #randomStyleCode()} or {@link #randomColorCode()}.
*
* @return Any random foramtting code without {@code '§'} in front.
*/
public static char randomFormattingCode() {
return ThreadLocalRandom.current().nextBoolean() ? randomColorCode() : randomStyleCode();
}
/**
* @param indicator The indicator of the code you want to check.
* @return {@code true} if the either a color code or a style code.
*/
public static boolean isFormattingCode(char indicator) {
return isColorCode(indicator) || isStyleCode(indicator);
}
}