7
7
import net .kyori .adventure .text .Component ;
8
8
import net .kyori .adventure .text .TextComponent ;
9
9
import net .kyori .adventure .text .TranslatableComponent ;
10
- import net .kyori .adventure .text .format .NamedTextColor ;
11
10
import net .kyori .adventure .text .format .Style ;
11
+ import net .kyori .adventure .text .format .TextColor ;
12
12
import net .kyori .adventure .text .format .TextDecoration ;
13
13
import org .jetbrains .annotations .Nullable ;
14
- import org .w3c .dom .Text ;
15
14
16
15
import java .io .InputStream ;
17
16
import java .io .InputStreamReader ;
@@ -28,6 +27,7 @@ public class Utils {
28
27
public static String getFullText (TextComponent message , boolean colored ) {
29
28
if (message .children ().size () > 0 ) {
30
29
StringBuilder text = new StringBuilder ();
30
+ text .append (parseColor (message , colored ));
31
31
for (Component child : message .children ()) {
32
32
if (child instanceof TextComponent ) {
33
33
text .append (getFullText ((TextComponent ) child , colored ));
@@ -38,15 +38,7 @@ public static String getFullText(TextComponent message, boolean colored) {
38
38
}
39
39
return text .toString ();
40
40
} else {
41
- String out = message .content ();
42
- if (colored ) {
43
- if (message .style ().color () != null ) {
44
- List <Attribute > formats = new ArrayList <>(getFormats (message .style ().decorations ()));
45
- formats .add (getColor ((NamedTextColor ) message .style ().color ()));
46
- out = colorize (out , formats .toArray (new Attribute [0 ]));
47
- }
48
- }
49
- return out ;
41
+ return parseColor (message , colored );
50
42
}
51
43
}
52
44
@@ -55,120 +47,57 @@ public static String getFullText(TextComponent message, boolean colored) {
55
47
* From 1.19.1 the protocol does not include sender's information in the message.
56
48
* Thus, we need to merge two TextComponent (sender and message) and generate a text that consists of sent message.
57
49
* This means using old getFullText will just send the message. Which does NOT contain username who sent this message
58
- * @param sender The TextComponent for sender.
50
+ *
51
+ * @param sender The TextComponent for sender.
59
52
* @param message The TextComponent for message.
60
53
* @param colored Whether if this output was colored or not.
61
54
* @return Returns String in "Username : Message" format.
62
55
*/
63
56
public static String getFullText (@ Nullable TextComponent sender , TextComponent message , boolean colored ) {
64
- if (colored ) { // Check if we are using colors.
57
+ if (colored ) {
65
58
Style messageStyle = message .style ();
66
59
StringBuilder outString = new StringBuilder ();
67
60
68
61
if (sender != null ) {
69
62
Style senderStyle = sender .style ();
70
63
if (senderStyle .color () != null ) { // Generate sender text with color.
71
- NamedTextColor color = (NamedTextColor ) senderStyle .color ();
72
- outString .append (colorize (sender .content (), getColor (color )));
64
+ outString .append (colorizeText (sender .content (), senderStyle .color ()));
73
65
} else {
74
66
outString .append (sender .content ());
75
67
}
76
68
outString .append (" : " ); // Add delimiter as : between sender and message.
77
69
}
78
70
79
- if (messageStyle .color () != null ) { // Generate message text with color.
80
- NamedTextColor color = (NamedTextColor ) messageStyle .color ();
81
- outString .append (colorize (message .content (), getColor (color )));
71
+ if (messageStyle .color () != null && sender != null ) { // Generate message text with color.
72
+ outString .append (colorizeText (sender .content (), messageStyle .color ()));
82
73
} else {
83
74
outString .append (message .content ());
84
75
}
85
76
86
77
return outString .toString ();
87
- } else { // When color formatting was disabled.
78
+ } else {
88
79
if (sender == null )
89
80
return message .content ();
90
81
else
91
82
return sender .content () + " : " + message .content ();
92
83
}
93
84
}
94
85
95
- /**
96
- * A static function that gets full text from text components.
97
- * From 1.19.1 the protocol does not include sender's information in the message.
98
- * Sometimes the message as component is null. When this happens, this method will show message as String.
99
- * Since this is just a String being represented, this will not have formatting on String.
100
- * @param sender The TextComponent for sender.
101
- * @param message The TextComponent for message.
102
- * @param colored Whether if this output was colored or not.
103
- * @return Returns String in "Username : Message" format.
104
- */
105
- public static String getFullText (@ Nullable TextComponent sender , String message , boolean colored ) {
106
- if (colored ) { // Check if we are using colors.
107
- StringBuilder outString = new StringBuilder ();
108
-
109
- if (sender != null ) {
110
- Style senderStyle = sender .style ();
111
- if (senderStyle .color () != null ) { // Generate sender text with color.
112
- NamedTextColor color = (NamedTextColor ) senderStyle .color ();
113
- outString .append (colorize (sender .content (), getColor (color )));
114
- } else {
115
- outString .append (sender .content ());
116
- }
117
- outString .append (" : " ); // Add delimiter as : between sender and message.
86
+ private static String parseColor (TextComponent message , boolean colored ) {
87
+ String out = message .content ();
88
+ if (colored ) {
89
+ if (message .style ().color () != null ) {
90
+ List <Attribute > formats = new ArrayList <>(getFormats (message .style ().decorations ()));
91
+ TextColor color = message .style ().color ();
92
+ formats .add (Attribute .TEXT_COLOR (color .red (), color .green (), color .blue ()));
93
+ out = colorize (out , formats .toArray (new Attribute [0 ]));
118
94
}
119
-
120
- outString .append (message );
121
- return outString .toString ();
122
- } else { // When color formatting was disabled.
123
- if (sender == null )
124
- return message ;
125
- else
126
- return sender .content () + " : " + message ;
127
95
}
96
+ return out ;
128
97
}
129
98
130
- public static Attribute getColor (NamedTextColor color ) {
131
- switch (color .toString ()) {
132
- case "red" :
133
- return Attribute .BRIGHT_RED_TEXT ();
134
- case "dark_red" :
135
- return Attribute .RED_TEXT ();
136
-
137
- case "blue" :
138
- return Attribute .BRIGHT_BLUE_TEXT ();
139
- case "dark_blue" :
140
- return Attribute .BLUE_TEXT ();
141
-
142
- case "white" :
143
- return Attribute .BRIGHT_WHITE_TEXT ();
144
- case "black" :
145
- return Attribute .BLACK_TEXT ();
146
- case "gray" :
147
- return Attribute .WHITE_TEXT ();
148
- case "dark_gray" :
149
- return Attribute .BRIGHT_BLACK_TEXT ();
150
-
151
- case "aqua" :
152
- return Attribute .BRIGHT_CYAN_TEXT ();
153
- case "dark_aqua" :
154
- return Attribute .CYAN_TEXT ();
155
-
156
- case "yellow" :
157
- return Attribute .BRIGHT_YELLOW_TEXT ();
158
- case "gold" :
159
- return Attribute .YELLOW_TEXT ();
160
-
161
- case "green" :
162
- return Attribute .BRIGHT_GREEN_TEXT ();
163
- case "dark_green" :
164
- return Attribute .GREEN_TEXT ();
165
-
166
- case "purple" :
167
- return Attribute .BRIGHT_MAGENTA_TEXT ();
168
- case "dark_purple" :
169
- return Attribute .MAGENTA_TEXT ();
170
- }
171
- return Attribute .NONE ();
99
+ private static String colorizeText (String text , TextColor color ) {
100
+ return colorize (text , Attribute .TEXT_COLOR (color .red (), color .green (), color .blue ()));
172
101
}
173
102
174
103
public static List <Attribute > getFormats (Map <TextDecoration , TextDecoration .State > decorations ) {
@@ -191,7 +120,6 @@ public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.Stat
191
120
return list ;
192
121
}
193
122
194
-
195
123
public static String translate (TranslatableComponent message ) {
196
124
if (lang == null ) {
197
125
try {
@@ -217,8 +145,7 @@ public static String translate(TranslatableComponent message) {
217
145
for (Component component : message .args ()) {
218
146
if (component instanceof TranslatableComponent ) {
219
147
placeholders .add (translate ((TranslatableComponent ) component ));
220
- }
221
- else if (component instanceof TextComponent ) {
148
+ } else if (component instanceof TextComponent ) {
222
149
placeholders .add (getFullText ((TextComponent ) component , false ));
223
150
}
224
151
}
@@ -228,5 +155,4 @@ else if (component instanceof TextComponent) {
228
155
return base ;
229
156
}
230
157
}
231
-
232
158
}
0 commit comments