|
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; |
@@ -39,103 +40,18 @@ public static String translateColorCodes(@NotNull String text){ |
39 | 40 | * @return the TextComponent with hex colors and regular colors |
40 | 41 | */ |
41 | 42 | public static TextComponent translateColorCodesToTextComponent(@NotNull String text){ |
| 43 | + //This is done solely to ensure hex color codes are in the format |
| 44 | + //fromLegacyText expects: |
| 45 | + //&#FF0000 -> &x&f&f&0&0&0&0 |
| 46 | + String colored = translateColorCodes(text); |
42 | 47 |
|
43 | | - String[] texts = text.split(String.format(WITH_DELIMITER, "&")); |
| 48 | + TextComponent base = new TextComponent(); |
| 49 | + BaseComponent[] converted = TextComponent.fromLegacyText(colored); |
44 | 50 |
|
45 | | - ComponentBuilder builder = new ComponentBuilder(); |
46 | | - |
47 | | - for (int i = 0; i < texts.length; i++){ |
48 | | - TextComponent subComponent = new TextComponent(); |
49 | | - if (texts[i].equalsIgnoreCase("&")){ |
50 | | - //get the next string |
51 | | - i++; |
52 | | - if (texts[i].charAt(0) == '#'){ |
53 | | - subComponent.setText(texts[i].substring(7)); |
54 | | - subComponent.setColor(net.md_5.bungee.api.ChatColor.of(texts[i].substring(0, 7))); |
55 | | - }else{ |
56 | | - if (texts[i].length() > 1){ |
57 | | - subComponent.setText(texts[i].substring(1)); |
58 | | - }else{ |
59 | | - subComponent.setText(" "); |
60 | | - } |
61 | | - |
62 | | - switch (texts[i].charAt(0)){ |
63 | | - case '0': |
64 | | - subComponent.setColor(ChatColor.BLACK); |
65 | | - break; |
66 | | - case '1': |
67 | | - subComponent.setColor(ChatColor.DARK_BLUE); |
68 | | - break; |
69 | | - case '2': |
70 | | - subComponent.setColor(ChatColor.DARK_GREEN); |
71 | | - break; |
72 | | - case '3': |
73 | | - subComponent.setColor(ChatColor.DARK_AQUA); |
74 | | - break; |
75 | | - case '4': |
76 | | - subComponent.setColor(ChatColor.DARK_RED); |
77 | | - break; |
78 | | - case '5': |
79 | | - subComponent.setColor(ChatColor.DARK_PURPLE); |
80 | | - break; |
81 | | - case '6': |
82 | | - subComponent.setColor(ChatColor.GOLD); |
83 | | - break; |
84 | | - case '7': |
85 | | - subComponent.setColor(ChatColor.GRAY); |
86 | | - break; |
87 | | - case '8': |
88 | | - subComponent.setColor(ChatColor.DARK_GRAY); |
89 | | - break; |
90 | | - case '9': |
91 | | - subComponent.setColor(ChatColor.BLUE); |
92 | | - break; |
93 | | - case 'a': |
94 | | - subComponent.setColor(ChatColor.GREEN); |
95 | | - break; |
96 | | - case 'b': |
97 | | - subComponent.setColor(ChatColor.AQUA); |
98 | | - break; |
99 | | - case 'c': |
100 | | - subComponent.setColor(ChatColor.RED); |
101 | | - break; |
102 | | - case 'd': |
103 | | - subComponent.setColor(ChatColor.LIGHT_PURPLE); |
104 | | - break; |
105 | | - case 'e': |
106 | | - subComponent.setColor(ChatColor.YELLOW); |
107 | | - break; |
108 | | - case 'f': |
109 | | - subComponent.setColor(ChatColor.WHITE); |
110 | | - break; |
111 | | - case 'k': |
112 | | - subComponent.setObfuscated(true); |
113 | | - break; |
114 | | - case 'l': |
115 | | - subComponent.setBold(true); |
116 | | - break; |
117 | | - case 'm': |
118 | | - subComponent.setStrikethrough(true); |
119 | | - break; |
120 | | - case 'n': |
121 | | - subComponent.setUnderlined(true); |
122 | | - break; |
123 | | - case 'o': |
124 | | - subComponent.setItalic(true); |
125 | | - break; |
126 | | - case 'r': |
127 | | - subComponent.setColor(ChatColor.RESET); |
128 | | - break; |
129 | | - } |
130 | | - |
131 | | - } |
132 | | - builder.append(subComponent); |
133 | | - }else{ |
134 | | - builder.append(texts[i]); |
135 | | - } |
| 51 | + for(BaseComponent comp : converted) { |
| 52 | + base.addExtra(comp); |
136 | 53 | } |
137 | 54 |
|
138 | | - return new TextComponent(builder.create()); |
139 | | - |
| 55 | + return base; |
140 | 56 | } |
141 | 57 | } |
0 commit comments