Important chat bug fixes #73

Merged
Norbiros merged 1 commits from fix/chat into main 2023-08-14 20:48:38 +00:00
2 changed files with 25 additions and 99 deletions

No files matched your search

@@ -31,9 +31,9 @@ public class MainListener implements SessionListener {
// Sometimes the message's body gets null. // Sometimes the message's body gets null.
// For example, some commands like /say makes the message content as null. // For example, some commands like /say makes the message content as null.
// However, the message exists as in getMessagePlain(), thus can retrieve message using the method. // However, the message exists as in getMessagePlain(), thus can retrieve message using the method.
if (message == null) { // When this message was null. if (message == null) {
Log.chat(Utils.getFullText((TextComponent) sender, clientboundPlayerChatPacket.getContent(), Main.coloredChat)); Log.chat(Utils.getFullText((TextComponent) sender, Component.text(clientboundPlayerChatPacket.getContent()), Main.coloredChat));
} else { // When message exists. } else {
Log.chat(Utils.getFullText((TextComponent) sender, (TextComponent) message, Main.coloredChat)); Log.chat(Utils.getFullText((TextComponent) sender, (TextComponent) message, Main.coloredChat));
} }
chatPrintedOut = true; chatPrintedOut = true;
+22 -96
View File
@@ -7,11 +7,10 @@ import com.google.gson.JsonParser;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Text;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -28,6 +27,7 @@ public class Utils {
public static String getFullText(TextComponent message, boolean colored) { public static String getFullText(TextComponent message, boolean colored) {
if (message.children().size() > 0) { if (message.children().size() > 0) {
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
text.append(parseColor(message, colored));
for (Component child : message.children()) { for (Component child : message.children()) {
if (child instanceof TextComponent) { if (child instanceof TextComponent) {
text.append(getFullText((TextComponent) child, colored)); text.append(getFullText((TextComponent) child, colored));
@@ -38,15 +38,7 @@ public class Utils {
} }
return text.toString(); return text.toString();
} else { } else {
String out = message.content(); return parseColor(message, colored);
if (colored) {
if (message.style().color() != null) {
List<Attribute> formats = new ArrayList<>(getFormats(message.style().decorations()));
formats.add(getColor((NamedTextColor) message.style().color()));
out = colorize(out, formats.toArray(new Attribute[0]));
}
}
return out;
} }
} }
@@ -55,36 +47,35 @@ public class Utils {
* From 1.19.1 the protocol does not include sender's information in the message. * From 1.19.1 the protocol does not include sender's information in the message.
* Thus, we need to merge two TextComponent (sender and message) and generate a text that consists of sent message. * Thus, we need to merge two TextComponent (sender and message) and generate a text that consists of sent message.
* This means using old getFullText will just send the message. Which does NOT contain username who sent this message * This means using old getFullText will just send the message. Which does NOT contain username who sent this message
* @param sender The TextComponent for sender. *
* @param sender The TextComponent for sender.
* @param message The TextComponent for message. * @param message The TextComponent for message.
* @param colored Whether if this output was colored or not. * @param colored Whether if this output was colored or not.
* @return Returns String in "Username : Message" format. * @return Returns String in "Username : Message" format.
*/ */
public static String getFullText(@Nullable TextComponent sender, TextComponent message, boolean colored) { public static String getFullText(@Nullable TextComponent sender, TextComponent message, boolean colored) {
if (colored) { // Check if we are using colors. if (colored) {
Style messageStyle = message.style(); Style messageStyle = message.style();
StringBuilder outString = new StringBuilder(); StringBuilder outString = new StringBuilder();
if (sender != null) { if (sender != null) {
Style senderStyle = sender.style(); Style senderStyle = sender.style();
if (senderStyle.color() != null) { // Generate sender text with color. if (senderStyle.color() != null) { // Generate sender text with color.
NamedTextColor color = (NamedTextColor) senderStyle.color(); outString.append(colorizeText(sender.content(), senderStyle.color()));
outString.append(colorize(sender.content(), getColor(color)));
} else { } else {
outString.append(sender.content()); outString.append(sender.content());
} }
outString.append(" : "); // Add delimiter as : between sender and message. outString.append(" : "); // Add delimiter as : between sender and message.
} }
if (messageStyle.color() != null) { // Generate message text with color. if (messageStyle.color() != null && sender != null) { // Generate message text with color.
NamedTextColor color = (NamedTextColor) messageStyle.color(); outString.append(colorizeText(sender.content(), messageStyle.color()));
outString.append(colorize(message.content(), getColor(color)));
} else { } else {
outString.append(message.content()); outString.append(message.content());
} }
return outString.toString(); return outString.toString();
} else { // When color formatting was disabled. } else {
if (sender == null) if (sender == null)
return message.content(); return message.content();
else else
@@ -92,83 +83,21 @@ public class Utils {
} }
} }
/** private static String parseColor(TextComponent message, boolean colored) {
* A static function that gets full text from text components. String out = message.content();
* From 1.19.1 the protocol does not include sender's information in the message. if (colored) {
* Sometimes the message as component is null. When this happens, this method will show message as String. if (message.style().color() != null) {
* Since this is just a String being represented, this will not have formatting on String. List<Attribute> formats = new ArrayList<>(getFormats(message.style().decorations()));
* @param sender The TextComponent for sender. TextColor color = message.style().color();
* @param message The TextComponent for message. formats.add(Attribute.TEXT_COLOR(color.red(), color.green(), color.blue()));
* @param colored Whether if this output was colored or not. out = colorize(out, formats.toArray(new Attribute[0]));
* @return Returns String in "Username : Message" format.
*/
public static String getFullText(@Nullable TextComponent sender, String message, boolean colored) {
if (colored) { // Check if we are using colors.
StringBuilder outString = new StringBuilder();
if (sender != null) {
Style senderStyle = sender.style();
if (senderStyle.color() != null) { // Generate sender text with color.
NamedTextColor color = (NamedTextColor) senderStyle.color();
outString.append(colorize(sender.content(), getColor(color)));
} else {
outString.append(sender.content());
}
outString.append(" : "); // Add delimiter as : between sender and message.
} }
outString.append(message);
return outString.toString();
} else { // When color formatting was disabled.
if (sender == null)
return message;
else
return sender.content() + " : " + message;
} }
return out;
} }
public static Attribute getColor(NamedTextColor color) { private static String colorizeText(String text, TextColor color) {
switch (color.toString()) { return colorize(text, Attribute.TEXT_COLOR(color.red(), color.green(), color.blue()));
case "red":
return Attribute.BRIGHT_RED_TEXT();
case "dark_red":
return Attribute.RED_TEXT();
case "blue":
return Attribute.BRIGHT_BLUE_TEXT();
case "dark_blue":
return Attribute.BLUE_TEXT();
case "white":
return Attribute.BRIGHT_WHITE_TEXT();
case "black":
return Attribute.BLACK_TEXT();
case "gray":
return Attribute.WHITE_TEXT();
case "dark_gray":
return Attribute.BRIGHT_BLACK_TEXT();
case "aqua":
return Attribute.BRIGHT_CYAN_TEXT();
case "dark_aqua":
return Attribute.CYAN_TEXT();
case "yellow":
return Attribute.BRIGHT_YELLOW_TEXT();
case "gold":
return Attribute.YELLOW_TEXT();
case "green":
return Attribute.BRIGHT_GREEN_TEXT();
case "dark_green":
return Attribute.GREEN_TEXT();
case "purple":
return Attribute.BRIGHT_MAGENTA_TEXT();
case "dark_purple":
return Attribute.MAGENTA_TEXT();
}
return Attribute.NONE();
} }
public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.State> decorations) { public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.State> decorations) {
@@ -191,7 +120,6 @@ public class Utils {
return list; return list;
} }
public static String translate(TranslatableComponent message) { public static String translate(TranslatableComponent message) {
if (lang == null) { if (lang == null) {
try { try {
@@ -217,8 +145,7 @@ public class Utils {
for (Component component : message.args()) { for (Component component : message.args()) {
if (component instanceof TranslatableComponent) { if (component instanceof TranslatableComponent) {
placeholders.add(translate((TranslatableComponent) component)); placeholders.add(translate((TranslatableComponent) component));
} } else if (component instanceof TextComponent) {
else if (component instanceof TextComponent) {
placeholders.add(getFullText((TextComponent) component, false)); placeholders.add(getFullText((TextComponent) component, false));
} }
} }
@@ -228,5 +155,4 @@ public class Utils {
return base; return base;
} }
} }
} }