Skip to content

Commit 3311f08

Browse files
authored
Merge pull request #73 from Norbiros/fix/chat
Important chat bug fixes
2 parents c05b7ff + 896c320 commit 3311f08

File tree

2 files changed

+25
-99
lines changed

2 files changed

+25
-99
lines changed

src/main/java/me/creepermaxcz/mcbots/MainListener.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public void packetReceived(Session session, Packet packet) {
3131
// Sometimes the message's body gets null.
3232
// For example, some commands like /say makes the message content as null.
3333
// However, the message exists as in getMessagePlain(), thus can retrieve message using the method.
34-
if (message == null) { // When this message was null.
35-
Log.chat(Utils.getFullText((TextComponent) sender, clientboundPlayerChatPacket.getContent(), Main.coloredChat));
36-
} else { // When message exists.
34+
if (message == null) {
35+
Log.chat(Utils.getFullText((TextComponent) sender, Component.text(clientboundPlayerChatPacket.getContent()), Main.coloredChat));
36+
} else {
3737
Log.chat(Utils.getFullText((TextComponent) sender, (TextComponent) message, Main.coloredChat));
3838
}
3939
chatPrintedOut = true;

src/main/java/me/creepermaxcz/mcbots/Utils.java

+22-96
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import net.kyori.adventure.text.Component;
88
import net.kyori.adventure.text.TextComponent;
99
import net.kyori.adventure.text.TranslatableComponent;
10-
import net.kyori.adventure.text.format.NamedTextColor;
1110
import net.kyori.adventure.text.format.Style;
11+
import net.kyori.adventure.text.format.TextColor;
1212
import net.kyori.adventure.text.format.TextDecoration;
1313
import org.jetbrains.annotations.Nullable;
14-
import org.w3c.dom.Text;
1514

1615
import java.io.InputStream;
1716
import java.io.InputStreamReader;
@@ -28,6 +27,7 @@ public class Utils {
2827
public static String getFullText(TextComponent message, boolean colored) {
2928
if (message.children().size() > 0) {
3029
StringBuilder text = new StringBuilder();
30+
text.append(parseColor(message, colored));
3131
for (Component child : message.children()) {
3232
if (child instanceof TextComponent) {
3333
text.append(getFullText((TextComponent) child, colored));
@@ -38,15 +38,7 @@ public static String getFullText(TextComponent message, boolean colored) {
3838
}
3939
return text.toString();
4040
} 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);
5042
}
5143
}
5244

@@ -55,120 +47,57 @@ public static String getFullText(TextComponent message, boolean colored) {
5547
* From 1.19.1 the protocol does not include sender's information in the message.
5648
* Thus, we need to merge two TextComponent (sender and message) and generate a text that consists of sent message.
5749
* 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.
5952
* @param message The TextComponent for message.
6053
* @param colored Whether if this output was colored or not.
6154
* @return Returns String in "Username : Message" format.
6255
*/
6356
public static String getFullText(@Nullable TextComponent sender, TextComponent message, boolean colored) {
64-
if (colored) { // Check if we are using colors.
57+
if (colored) {
6558
Style messageStyle = message.style();
6659
StringBuilder outString = new StringBuilder();
6760

6861
if (sender != null) {
6962
Style senderStyle = sender.style();
7063
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()));
7365
} else {
7466
outString.append(sender.content());
7567
}
7668
outString.append(" : "); // Add delimiter as : between sender and message.
7769
}
7870

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()));
8273
} else {
8374
outString.append(message.content());
8475
}
8576

8677
return outString.toString();
87-
} else { // When color formatting was disabled.
78+
} else {
8879
if (sender == null)
8980
return message.content();
9081
else
9182
return sender.content() + " : " + message.content();
9283
}
9384
}
9485

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]));
11894
}
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;
12795
}
96+
return out;
12897
}
12998

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()));
172101
}
173102

174103
public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.State> decorations) {
@@ -191,7 +120,6 @@ public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.Stat
191120
return list;
192121
}
193122

194-
195123
public static String translate(TranslatableComponent message) {
196124
if (lang == null) {
197125
try {
@@ -217,8 +145,7 @@ public static String translate(TranslatableComponent message) {
217145
for (Component component : message.args()) {
218146
if (component instanceof TranslatableComponent) {
219147
placeholders.add(translate((TranslatableComponent) component));
220-
}
221-
else if (component instanceof TextComponent) {
148+
} else if (component instanceof TextComponent) {
222149
placeholders.add(getFullText((TextComponent) component, false));
223150
}
224151
}
@@ -228,5 +155,4 @@ else if (component instanceof TextComponent) {
228155
return base;
229156
}
230157
}
231-
232158
}

0 commit comments

Comments
 (0)