|
8 | 8 | import net.kyori.adventure.text.TextComponent;
|
9 | 9 | import net.kyori.adventure.text.TranslatableComponent;
|
10 | 10 | import net.kyori.adventure.text.format.NamedTextColor;
|
| 11 | +import net.kyori.adventure.text.format.Style; |
11 | 12 | import net.kyori.adventure.text.format.TextDecoration;
|
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | +import org.w3c.dom.Text; |
12 | 15 |
|
13 | 16 | import java.io.InputStream;
|
14 | 17 | import java.io.InputStreamReader;
|
@@ -45,7 +48,83 @@ public static String getFullText(TextComponent message, boolean colored) {
|
45 | 48 | }
|
46 | 49 | return out;
|
47 | 50 | }
|
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A static function that gets full text from text components. |
| 55 | + * From 1.19.1 the protocol does not include sender's information in the message. |
| 56 | + * Thus, we need to merge two TextComponent (sender and message) and generate a text that consists of sent message. |
| 57 | + * 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. |
| 59 | + * @param message The TextComponent for message. |
| 60 | + * @param colored Whether if this output was colored or not. |
| 61 | + * @return Returns String in "Username : Message" format. |
| 62 | + */ |
| 63 | + public static String getFullText(@Nullable TextComponent sender, TextComponent message, boolean colored) { |
| 64 | + if (colored) { // Check if we are using colors. |
| 65 | + Style messageStyle = message.style(); |
| 66 | + StringBuilder outString = new StringBuilder(); |
| 67 | + |
| 68 | + if (sender != null) { |
| 69 | + Style senderStyle = sender.style(); |
| 70 | + if (senderStyle.color() != null) { // Generate sender text with color. |
| 71 | + NamedTextColor color = (NamedTextColor) senderStyle.color(); |
| 72 | + outString.append(colorize(sender.content(), getColor(color))); |
| 73 | + } else { |
| 74 | + outString.append(sender.content()); |
| 75 | + } |
| 76 | + outString.append(" : "); // Add delimiter as : between sender and message. |
| 77 | + } |
48 | 78 |
|
| 79 | + if (messageStyle.color() != null) { // Generate message text with color. |
| 80 | + NamedTextColor color = (NamedTextColor) messageStyle.color(); |
| 81 | + outString.append(colorize(message.content(), getColor(color))); |
| 82 | + } else { |
| 83 | + outString.append(message.content()); |
| 84 | + } |
| 85 | + |
| 86 | + return outString.toString(); |
| 87 | + } else { // When color formatting was disabled. |
| 88 | + if (sender == null) |
| 89 | + return message.content(); |
| 90 | + else |
| 91 | + return sender.content() + " : " + message.content(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 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. |
| 118 | + } |
| 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 | + } |
49 | 128 | }
|
50 | 129 |
|
51 | 130 | public static Attribute getColor(NamedTextColor color) {
|
|
0 commit comments