/
FormattedTextHelper.java
752 lines (726 loc) · 36.7 KB
/
FormattedTextHelper.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package com.denizenscript.denizen.utilities;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.properties.bukkit.BukkitElementExtensions;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.*;
import java.util.List;
public class FormattedTextHelper {
// <--[language]
// @name Denizen Text Formatting
// @group Denizen Magic
// @description
// Denizen provides a variety of special chat format options like "on_hover" and "on_click".
// These options exist within Denizen and do not appear in the historical Minecraft legacy chat format that most plugins and systems read.
// That legacy system has 16 colors (0-9, A-F) and a few toggleable formats (bold, italic, etc). It does not contain anything that needs more than just an on/off.
//
// Modern Minecraft, however, supports a JSON based "raw" message format that can do click events, hover events, full RGB colors, etc.
//
// Denizen therefore has its own internal system that works like the legacy format system, but also supports the new options normally only available as 'raw JSON'.
//
// Because it is entirely processed within Denizen, these options only work within Denizen, when performing actions that support raw JSON input.
// This magic tool exists to let you write messages without having to write the messy JSON.
//
// Be aware that many inputs do not support raw JSON, and as such are limited only the historical Minecraft legacy format.
// Also be aware that click events, hover events, etc. are exclusively limited to the chat bar and the pages of books, as you cannot mouse over anything else.
//
// Also note that RGB colors use a format that Spigot invented, meaning they will work in places that use Spigot's parser OR Denizen's version, but nowhere that uses the vanilla format still.
//
// Thanks to Paper's implementation of component APIs where Spigot was too lazy to, Paper servers have advanced text formatting available in more areas.
// -->
public static AsciiMatcher needsEscapeMatcher = new AsciiMatcher("&;[]");
public static String escape(String input) {
if (needsEscapeMatcher.containsAnyMatch(input)) {
input = input.replace("&", "&").replace(";", "&sc").replace("[", "&lb").replace("]", "&rb");
}
return input.replace(String.valueOf(ChatColor.COLOR_CHAR), "&ss");
}
public static String unescape(String input) {
if (input.indexOf('&') != -1) {
return input.replace("&sc", ";").replace("&lb", "[").replace("&rb", "]").replace("&ss", String.valueOf(ChatColor.COLOR_CHAR)).replace("&", "&");
}
return input;
}
public static boolean hasRootFormat(BaseComponent component) {
if (component.hasFormatting()) {
return true;
}
if (!(component instanceof TextComponent)) {
return false;
}
if (!((TextComponent) component).getText().isEmpty()) {
return false;
}
List<BaseComponent> extra = component.getExtra();
if (extra == null || extra.isEmpty()) {
return false;
}
return hasRootFormat(extra.get(0));
}
public static String stringify(BaseComponent[] components) {
if (components == null) {
return null;
}
if (components.length == 0) {
return "";
}
StringBuilder builder = new StringBuilder(128 * components.length);
if (hasRootFormat(components[0])) {
builder.append(RESET);
}
for (BaseComponent component : components) {
builder.append(stringify(component));
}
String output = builder.toString();
while (output.endsWith(RESET)) {
output = output.substring(0, output.length() - RESET.length());
}
while (output.startsWith(POSSIBLE_RESET_PREFIX) && output.length() > 4 && colorCodeInvalidator.isMatch(output.charAt(3))) {
output = output.substring(2);
}
return cleanRedundantCodes(output);
}
public static String stringifyRGBSpigot(String hex) {
StringBuilder hexBuilder = new StringBuilder(7);
hexBuilder.append('x');
for (int i = hex.length(); i < 6; i++) {
hexBuilder.append('0');
}
hexBuilder.append(hex);
hex = hexBuilder.toString();
StringBuilder outColor = new StringBuilder();
for (char c : hex.toCharArray()) {
outColor.append(org.bukkit.ChatColor.COLOR_CHAR).append(c);
}
return outColor.toString();
}
public static String stringify(BaseComponent component) {
if (component == null) {
return null;
}
StringBuilder builder = new StringBuilder(128);
ChatColor color = component.getColor();
if (color != null) {
builder.append(color);
}
if (component.isBold()) {
builder.append(ChatColor.BOLD);
}
if (component.isItalic()) {
builder.append(ChatColor.ITALIC);
}
if (component.isStrikethrough()) {
builder.append(ChatColor.STRIKETHROUGH);
}
if (component.isUnderlined()) {
builder.append(ChatColor.UNDERLINE);
}
if (component.isObfuscated()) {
builder.append(ChatColor.MAGIC);
}
if (component.getFontRaw() != null || (component.getFont() != null && component.getColorRaw() != null)) {
builder.append(ChatColor.COLOR_CHAR).append("[font=").append(component.getFont()).append("]");
}
boolean hasInsertion = component.getInsertion() != null;
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR).append("[insertion=").append(escape(component.getInsertion())).append("]");
}
boolean hasHover = component.getHoverEvent() != null;
if (hasHover) {
HoverEvent hover = component.getHoverEvent();
builder.append(ChatColor.COLOR_CHAR).append("[hover=").append(hover.getAction().name()).append(";").append(escape(NMSHandler.instance.stringForHover(hover))).append("]");
}
boolean hasClick = component.getClickEvent() != null;
if (hasClick) {
ClickEvent click = component.getClickEvent();
builder.append(ChatColor.COLOR_CHAR).append("[click=").append(click.getAction().name()).append(";").append(escape(click.getValue())).append("]");
}
if (component instanceof TextComponent) {
builder.append(((TextComponent) component).getText());
}
else if (component instanceof TranslatableComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[translate=").append(escape(((TranslatableComponent) component).getTranslate()));
List<BaseComponent> with = ((TranslatableComponent) component).getWith();
if (with != null) {
for (BaseComponent withComponent : with) {
builder.append(";").append(escape(stringify(withComponent)));
}
}
builder.append("]");
}
else if (component instanceof SelectorComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[selector=").append(escape(((SelectorComponent) component).getSelector())).append("]");
}
else if (component instanceof KeybindComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[keybind=").append(escape(((KeybindComponent) component).getKeybind())).append("]");
}
else if (component instanceof ScoreComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[score=").append(escape(((ScoreComponent) component).getName()))
.append(";").append(escape(((ScoreComponent) component).getObjective()))
.append(";").append(escape(((ScoreComponent) component).getValue())).append("]");
}
List<BaseComponent> after = component.getExtra();
if (after != null) {
for (BaseComponent afterComponent : after) {
builder.append(stringify(afterComponent));
}
}
if (hasClick) {
builder.append(ChatColor.COLOR_CHAR + "[/click]");
}
if (hasHover) {
builder.append(ChatColor.COLOR_CHAR + "[/hover]");
}
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR + "[/insertion]");
}
builder.append(RESET);
String output = builder.toString();
return cleanRedundantCodes(output);
}
public static final String RESET = ChatColor.RESET.toString(), POSSIBLE_RESET_PREFIX = RESET + ChatColor.COLOR_CHAR;
public static TextComponent copyFormatToNewText(TextComponent last) {
TextComponent toRet = new TextComponent();
toRet.setObfuscated(last.isObfuscatedRaw());
toRet.setBold(last.isBoldRaw());
toRet.setStrikethrough(last.isStrikethroughRaw());
toRet.setUnderlined(last.isUnderlinedRaw());
toRet.setItalic(last.isItalicRaw());
toRet.setColor(last.getColorRaw());
return toRet;
}
public static BaseComponent[] parse(String str, ChatColor baseColor) {
if (str == null) {
return null;
}
return parse(str, baseColor, true);
}
public static int findNextNormalColorSymbol(String base, int startAt) {
while (true) {
int next = base.indexOf(ChatColor.COLOR_CHAR, startAt);
if (next == -1 || next + 1 >= base.length()) {
return -1;
}
char after = base.charAt(next + 1);
if (colorCodeInvalidator.isMatch(after)) {
return next;
}
startAt = next + 1;
}
}
public static int findEndIndexFor(String base, String startSymbol, String endSymbol, int startAt) {
int layers = 1;
while (true) {
int next = base.indexOf(ChatColor.COLOR_CHAR, startAt);
if (next == -1) {
return -1;
}
if (next + endSymbol.length() >= base.length()) {
return -1;
}
if (base.startsWith(startSymbol, next + 1)) {
layers++;
}
else if (base.startsWith(endSymbol, next + 1)) {
layers--;
if (layers == 0) {
return next;
}
}
startAt = next + 1;
}
}
public static int findEndIndexFor(String base, String type, int startAt) {
return findEndIndexFor(base, "[" + type + "=", "[/" + type + "]", startAt);
}
public static String HEX = "0123456789abcdefABCDEF";
public static AsciiMatcher allowedCharCodes = new AsciiMatcher(HEX + "klmnorxKLMNORX[");
public static AsciiMatcher hexMatcher = new AsciiMatcher(HEX);
public static AsciiMatcher colorCodesOrReset = new AsciiMatcher(HEX + "rR"); // Any color code that can be invalidated
public static AsciiMatcher colorCodeInvalidator = new AsciiMatcher(HEX + "rRxX"); // Any code that can invalidate the colors above
public static String cleanRedundantCodes(String str) {
int index = str.indexOf(ChatColor.COLOR_CHAR);
if (index == -1) {
return str;
}
int start = 0;
StringBuilder output = new StringBuilder(str.length());
while (index != -1) {
output.append(str, start, index);
start = index;
if (index + 1 >= str.length()) {
break;
}
char symbol = str.charAt(index + 1);
if (allowedCharCodes.isMatch(symbol)) {
if (symbol == 'x' || symbol == 'X') { // Skip entire hex block
index = str.indexOf(ChatColor.COLOR_CHAR, index + 14);
continue;
}
int nextIndex = str.indexOf(ChatColor.COLOR_CHAR, index + 1);
if (colorCodesOrReset.isMatch(symbol) && nextIndex == index + 2 && nextIndex + 1 < str.length()) {
char nextSymbol = str.charAt(nextIndex + 1);
if (colorCodeInvalidator.isMatch(nextSymbol)) {
start = index + 2; // Exclude from output the initial (redundant) color code
index = nextIndex;
continue;
}
}
}
index = str.indexOf(ChatColor.COLOR_CHAR, index + 1);
}
output.append(str, start, str.length());
return output.toString();
}
public static TextComponent getCleanRef() {
TextComponent reference = new TextComponent();
reference.setBold(false);
reference.setItalic(false);
reference.setStrikethrough(false);
reference.setUnderlined(false);
reference.setObfuscated(false);
return reference;
}
public static BaseComponent[] parseSimpleColorsOnly(String str) {
TextComponent root = new TextComponent();
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
int lastStart = 0;
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
lastStart = firstChar;
}
TextComponent nextText = new TextComponent();
while (firstChar != -1 && firstChar + 1 < str.length()) {
char c = str.charAt(firstChar + 1);
if (allowedCharCodes.isMatch(c)) {
if (c == 'r' || c == 'R') {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
lastStart = firstChar + 2;
}
else if (c == 'X' || c == 'x' && firstChar + 13 < str.length()) {
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int i = 1; i <= 6; i++) {
if (str.charAt(firstChar + i * 2) != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexChar = str.charAt(firstChar + 1 + i * 2);
if (!hexMatcher.isMatch(hexChar)) {
color = null;
break;
}
color.append(hexChar);
}
if (color != null) {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
nextText.setColor(ChatColor.of(color.toString()));
firstChar += 12;
lastStart = firstChar + 2;
}
}
else if (colorCodesOrReset.isMatch(c)) {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
nextText.setColor(ChatColor.getByChar(c));
lastStart = firstChar + 2;
}
else { // format code
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (c == 'k' || c == 'K') {
nextText.setObfuscated(true);
}
else if (c == 'l' || c == 'L') {
nextText.setBold(true);
}
else if (c == 'm' || c == 'M') {
nextText.setStrikethrough(true);
}
else if (c == 'n' || c == 'N') {
nextText.setUnderlined(true);
}
else if (c == 'o' || c == 'O') {
nextText.setItalic(true);
}
lastStart = firstChar + 2;
}
}
firstChar = str.indexOf(ChatColor.COLOR_CHAR, firstChar + 1);
}
if (lastStart < str.length()) {
nextText.setText(str.substring(lastStart));
root.addExtra(nextText);
}
return new BaseComponent[] { root };
}
public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
if (str == null) {
return null;
}
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
if (str.contains("://")) {
firstChar = 0;
}
else {
TextComponent base = new TextComponent();
base.addExtra(new TextComponent(str)); // This is for compat with how Spigot does parsing of plaintext.
return new BaseComponent[]{base};
}
}
str = cleanRedundantCodes(str);
if (cleanBase && str.length() < 512) {
if (!str.contains(ChatColor.COLOR_CHAR + "[") && !str.contains("://")) {
return parseSimpleColorsOnly(str);
}
// Ensure compat with certain weird vanilla translate strings.
if (str.startsWith(ChatColor.COLOR_CHAR + "[translate=") && str.endsWith("]") && str.indexOf(']') == str.length() - 1) {
String translatable = str.substring("&[translate=".length(), str.length() - 1);
TranslatableComponent component = new TranslatableComponent();
List<String> innardParts = CoreUtilities.split(translatable, ';');
component.setTranslate(unescape(innardParts.get(0)));
for (int i = 1; i < innardParts.size(); i++) {
for (BaseComponent subComponent : parse(unescape(innardParts.get(i)), baseColor, false)) {
component.addWith(subComponent);
}
}
return new BaseComponent[] { component };
}
}
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(baseColor);
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
}
}
else {
base.setText(str.substring(0, firstChar));
}
root.addExtra(base);
str = str.substring(firstChar);
char[] chars = str.toCharArray();
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ChatColor.COLOR_CHAR && i + 1 < chars.length) {
char code = chars[i + 1];
if (!allowedCharCodes.isMatch(code)) {
continue;
}
if (code == '[') {
int endBracket = str.indexOf(']', i + 2);
if (endBracket == -1) {
continue;
}
String innards = str.substring(i + 2, endBracket);
List<String> innardParts = CoreUtilities.split(innards, ';');
List<String> innardBase = CoreUtilities.split(innardParts.get(0), '=', 2);
innardParts.remove(0);
String innardType = CoreUtilities.toLowerCase(innardBase.get(0));
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText.setText("");
if (innardType.equals("score") && innardParts.size() == 2) {
ScoreComponent component = new ScoreComponent(unescape(innardBase.get(1)), unescape(innardParts.get(0)), unescape(innardParts.get(1)));
lastText.addExtra(component);
}
else if (innardType.equals("keybind")) {
KeybindComponent component = new KeybindComponent();
component.setKeybind(unescape(innardBase.get(1)));
lastText.addExtra(component);
}
else if (innardType.equals("selector")) {
SelectorComponent component = new SelectorComponent(unescape(innardBase.get(1)));
lastText.addExtra(component);
}
else if (innardType.equals("translate")) {
TranslatableComponent component = new TranslatableComponent();
component.setTranslate(unescape(innardBase.get(1)));
for (String extra : innardParts) {
for (BaseComponent subComponent : parse(unescape(extra), baseColor, false)) {
component.addWith(subComponent);
}
}
lastText.addExtra(component);
}
else if (innardType.equals("click") && innardParts.size() == 1) {
int endIndex = findEndIndexFor(str, "click", endBracket);
if (endIndex == -1) {
continue;
}
TextComponent clickableText = new TextComponent();
ClickEvent.Action action = ElementTag.asEnum(ClickEvent.Action.class, innardBase.get(1));
clickableText.setClickEvent(new ClickEvent(action == null ? ClickEvent.Action.SUGGEST_COMMAND : action, unescape(innardParts.get(0))));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
clickableText.addExtra(subComponent);
}
lastText.addExtra(clickableText);
endBracket = endIndex + "&[/click".length();
}
else if (innardType.equals("hover")) {
int endIndex = findEndIndexFor(str, "hover", endBracket);
if (endIndex == -1) {
continue;
}
TextComponent hoverableText = new TextComponent();
HoverEvent.Action action = ElementTag.asEnum(HoverEvent.Action.class, innardBase.get(1));
if (HoverFormatHelper.processHoverInput(action == null ? HoverEvent.Action.SHOW_TEXT : action, hoverableText, innardParts.get(0))) {
continue;
}
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
hoverableText.addExtra(subComponent);
}
lastText.addExtra(hoverableText);
endBracket = endIndex + "&[/hover".length();
}
else if (innardType.equals("insertion")) {
int endIndex = str.indexOf(ChatColor.COLOR_CHAR + "[/insertion]", i);
int backupEndIndex = str.indexOf(ChatColor.COLOR_CHAR + "[insertion=", i + 5);
if (backupEndIndex > 0 && backupEndIndex < endIndex) {
endIndex = backupEndIndex;
}
if (endIndex == -1) {
continue;
}
TextComponent insertableText = new TextComponent();
insertableText.setInsertion(unescape(innardBase.get(1)));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
insertableText.addExtra(subComponent);
}
lastText.addExtra(insertableText);
endBracket = endIndex + "&[/insertion".length();
}
else if (innardType.equals("reset")) {
if (innardBase.get(1).length() == 1) {
char subCode = innardBase.get(1).charAt(0);
if (subCode == 'k' || subCode == 'K') {
nextText.setObfuscated(false);
}
else if (subCode == 'l' || subCode == 'L') {
nextText.setBold(false);
}
else if (subCode == 'm' || subCode == 'M') {
nextText.setStrikethrough(false);
}
else if (subCode == 'n' || subCode == 'N') {
nextText.setUnderlined(false);
}
else if (subCode == 'o' || subCode == 'O') {
nextText.setItalic(false);
}
}
else if (innardBase.get(1).equals("font")) {
nextText.setFont(base.getFont());
}
else {
nextText.setColor(base.getColor());
}
}
else if (innardType.equals("color")) {
String colorChar = innardBase.get(1);
ChatColor color = null;
if (colorChar.length() == 1) {
color = ChatColor.getByChar(colorChar.charAt(0));
}
else if (colorChar.length() == 7) {
color = ChatColor.of(colorChar);
}
else if (CoreConfiguration.debugVerbose) {
Debug.echoError("Text parse issue: cannot interpret color '" + innardBase.get(1) + "'.");
}
if (color != null) {
int endIndex = findEndIndexFor(str, "[color=", "[reset=color]", endBracket);
if (endIndex == -1) {
nextText.setColor(color);
}
else {
TextComponent colorText = new TextComponent();
colorText.setColor(color);
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), color, false)) {
colorText.addExtra(subComponent);
}
lastText.addExtra(colorText);
endBracket = endIndex + "&[reset=color".length();
}
}
}
else if (innardType.equals("gradient") && innardParts.size() == 2) {
String from = innardBase.get(1), to = innardParts.get(0), style = innardParts.get(1);
ColorTag fromColor = ColorTag.valueOf(from, CoreUtilities.noDebugContext);
ColorTag toColor = ColorTag.valueOf(to, CoreUtilities.noDebugContext);
BukkitElementExtensions.GradientStyle styleEnum = new ElementTag(style).asEnum(BukkitElementExtensions.GradientStyle.class);
if (fromColor == null || toColor == null || styleEnum == null) {
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Text parse issue: cannot interpret gradient input '" + innards + "'.");
}
}
else {
int endIndex = findNextNormalColorSymbol(str, i + 1);
if (endIndex == -1) {
endIndex = str.length();
}
String gradientText = BukkitElementExtensions.doGradient(str.substring(endBracket + 1, endIndex), fromColor, toColor, styleEnum);
for (BaseComponent subComponent : parse(gradientText, baseColor, false)) {
lastText.addExtra(subComponent);
}
endBracket = endIndex - 1;
}
}
else if (innardType.equals("font")) {
int endIndex = findEndIndexFor(str, "[font=", "[reset=font]", endBracket);
if (endIndex == -1) {
nextText.setFont(innardBase.get(1));
}
else {
TextComponent fontText = new TextComponent();
fontText.setFont(innardBase.get(1));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
fontText.addExtra(subComponent);
}
lastText.addExtra(fontText);
endBracket = endIndex + "&[reset=font".length();
}
}
else {
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Text parse issue: cannot interpret type '" + innardType + "' with " + innardParts.size() + " parts.");
}
}
}
i = endBracket;
started = endBracket + 1;
continue;
}
else if (code == 'r' || code == 'R') {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(baseColor);
}
else if (colorCodesOrReset.isMatch(code)) {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.getByChar(code));
}
else if (code == 'x') {
if (i + 13 >= chars.length) {
continue;
}
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int c = 1; c <= 6; c++) {
if (chars[i + c * 2] != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexPart = chars[i + 1 + c * 2];
if (!hexMatcher.isMatch(hexPart)) {
color = null;
break;
}
color.append(hexPart);
}
if (color == null) {
continue;
}
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.of(color.toString()));
i += 13;
started = i + 1;
continue;
}
else {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
}
else if (code == 'l' || code == 'L') {
nextText.setBold(true);
}
else if (code == 'm' || code == 'M') {
nextText.setStrikethrough(true);
}
else if (code == 'n' || code == 'N') {
nextText.setUnderlined(true);
}
else if (code == 'o' || code == 'O') {
nextText.setItalic(true);
}
}
i++;
started = i + 1;
}
else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i + 1] == 't' && chars[i + 2] == 't' && chars[i + 3] == 'p') {
String subStr = str.substring(i, i + "https://a.".length());
if (subStr.startsWith("https://") || subStr.startsWith("http://")) {
int nextSpace = CoreUtilities.indexOfAny(str, i, ' ', '\t', '\n', ChatColor.COLOR_CHAR);
if (nextSpace == -1) {
nextSpace = str.length();
}
String url = str.substring(i, nextSpace);
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = new TextComponent(lastText);
nextText.setText("");
TextComponent clickableText = new TextComponent(url);
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
lastText.addExtra(clickableText);
i = nextSpace - 1;
started = nextSpace;
continue;
}
}
}
nextText.setText(nextText.getText() + str.substring(started));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
return new BaseComponent[] { cleanBase ? root : base };
}
}