-
Notifications
You must be signed in to change notification settings - Fork 8
/
StringUtils.java
314 lines (280 loc) · 9.49 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
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
*/
@Singleton
public class StringUtils {
// an array containing all color codes in alphabetical order.
private 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 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 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 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 List<String> extractColorCodes(String text) {
List<String> output = Lists.newArrayList();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '§' && this.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 List<String> extractStyleCodes(String text) {
List<String> output = Lists.newArrayList();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '§' && this.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 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 (this.isStyleCode(text.charAt(i + 1))
|| this.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 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 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 (this.lastFormattingCodesOf(text).endsWith("§")) {
String colorCodes = this.lastFormattingCodesOf(text);
return this.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 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 boolean isStyleCode(char indicator) {
for (char current : styleCodes) {
if (current == indicator) {
return true;
}
}
return false;
}
public ChatColor getChatColor(char formattingCode) {
return isFormattingCode(formattingCode)
? ChatColor.getByChar(formattingCode)
: ChatColor.WHITE;
}
public org.bukkit.ChatColor getBukkitChatColor(char formattingCode) {
return isFormattingCode(formattingCode)
? org.bukkit.ChatColor.getByChar(formattingCode)
: org.bukkit.ChatColor.WHITE;
}
public ChatColor getChatColor(String formattingCode) {
char code = formattingCode.charAt(1);
return isFormattingCode(code) && formattingCode.charAt(0) == '§'
? ChatColor.getByChar(code)
: ChatColor.WHITE;
}
public 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;
}
public 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;
}
public String endsWithFormattingCode(String text) {
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;
}
public char randomColorCode() {
return colorCodes[ThreadLocalRandom.current().nextInt(colorCodes.length - 1)];
}
public char randomStyleCode() {
return styleCodes[ThreadLocalRandom.current().nextInt(colorCodes.length - 1)];
}
/**
* @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 boolean isFormattingCode(char indicator) {
return isColorCode(indicator) || isStyleCode(indicator);
}
}