Skip to content

Commit b3ccf82

Browse files
authored
WrappedServerPing: Properly translate MotD to components for RGB colors (#1152)
In previous Minecraft versions, using WrappedServerPing.setMotD(String) behaved exactly like using Bukkit's ServerListPingEvent.setMotd(String). With the addition of RGB colors in Minecraft 1.16, Spigot's ServerListPingEvent was patched to translate the MotD string to the chat component equivalent to make it possible to use RGB colors in MotDs. In general, using raw legacy color codes (e.g. §c) within a (JSON) text component tends to cause weird issues on newer Minecraft versions, so it's better to translate them to the JSON equivalents on the server. However, the WrappedServerPing implementation in ProtocolLib was never updated with the same change, which makes it behave differently from Spigot's ServerListPingEvent now. Using ServerListPingEvent RGB color codes work, using ProtocolLib they do not work. To fix this, this commit changes WrappedServerPing.setMotD(String) to use the same method as Spigot for translating the legacy text to the JSON/chat component equivalent. This allows for example ServerListPlus to use Spigot's RGB color codes (e.g. &x&7&9&b&8&f&fHello) without requiring any changes in ServerListPlus.
1 parent 0c01a11 commit b3ccf82

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
5050
}
5151

5252
// Get a component from a standard Minecraft message
53-
CONSTRUCT_COMPONENT = Accessors.getMethodAccessor(MinecraftReflection.getCraftChatMessage(), "fromString", String.class);
53+
CONSTRUCT_COMPONENT = Accessors.getMethodAccessor(MinecraftReflection.getCraftChatMessage(), "fromString", String.class, boolean.class);
5454

5555
// And the component text constructor
5656
CONSTRUCT_TEXT_COMPONENT = Accessors.getConstructorAccessor(MinecraftReflection.getChatComponentTextClass(), String.class);
@@ -95,6 +95,9 @@ public static WrappedChatComponent fromJson(String json) {
9595

9696
/**
9797
* Construct a wrapper around a new text chat component with the given text.
98+
* <p>
99+
* Note: {@link #fromLegacyText(String)} is preferred for text that contains
100+
* legacy formatting codes since it will translate them to the JSON equivalent.
98101
* @param text - the text of the text chat component.
99102
* @return The wrapper around the new chat component.
100103
*/
@@ -111,7 +114,7 @@ public static WrappedChatComponent fromText(String text) {
111114
* @return The equivalent chat components.
112115
*/
113116
public static WrappedChatComponent[] fromChatMessage(String message) {
114-
Object[] components = (Object[]) CONSTRUCT_COMPONENT.invoke(null, message);
117+
Object[] components = (Object[]) CONSTRUCT_COMPONENT.invoke(null, message, false);
115118
WrappedChatComponent[] result = new WrappedChatComponent[components.length];
116119

117120
for (int i = 0; i < components.length; i++) {
@@ -120,6 +123,18 @@ public static WrappedChatComponent[] fromChatMessage(String message) {
120123
return result;
121124
}
122125

126+
/**
127+
* Construct a single chat component from a standard Minecraft message
128+
* (with legacy formatting codes), preserving multiple lines.
129+
* @param message - the message.
130+
* @return The equivalent chat component.
131+
*/
132+
public static WrappedChatComponent fromLegacyText(String message) {
133+
// With keepNewlines = true (second parameter), only one component is returned
134+
Object[] components = (Object[]) CONSTRUCT_COMPONENT.invoke(null, message, true);
135+
return fromHandle(components[0]);
136+
}
137+
123138
/**
124139
* Retrieve a copy of this component as a JSON string.
125140
* <p>

src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void setMotD(WrappedChatComponent description) {
161161
* @param message - the message.
162162
*/
163163
public void setMotD(String message) {
164-
setMotD(WrappedChatComponent.fromText(message));
164+
setMotD(WrappedChatComponent.fromLegacyText(message));
165165
}
166166

167167
/**

0 commit comments

Comments
 (0)