Requesting PR for 1.19.1 (and 1.19.2) #44

Merged
isu-kim merged 8 commits from main into main 2022-08-16 12:03:14 +00:00
2 changed files with 43 additions and 2 deletions
Showing only changes of commit 73c51871c9 - Show all commits

No files matched your search

@@ -21,25 +21,35 @@ public class MainListener implements SessionListener {
// Instead, they use ClientboundPlayerChatPacket and ClientboundSystemChatPacket for taking care of chat packets.
Component message = null;
Component sender = null;
boolean isMessageNull = false; // For some cases when message is null.
if (packet instanceof ClientboundPlayerChatPacket) {
ClientboundPlayerChatPacket clientboundPlayerChatPacket = ((ClientboundPlayerChatPacket) packet);
message = clientboundPlayerChatPacket.getUnsignedContent();
sender = clientboundPlayerChatPacket.getName();
isMessageNull = message == null;
} else if (packet instanceof ClientboundSystemChatPacket) {
message = ((ClientboundSystemChatPacket) packet).getContent();
}
if (message instanceof TextComponent) {
// For some commands (like /say), ClientboundPlayerChatPacket.getUnsignedContent() is null.
// However for those cases, the message is still valid in ClientboundPlayerChatPacket.getMessagePlain()
// Thus, this if condition will figure out if this case was the case and prints out message from the packet.
if (isMessageNull) { // When this message was null.
ClientboundPlayerChatPacket clientboundPlayerChatPacket = ((ClientboundPlayerChatPacket) packet);
Log.chat(Utils.getFullText((TextComponent) sender, clientboundPlayerChatPacket.getMessagePlain(), Main.coloredChat));
} else { // If this was normal case when unsigned content was not null.
if (message instanceof TextComponent) { // For normal chat packets.
// Log.chat(Utils.getFullText((TextComponent) message, Main.coloredChat)); // Use this for only messages.
Log.chat(Utils.getFullText((TextComponent) sender, (TextComponent) message, Main.coloredChat)); // Use this for messages and sender's username.
}
if (message instanceof TranslatableComponent) {
if (message instanceof TranslatableComponent) { // For system chat packets (such as user joining server).
TranslatableComponent msg = (TranslatableComponent) message;
Log.chat("[T]", Utils.translate(msg));
}
}
}
@Override
public void packetSending(PacketSendingEvent event) {
@@ -88,6 +88,37 @@ 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(TextComponent sender, String message, boolean colored) {
if (colored) { // Check if we are using colors.
Style senderStyle = sender.style();
StringBuilder outString = new StringBuilder();
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 formating was disabled.
return sender.content() + " : " + message;
}
}
public static Attribute getColor(NamedTextColor color) {
switch (color.toString()) {
case "red":