-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathChatMessages.java
596 lines (499 loc) · 16.9 KB
/
ChatMessages.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.util.chat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.LanguageChangeListener;
import ch.njol.skript.util.Utils;
import net.md_5.bungee.api.ChatColor;
/**
* Handles parsing chat messages.
*/
public class ChatMessages {
/**
* Link parse mode for potential links which are not marked with tags.
*/
public static LinkParseMode linkParseMode = LinkParseMode.DISABLED;
/**
* If color codes should also function as reset code.
*/
public static boolean colorResetCodes = false;
/**
* Chat codes, see {@link SkriptChatCode}.
*/
static final Map<String, ChatCode> codes = new HashMap<>();
/**
* Chat codes registered by addons, as backup for language changes.
*/
static final Set<ChatCode> addonCodes = new HashSet<>();
/**
* Color chars, as used by Mojang's legacy chat colors.
*/
static final ChatCode[] colorChars = new ChatCode[256];
/**
* Used to detect links when strict link parsing is enabled.
*/
@SuppressWarnings("null")
static final Pattern linkPattern = Pattern.compile("[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)");
/**
* Instance of GSON we use for serialization.
*/
static final Gson gson;
/**
* Registers language change listener for chat system.
* Called once by Skript, please don't call this addon developers.
*/
public static void registerListeners() {
// When language changes or server is loaded loop through all chatcodes
Language.addListener(new LanguageChangeListener() {
@Override
public void onLanguageChange() {
codes.clear();
Skript.debug("Parsing message style lang files");
for (SkriptChatCode code : SkriptChatCode.values()) {
assert code != null;
registerChatCode(code);
}
// Re-register any missing addon chat codes
for (ChatCode code : addonCodes) {
assert code != null;
registerChatCode(code);
}
// Add formatting chars
addColorChar('k', SkriptChatCode.obfuscated);
addColorChar('l', SkriptChatCode.bold);
addColorChar('m', SkriptChatCode.strikethrough);
addColorChar('n', SkriptChatCode.underlined);
addColorChar('o', SkriptChatCode.italic);
addColorChar('r', SkriptChatCode.reset);
}
});
}
static void registerChatCode(ChatCode code) {
String langName = code.getLangName();
if (code.isLocalized()) {
if (code.getColorCode() != null) { // Color code!
// Avoid dependency on SkriptColor
for (String name : Language.getList("colors." + langName + ".names")) {
codes.put(name, code);
}
} else { // Not color code
for (String name : Language.getList("chat styles." + langName)) {
codes.put(name, code);
}
}
} else { // Not localized, lang name is as-is
codes.put(langName, code);
}
// Register color char
if (code.getColorChar() != 0) {
addColorChar(code.getColorChar(), code);
}
}
static void addColorChar(char code, ChatCode data) {
colorChars[code] = data;
colorChars[Character.toUpperCase(code)] = data;
}
static {
Gson nullableGson = new GsonBuilder().registerTypeAdapter(boolean.class, new MessageComponent.BooleanSerializer()).create();
assert nullableGson != null;
gson = nullableGson;
}
/**
* Component list - this is just serialization mapper class.
*/
private static class ComponentList {
public ComponentList(List<MessageComponent> components) {
this.extra = components;
}
@SuppressWarnings("unused")
public ComponentList(MessageComponent[] components) {
this.extra = Arrays.asList(components);
}
/**
* DO NOT USE!
*/
@SuppressWarnings("unused")
@Deprecated
public String text = "";
/**
* Actual message data.
*/
@SuppressWarnings("unused")
@Nullable
public List<MessageComponent> extra;
}
/**
* Parses a string to list of chat message components.
* @param msg Input string.
* @return List with components.
*/
@SuppressWarnings("null")
public static List<MessageComponent> parse(String msg) {
char[] chars = msg.toCharArray();
List<MessageComponent> components = new ArrayList<>();
MessageComponent current = new MessageComponent();
components.add(current);
StringBuilder curStr = new StringBuilder();
boolean lastWasColor = true;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
ChatCode code = null;
String param = "";
if (c == '<') { // Tag parsing
// Find where the tag ends
int end = -1;
int angleBrackets = 1; // Ignore stuff that looks like tag inside the tag
for (int j = i + 1; j < chars.length; j++) {
char c2 = chars[j];
if (c2 == '<')
angleBrackets++;
else if (c2 == '>')
angleBrackets--;
if (angleBrackets == 0) {
end = j;
break;
}
}
if (end != -1) { // If this COULD be valid tag...
String tag = msg.substring(i + 1, end);
String name;
if (tag.contains(":")) {
String[] split = tag.split(":", 2);
name = split[0];
param = split[1];
} else {
name = tag;
}
name = name.toLowerCase(); // Tags are case-insensitive
boolean tryHex = Utils.HEX_SUPPORTED && name.startsWith("#");
ChatColor chatColor = null;
if (tryHex) {
chatColor = Utils.parseHexColor(name);
tryHex = chatColor != null;
}
code = codes.get(name);
if (code != null || tryHex) { // ... and if the tag IS really valid
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;
MessageComponent old = current;
current = new MessageComponent();
components.add(current);
if (tryHex) {
current.color = chatColor;
} else if (code.getColorCode() != null) { // Just update color code
current.color = ChatColor.getByChar(code.getColorChar());
} else {
assert param != null;
code.updateComponent(current, param); // Call SkriptChatCode update
}
// Copy styles from old to current if needed
copyStyles(old, current);
// Increment i to tag end
i = end;
lastWasColor = true;
continue;
}
// If this is invalid tag, just ignore it. Maybe scripter was trying to be clever with formatting
}
} else if (c == '&' || c == '§') {
// Corner case: this is last character, so we cannot get next
if (i == chars.length - 1) {
curStr.append(c);
continue;
}
char color = chars[i + 1];
boolean tryHex = Utils.HEX_SUPPORTED && color == 'x';
ChatColor chatColor = null;
if (tryHex && i + 14 < chars.length) { // Try to parse hex "&x&1&2&3&4&5&6"
chatColor = Utils.parseHexColor(msg.substring(i + 2, i + 14).replace("&", "").replace("§", ""));
tryHex = chatColor != null;
}
if (color >= colorChars.length) { // Invalid Unicode color character
curStr.append(c);
continue;
}
code = colorChars[color];
if (code == null && !tryHex) {
curStr.append(c).append(color); // Invalid formatting char, plain append
} else {
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;
MessageComponent old = current;
current = new MessageComponent();
components.add(current);
if (tryHex) { // Set color to hex ChatColor
current.color = chatColor;
i = i + 12; // Skip past all the tags
} else if (code.getColorCode() != null) { // Just update color code
current.color = ChatColor.getByChar(code.getColorChar());
} else {
code.updateComponent(current, param); // Call SkriptChatCode update
}
// Copy styles from old to current if needed
copyStyles(old, current);
}
i++; // Skip this and color char
lastWasColor = true;
continue;
}
// Attempt link parsing, if a tag was not found
if ((linkParseMode == LinkParseMode.STRICT || linkParseMode == LinkParseMode.LENIENT) && c == 'h') {
String rest = msg.substring(i); // Get rest of string
String link = null;
if (rest.startsWith("http://") || rest.startsWith("https://")) {
link = rest.split(" ", 2)[0];
}
// Link found
if (link != null && !link.isEmpty()) {
// Take previous component, create new
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;
MessageComponent old = current;
current = new MessageComponent();
copyStyles(old, current);
components.add(current);
// Make new component a link
SkriptChatCode.open_url.updateComponent(current, link); // URL for client...
current.text = link; // ... and for player
i += link.length() - 1; // Skip link for all other parsing
// Add one MORE component (this comes after the link)
current = new MessageComponent();
components.add(current);
continue;
}
} else if (linkParseMode == LinkParseMode.LENIENT && (lastWasColor || i == 0 || chars[i - 1] == ' ')) {
// Lenient link parsing
String rest = msg.substring(i); // Get rest of string
String link = null;
String potentialLink = rest.split(" ", 2)[0]; // Split stuff
if (linkPattern.matcher(potentialLink).matches()) { // Check if it is at least somewhat valid URL
link = potentialLink;
}
// Link found
if (link != null && !link.isEmpty()) {
// Insert protocol (aka guess it) if it isn't there
String url;
if (!link.startsWith("http://") && !link.startsWith("https://")) {
url = "https://" + link;
} else {
url = link;
}
// Take previous component, create new
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;
MessageComponent old = current;
current = new MessageComponent();
copyStyles(old, current);
components.add(current);
// Make new component a link
SkriptChatCode.open_url.updateComponent(current, url); // URL for client...
current.text = link; // ... and for player
i += link.length() - 1; // Skip link for all other parsing
// Add one MORE component (this comes after the link)
current = new MessageComponent();
components.add(current);
continue;
}
}
curStr.append(c); // Append this char to curStr
lastWasColor = false;
}
String text = curStr.toString();
assert text != null;
current.text = text;
return components;
}
@SuppressWarnings("null")
public static MessageComponent[] parseToArray(String msg) {
return parse(msg).toArray(new MessageComponent[0]);
}
/**
* Parses a string that may only contain colour codes using the '§' character to a list of components.
*/
public static List<MessageComponent> fromParsedString(String msg) {
char[] chars = msg.toCharArray();
List<MessageComponent> components = new ArrayList<>();
MessageComponent current = new MessageComponent();
components.add(current);
StringBuilder curStr = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
ChatCode code;
String param = "";
if (c == '§') {
// Corner case: this is last character, so we cannot get next
if (i == chars.length - 1) {
curStr.append(c);
continue;
}
char color = chars[i + 1];
boolean tryHex = Utils.HEX_SUPPORTED && color == 'x';
ChatColor chatColor = null;
if (tryHex && i + 14 < chars.length) { // Try to parse hex "&x&1&2&3&4&5&6"
chatColor = Utils.parseHexColor(msg.substring(i + 2, i + 14).replace("&", "").replace("§", ""));
tryHex = chatColor != null;
}
if (color >= colorChars.length) { // Invalid Unicode color character
curStr.append(c);
continue;
}
code = colorChars[color];
if (code == null && !tryHex) {
curStr.append(c).append(color); // Invalid formatting char, plain append
} else {
String text = curStr.toString();
curStr = new StringBuilder();
current.text = text;
MessageComponent old = current;
current = new MessageComponent();
components.add(current);
if (tryHex) { // Set color to hex ChatColor
current.color = chatColor;
i = i + 12; // Skip past all the tags
} else if (code.getColorCode() != null) { // Just update color code
current.color = ChatColor.getByChar(code.getColorChar());
} else {
code.updateComponent(current, param); // Call SkriptChatCode update
}
// Copy styles from old to current if needed
copyStyles(old, current);
}
i++; // Skip this and color char
continue;
}
curStr.append(c); // Append this char to curStr
}
current.text = curStr.toString();
return components;
}
public static String toJson(String msg) {
ComponentList componentList = new ComponentList(parse(msg));
String json = gson.toJson(componentList);
assert json != null;
return json;
}
public static String toJson(List<MessageComponent> components) {
ComponentList componentList = new ComponentList(components);
String json = gson.toJson(componentList);
assert json != null;
return json;
}
/**
* Copies styles from component to another. Note that this only copies
* additional styling, i.e. if text was not bold and is bold, it will remain bold.
* @param from
* @param to
*/
public static void copyStyles(MessageComponent from, MessageComponent to) {
if (to.reset)
return;
// If we don't have color or colors don't reset formatting, copy formatting
if (to.color == null || !colorResetCodes) {
if (!to.bold)
to.bold = from.bold;
if (!to.italic)
to.italic = from.italic;
if (!to.underlined)
to.underlined = from.underlined;
if (!to.strikethrough)
to.strikethrough = from.strikethrough;
if (!to.obfuscated)
to.obfuscated = from.obfuscated;
if (to.color == null)
to.color = from.color;
}
// Links and such are never reset by color codes - weird, but it'd break too much stuff
if (to.clickEvent == null)
to.clickEvent = from.clickEvent;
if (to.insertion == null)
to.insertion = from.insertion;
if (to.hoverEvent == null)
to.hoverEvent = from.hoverEvent;
}
public static void shareStyles(MessageComponent[] components) {
MessageComponent previous = null;
for (MessageComponent c : components) {
if (previous != null) {
assert c != null;
copyStyles(previous, c);
}
previous = c;
}
}
/**
* Constructs plain text only message component.
* @param str
*/
public static MessageComponent plainText(String str) {
MessageComponent component = new MessageComponent();
component.text = str;
return component;
}
/**
* Registers a chat code. This is for addon developers.
* @param code Something that implements {@link ChatCode}.
* For inspiration, check {@link SkriptChatCode} source code.
*/
public static void registerAddonCode(@Nullable SkriptAddon addon, @Nullable ChatCode code) {
Objects.requireNonNull(addon);
Objects.requireNonNull(code);
addonCodes.add(code); // So that language reloads don't break everything
registerChatCode(code);
}
/**
* Strips all styles from given string.
* @param text String to strip styles from.
* @return A string without styles.
*/
public static String stripStyles(String text) {
List<MessageComponent> components = parse(text);
StringBuilder sb = new StringBuilder();
for (MessageComponent component : components) { // This also strips bracket tags ex. <red> <ttp:..> etc.
sb.append(component.text);
}
String plain = sb.toString();
if (Utils.HEX_SUPPORTED) // Strip '§x', '&x'
plain = plain.replaceAll("[§&]x", "");
plain = plain.replaceAll("(?i)[&§][0-9a-folkrnm]", ""); // strips colors & or § (ex. &5)
assert plain != null;
return plain;
}
}