|
1 | 1 | package me.kodysimpson.simpapi.colors; |
2 | 2 |
|
3 | 3 | import net.md_5.bungee.api.ChatColor; |
| 4 | +import net.md_5.bungee.api.chat.BaseComponent; |
4 | 5 | import net.md_5.bungee.api.chat.ComponentBuilder; |
5 | 6 | import net.md_5.bungee.api.chat.TextComponent; |
6 | 7 | import org.jetbrains.annotations.NotNull; |
7 | 8 |
|
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
8 | 12 | public class ColorTranslator { |
9 | 13 |
|
10 | | - static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; |
| 14 | + private static final Pattern HEX_PATTERN = Pattern.compile("(&#[0-9a-fA-F]{6})"); |
| 15 | + |
| 16 | + @Deprecated |
| 17 | + public static final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; |
11 | 18 |
|
12 | 19 | /** |
13 | 20 | * @param text The string of text to apply color/effects to |
14 | 21 | * @return Returns a string of text with color/effects applied |
15 | 22 | */ |
16 | 23 | public static String translateColorCodes(@NotNull String text){ |
17 | | - |
18 | | - String[] texts = text.split(String.format(WITH_DELIMITER, "&")); |
19 | | - |
20 | | - StringBuilder finalText = new StringBuilder(); |
21 | | - |
22 | | - for (int i = 0; i < texts.length; i++){ |
23 | | - if (texts[i].equalsIgnoreCase("&")){ |
24 | | - //get the next string |
25 | | - i++; |
26 | | - if (texts[i].charAt(0) == '#'){ |
27 | | - finalText.append(ChatColor.of(texts[i].substring(0, 7))).append(texts[i].substring(7)); |
28 | | - }else{ |
29 | | - finalText.append(ChatColor.translateAlternateColorCodes('&', "&" + texts[i])); |
30 | | - } |
31 | | - }else{ |
32 | | - finalText.append(texts[i]); |
33 | | - } |
| 24 | + //good thing we're stuck on java 8, which means we can't use this (: |
| 25 | + // String hexColored = HEX_PATTERN.matcher(text) |
| 26 | + // .replaceAll(match -> "" + ChatColor.of(match.group(1))); |
| 27 | + Matcher matcher = HEX_PATTERN.matcher(text); |
| 28 | + StringBuffer sb = new StringBuffer(); |
| 29 | + while (matcher.find()) { |
| 30 | + String hex = matcher.group(1).substring(1); |
| 31 | + matcher.appendReplacement(sb, "" + ChatColor.of(hex)); |
34 | 32 | } |
| 33 | + matcher.appendTail(sb); |
35 | 34 |
|
36 | | - return finalText.toString(); |
| 35 | + String hexColored = sb.toString(); |
| 36 | + |
| 37 | + return ChatColor.translateAlternateColorCodes('&', hexColored); |
37 | 38 | } |
38 | 39 |
|
39 | 40 | /** |
40 | 41 | * @param text The text with color codes that you want to turn into a TextComponent |
41 | 42 | * @return the TextComponent with hex colors and regular colors |
42 | 43 | */ |
43 | 44 | public static TextComponent translateColorCodesToTextComponent(@NotNull String text){ |
| 45 | + //This is done solely to ensure hex color codes are in the format |
| 46 | + //fromLegacyText expects: |
| 47 | + //&#FF0000 -> &x&f&f&0&0&0&0 |
| 48 | + String colored = translateColorCodes(text); |
44 | 49 |
|
45 | | - String[] texts = text.split(String.format(WITH_DELIMITER, "&")); |
46 | | - |
47 | | - ComponentBuilder builder = new ComponentBuilder(); |
48 | | - |
49 | | - for (int i = 0; i < texts.length; i++){ |
50 | | - TextComponent subComponent = new TextComponent(); |
51 | | - if (texts[i].equalsIgnoreCase("&")){ |
52 | | - //get the next string |
53 | | - i++; |
54 | | - if (texts[i].charAt(0) == '#'){ |
55 | | - subComponent.setText(texts[i].substring(7)); |
56 | | - subComponent.setColor(net.md_5.bungee.api.ChatColor.of(texts[i].substring(0, 7))); |
57 | | - }else{ |
58 | | - if (texts[i].length() > 1){ |
59 | | - subComponent.setText(texts[i].substring(1)); |
60 | | - }else{ |
61 | | - subComponent.setText(" "); |
62 | | - } |
| 50 | + TextComponent base = new TextComponent(); |
| 51 | + BaseComponent[] converted = TextComponent.fromLegacyText(colored); |
63 | 52 |
|
64 | | - switch (texts[i].charAt(0)){ |
65 | | - case '0': |
66 | | - subComponent.setColor(ChatColor.BLACK); |
67 | | - break; |
68 | | - case '1': |
69 | | - subComponent.setColor(ChatColor.DARK_BLUE); |
70 | | - break; |
71 | | - case '2': |
72 | | - subComponent.setColor(ChatColor.DARK_GREEN); |
73 | | - break; |
74 | | - case '3': |
75 | | - subComponent.setColor(ChatColor.DARK_AQUA); |
76 | | - break; |
77 | | - case '4': |
78 | | - subComponent.setColor(ChatColor.DARK_RED); |
79 | | - break; |
80 | | - case '5': |
81 | | - subComponent.setColor(ChatColor.DARK_PURPLE); |
82 | | - break; |
83 | | - case '6': |
84 | | - subComponent.setColor(ChatColor.GOLD); |
85 | | - break; |
86 | | - case '7': |
87 | | - subComponent.setColor(ChatColor.GRAY); |
88 | | - break; |
89 | | - case '8': |
90 | | - subComponent.setColor(ChatColor.DARK_GRAY); |
91 | | - break; |
92 | | - case '9': |
93 | | - subComponent.setColor(ChatColor.BLUE); |
94 | | - break; |
95 | | - case 'a': |
96 | | - subComponent.setColor(ChatColor.GREEN); |
97 | | - break; |
98 | | - case 'b': |
99 | | - subComponent.setColor(ChatColor.AQUA); |
100 | | - break; |
101 | | - case 'c': |
102 | | - subComponent.setColor(ChatColor.RED); |
103 | | - break; |
104 | | - case 'd': |
105 | | - subComponent.setColor(ChatColor.LIGHT_PURPLE); |
106 | | - break; |
107 | | - case 'e': |
108 | | - subComponent.setColor(ChatColor.YELLOW); |
109 | | - break; |
110 | | - case 'f': |
111 | | - subComponent.setColor(ChatColor.WHITE); |
112 | | - break; |
113 | | - case 'k': |
114 | | - subComponent.setObfuscated(true); |
115 | | - break; |
116 | | - case 'l': |
117 | | - subComponent.setBold(true); |
118 | | - break; |
119 | | - case 'm': |
120 | | - subComponent.setStrikethrough(true); |
121 | | - break; |
122 | | - case 'n': |
123 | | - subComponent.setUnderlined(true); |
124 | | - break; |
125 | | - case 'o': |
126 | | - subComponent.setItalic(true); |
127 | | - break; |
128 | | - case 'r': |
129 | | - subComponent.setColor(ChatColor.RESET); |
130 | | - break; |
131 | | - } |
132 | | - |
133 | | - } |
134 | | - builder.append(subComponent); |
135 | | - }else{ |
136 | | - builder.append(texts[i]); |
137 | | - } |
| 53 | + for(BaseComponent comp : converted) { |
| 54 | + base.addExtra(comp); |
138 | 55 | } |
139 | 56 |
|
140 | | - return new TextComponent(builder.create()); |
141 | | - |
| 57 | + return base; |
142 | 58 | } |
143 | 59 | } |
0 commit comments