Merge pull request #73 from Norbiros/fix/chat

Important chat bug fixes
This commit was merged in pull request #73.
This commit is contained in:
CreeperMaxCZ authored and GitHub committed 2023-08-14 22:48:38 +02:00
commit 3311f0889a
2 files changed
+25 -99

No files matched your search

@@ -31,9 +31,9 @@ public class MainListener implements SessionListener {
// Sometimes the message's body gets 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.
if (message == null) { // When this message was null.
Log.chat(Utils.getFullText((TextComponent) sender, clientboundPlayerChatPacket.getContent(), Main.coloredChat));
} else { // When message exists.
if (message == null) {
Log.chat(Utils.getFullText((TextComponent) sender, Component.text(clientboundPlayerChatPacket.getContent()), Main.coloredChat));
} else {
Log.chat(Utils.getFullText((TextComponent) sender, (TextComponent) message, Main.coloredChat));
}
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.TextComponent;
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.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Text;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -28,6 +27,7 @@ public class Utils {
public static String getFullText(TextComponent message, boolean colored) {
if (message.children().size() > 0) {
StringBuilder text = new StringBuilder();
text.append(parseColor(message, colored));
for (Component child : message.children()) {
if (child instanceof TextComponent) {
text.append(getFullText((TextComponent) child, colored));
@@ -38,15 +38,7 @@ public class Utils {
}
return text.toString();
} else {
String out = message.content();
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;
return parseColor(message, colored);
}
}
@@ -55,36 +47,35 @@ public class Utils {
* 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.
* 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 colored Whether if this output was colored or not.
* @return Returns String in "Username : Message" format.
*/
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();
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)));
outString.append(colorizeText(sender.content(), senderStyle.color()));
} else {
outString.append(sender.content());
}
outString.append(" : "); // Add delimiter as : between sender and message.
}
if (messageStyle.color() != null) { // Generate message text with color.
NamedTextColor color = (NamedTextColor) messageStyle.color();
outString.append(colorize(message.content(), getColor(color)));
if (messageStyle.color() != null && sender != null) { // Generate message text with color.
outString.append(colorizeText(sender.content(), messageStyle.color()));
} else {
outString.append(message.content());
}
return outString.toString();
} else { // When color formatting was disabled.
} else {
if (sender == null)
return message.content();
else
@@ -92,83 +83,21 @@ public class Utils {
}
}
/**
* A static function that gets full text from text components.
* From 1.19.1 the protocol does not include sender's information in the message.
* Sometimes the message as component is null. When this happens, this method will show message as String.
* Since this is just a String being represented, this will not have formatting on String.
* @param sender The TextComponent for sender.
* @param message The TextComponent for message.
* @param colored Whether if this output was colored or not.
* @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.
private static String parseColor(TextComponent message, boolean colored) {
String out = message.content();
if (colored) {
if (message.style().color() != null) {
List<Attribute> formats = new ArrayList<>(getFormats(message.style().decorations()));
TextColor color = message.style().color();
formats.add(Attribute.TEXT_COLOR(color.red(), color.green(), color.blue()));
out = colorize(out, formats.toArray(new Attribute[0]));
}
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) {
switch (color.toString()) {
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();
private static String colorizeText(String text, TextColor color) {
return colorize(text, Attribute.TEXT_COLOR(color.red(), color.green(), color.blue()));
}
public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.State> decorations) {
@@ -191,7 +120,6 @@ public class Utils {
return list;
}
public static String translate(TranslatableComponent message) {
if (lang == null) {
try {
@@ -217,8 +145,7 @@ public class Utils {
for (Component component : message.args()) {
if (component instanceof TranslatableComponent) {
placeholders.add(translate((TranslatableComponent) component));
}
else if (component instanceof TextComponent) {
} else if (component instanceof TextComponent) {
placeholders.add(getFullText((TextComponent) component, false));
}
}
@@ -228,5 +155,4 @@ public class Utils {
return base;
}
}
}