Skip to content

Commit 1912a9c

Browse files
committed
Fix setting ping player sample in 1.19.4
1 parent 0c6fa46 commit 1912a9c

File tree

5 files changed

+132
-93
lines changed

5 files changed

+132
-93
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.lang.reflect.Array;
1818
import java.lang.reflect.Method;
1919
import java.lang.reflect.Modifier;
20+
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.Optional;
@@ -310,4 +311,22 @@ public Class<T> getSpecificType() {
310311
}
311312
};
312313
}
314+
315+
public static <T> List<T> toList(Iterable<? extends T> iterable) {
316+
if (iterable instanceof List) {
317+
return (List<T>) iterable;
318+
}
319+
320+
List<T> result = new ArrayList<>();
321+
if (iterable instanceof Collection) {
322+
Collection<T> coll = (Collection<T>) iterable;
323+
result.addAll(coll);
324+
} else {
325+
for (T elem : iterable) {
326+
result.add(elem);
327+
}
328+
}
329+
330+
return result;
331+
}
313332
}

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,9 @@
3939
public class WrappedServerPing implements ClonableWrapper {
4040
private static final Class<?> GAME_PROFILE = MinecraftReflection.getGameProfileClass();
4141

42-
// For converting to the underlying array
43-
private static final EquivalentConverter<Iterable<? extends WrappedGameProfile>> PROFILE_CONVERT =
44-
BukkitConverters.getArrayConverter(GAME_PROFILE, BukkitConverters.getWrappedGameProfileConverter());
45-
4642
// Get profile from player
4743
private static final FieldAccessor ENTITY_HUMAN_PROFILE = Accessors.getFieldAccessor(
48-
MinecraftReflection.getEntityPlayerClass().getSuperclass(), GAME_PROFILE, true);
49-
50-
// Server ping fields
51-
private static final Class<?> SERVER_PING = MinecraftReflection.getServerPingClass();
44+
MinecraftReflection.getEntityPlayerClass().getSuperclass(), GAME_PROFILE, true);
5245

5346
private final ServerPingImpl impl;
5447

@@ -59,9 +52,6 @@ public class WrappedServerPing implements ClonableWrapper {
5952
*/
6053
public WrappedServerPing() {
6154
this.impl = newImpl();
62-
63-
resetPlayers();
64-
resetVersion();
6555
}
6656

6757
private WrappedServerPing(Object handle) {
@@ -119,18 +109,19 @@ public static WrappedServerPing fromJson(String json) {
119109

120110
/**
121111
* Retrieve the message of the day.
122-
* @return The messge of the day.
112+
* @return The message of the day.
123113
*/
124114
public WrappedChatComponent getMotD() {
125-
return WrappedChatComponent.fromHandle(impl.getMotD());
115+
Object handle = impl.getMotD();
116+
return handle != null ? WrappedChatComponent.fromHandle(handle) : null;
126117
}
127118

128119
/**
129120
* Set the message of the day.
130121
* @param description - message of the day.
131122
*/
132123
public void setMotD(WrappedChatComponent description) {
133-
impl.setMotD(description.getHandle());
124+
impl.setMotD(description != null ? description.getHandle() : null);
134125
}
135126

136127
/**
@@ -267,12 +258,7 @@ public boolean isPlayersVisible() {
267258
* @return Logged in players or an empty list if no player names will be displayed.
268259
*/
269260
public ImmutableList<WrappedGameProfile> getPlayers() {
270-
if (!isPlayersVisible())
271-
return ImmutableList.of();
272-
Object playerProfiles = impl.getPlayers();
273-
if (playerProfiles == null)
274-
return ImmutableList.of();
275-
return ImmutableList.copyOf(PROFILE_CONVERT.getSpecific(playerProfiles));
261+
return impl.getPlayers();
276262
}
277263

278264
/**
@@ -282,7 +268,7 @@ public ImmutableList<WrappedGameProfile> getPlayers() {
282268
public void setPlayers(Iterable<? extends WrappedGameProfile> profile) {
283269
if (!isPlayersVisible())
284270
resetPlayers();
285-
impl.setPlayers((profile != null) ? PROFILE_CONVERT.getGeneric(profile) : null);
271+
impl.setPlayers(profile);
286272
}
287273

288274
/**

src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.comphenix.protocol.wrappers.BukkitConverters;
1515
import com.comphenix.protocol.wrappers.WrappedChatComponent;
1616
import com.comphenix.protocol.wrappers.WrappedGameProfile;
17+
import com.comphenix.protocol.wrappers.WrappedServerPing;
18+
1719
import com.google.common.collect.ImmutableList;
1820

1921
import org.bukkit.Bukkit;
@@ -27,7 +29,7 @@
2729
* Represents a server ping packet data.
2830
* @author Kristian
2931
*/
30-
public class LegacyServerPing extends AbstractWrapper implements ServerPingImpl {
32+
public final class LegacyServerPing extends AbstractWrapper implements ServerPingImpl {
3133
private static final Class<?> GAME_PROFILE = MinecraftReflection.getGameProfileClass();
3234

3335
// For converting to the underlying array
@@ -132,32 +134,27 @@ public static LegacyServerPing fromJson(String json) {
132134

133135
/**
134136
* Retrieve the message of the day.
135-
* @return The messge of the day.
137+
* @return The message of the day.
136138
*/
137-
public WrappedChatComponent getMotD() {
138-
return WrappedChatComponent.fromHandle(DESCRIPTION.get(handle));
139+
@Override
140+
public Object getMotD() {
141+
return DESCRIPTION.get(handle);
139142
}
140143

141144
/**
142145
* Set the message of the day.
143146
* @param description - message of the day.
144147
*/
148+
@Override
145149
public void setMotD(Object description) {
146150
DESCRIPTION.set(handle, description);
147151
}
148152

149-
/**
150-
* Set the message of the day.
151-
* @param message - the message.
152-
*/
153-
public void setMotD(String message) {
154-
setMotD(WrappedChatComponent.fromLegacyText(message));
155-
}
156-
157153
/**
158154
* Retrieve the compressed PNG file that is being displayed as a favicon.
159155
* @return The favicon, or NULL if no favicon will be displayed.
160156
*/
157+
@Override
161158
public String getFavicon() {
162159
return (String) FAVICON.get(handle);
163160
}
@@ -166,6 +163,7 @@ public String getFavicon() {
166163
* Set the compressed PNG file that is being displayed.
167164
* @param image - the new compressed image or NULL if no favicon should be displayed.
168165
*/
166+
@Override
169167
public void setFavicon(String image) {
170168
FAVICON.set(handle, image);
171169
}
@@ -197,6 +195,7 @@ public void setChatPreviewEnabled(boolean chatPreviewEnabled) {
197195
* @return whether the server enforces secure chat.
198196
* @since 1.19.1
199197
*/
198+
@Override
200199
public boolean isEnforceSecureChat() {
201200
int index = MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove() ? 0 : 1;
202201
return (Boolean) BOOLEAN_ACCESSORS[index].get(handle);
@@ -207,6 +206,7 @@ public boolean isEnforceSecureChat() {
207206
* @param enforceSecureChat true if enabled, false otherwise.
208207
* @since 1.19.1
209208
*/
209+
@Override
210210
public void setEnforceSecureChat(boolean enforceSecureChat) {
211211
int index = MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove() ? 0 : 1;
212212
BOOLEAN_ACCESSORS[index].set(handle, enforceSecureChat);
@@ -218,6 +218,7 @@ public void setEnforceSecureChat(boolean enforceSecureChat) {
218218
* @throws IllegalStateException If the player count has been hidden via {@link #setPlayersVisible(boolean)}.
219219
* @see #setPlayersOnline(int)
220220
*/
221+
@Override
221222
public int getPlayersOnline() {
222223
if (players == null)
223224
throw new IllegalStateException("The player count has been hidden.");
@@ -231,6 +232,7 @@ public int getPlayersOnline() {
231232
* negative, as well as higher than the player maximum.
232233
* @param online - online players.
233234
*/
235+
@Override
234236
public void setPlayersOnline(int online) {
235237
if (players == null)
236238
resetPlayers();
@@ -243,6 +245,7 @@ public void setPlayersOnline(int online) {
243245
* @throws IllegalStateException If the player maximum has been hidden via {@link #setPlayersVisible(boolean)}.
244246
* @see #setPlayersMaximum(int)
245247
*/
248+
@Override
246249
public int getPlayersMaximum() {
247250
if (players == null)
248251
throw new IllegalStateException("The player maximum has been hidden.");
@@ -256,6 +259,7 @@ public int getPlayersMaximum() {
256259
* is less than the player count.
257260
* @param maximum - maximum player count.
258261
*/
262+
@Override
259263
public void setPlayersMaximum(int maximum) {
260264
if (players == null)
261265
resetPlayers();
@@ -268,6 +272,7 @@ public void setPlayersMaximum(int maximum) {
268272
* Note that this may set the current player count and maximum to their respective real values.
269273
* @param visible - TRUE if it should be visible, FALSE otherwise.
270274
*/
275+
@Override
271276
public void setPlayersVisible(boolean visible) {
272277
if (arePlayersVisible() != visible) {
273278
if (visible) {
@@ -287,6 +292,7 @@ public void setPlayersVisible(boolean visible) {
287292
* If not, the client will display ??? in the same location.
288293
* @return TRUE if the player statistics is visible, FALSE otherwise.
289294
*/
295+
@Override
290296
public boolean arePlayersVisible() {
291297
return players != null;
292298
}
@@ -295,6 +301,7 @@ public boolean arePlayersVisible() {
295301
* Retrieve a copy of all the logged in players.
296302
* @return Logged in players or an empty list if no player names will be displayed.
297303
*/
304+
@Override
298305
public ImmutableList<WrappedGameProfile> getPlayers() {
299306
if (players == null)
300307
return ImmutableList.of();
@@ -306,33 +313,21 @@ public ImmutableList<WrappedGameProfile> getPlayers() {
306313

307314
/**
308315
* Set the displayed list of logged in players.
309-
* @param profile - every logged in player.
316+
* @param playerSample - every logged in player.
310317
*/
311-
public void setPlayers(Object profile) {
318+
@Override
319+
public void setPlayers(Iterable<? extends WrappedGameProfile> playerSample) {
312320
if (players == null)
313321
resetPlayers();
314-
PLAYERS_PROFILES.set(players, profile);
315-
}
316322

317-
/**
318-
* Set the displayed lst of logged in players.
319-
* @param players - the players to display.
320-
*/
321-
public void setBukkitPlayers(Iterable<? extends Player> players) {
322-
final List<WrappedGameProfile> profiles = new ArrayList<>();
323-
324-
for (Player player : players) {
325-
Object profile = ENTITY_HUMAN_PROFILE.get(BukkitUnwrapper.getInstance().unwrapItem(player));
326-
profiles.add(WrappedGameProfile.fromHandle(profile));
327-
}
328-
329-
setPlayers(profiles);
323+
PLAYERS_PROFILES.set(players, PROFILE_CONVERT.getGeneric(playerSample));
330324
}
331325

332326
/**
333327
* Retrieve the version name of the current server.
334328
* @return The version name.
335329
*/
330+
@Override
336331
public String getVersionName() {
337332
return (String) VERSION_NAME.get(version);
338333
}
@@ -341,6 +336,7 @@ public String getVersionName() {
341336
* Set the version name of the current server.
342337
* @param name - the new version name.
343338
*/
339+
@Override
344340
public void setVersionName(String name) {
345341
VERSION_NAME.set(version, name);
346342
}
@@ -349,6 +345,7 @@ public void setVersionName(String name) {
349345
* Retrieve the protocol number.
350346
* @return The protocol.
351347
*/
348+
@Override
352349
public int getVersionProtocol() {
353350
return (Integer) VERSION_PROTOCOL.get(version);
354351
}
@@ -357,6 +354,7 @@ public int getVersionProtocol() {
357354
* Set the version protocol
358355
* @param protocol - the protocol number.
359356
*/
357+
@Override
360358
public void setVersionProtocol(int protocol) {
361359
VERSION_PROTOCOL.set(version, protocol);
362360
}
@@ -367,11 +365,11 @@ public void setVersionProtocol(int protocol) {
367365
*/
368366
public LegacyServerPing deepClone() {
369367
LegacyServerPing copy = new LegacyServerPing();
370-
WrappedChatComponent motd = getMotD();
368+
Object motd = getMotD();
371369

372370
copy.setPlayers(getPlayers());
373371
copy.setFavicon(getFavicon());
374-
copy.setMotD(motd != null ? motd.deepClone() : null);
372+
copy.setMotD(motd != null ? WrappedChatComponent.fromHandle(motd).getHandle() : null);
375373
copy.setVersionName(getVersionName());
376374
copy.setVersionProtocol(getVersionProtocol());
377375

src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.comphenix.protocol.wrappers.ping;
22

3-
import java.util.List;
3+
import java.util.Optional;
44

5-
import com.comphenix.protocol.wrappers.WrappedChatComponent;
65
import com.comphenix.protocol.wrappers.WrappedGameProfile;
7-
import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;
86

9-
public interface ServerPingImpl {
7+
import com.google.common.collect.ImmutableList;
8+
9+
public interface ServerPingImpl extends Cloneable {
1010
Object getMotD();
1111
void setMotD(Object description);
1212
int getPlayersMaximum();
1313
void setPlayersMaximum(int maxPlayers);
1414
int getPlayersOnline();
1515
void setPlayersOnline(int onlineCount);
16-
Object getPlayers();
17-
void setPlayers(Object playerSample);
16+
ImmutableList<WrappedGameProfile> getPlayers();
17+
void setPlayers(Iterable<? extends WrappedGameProfile> playerSample);
1818
String getVersionName();
1919
void setVersionName(String versionName);
2020
int getVersionProtocol();

0 commit comments

Comments
 (0)