Skip to content

Commit

Permalink
Fix setting ping player sample in 1.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulloy2 committed Mar 26, 2023
1 parent 0c6fa46 commit 1912a9c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 93 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/comphenix/protocol/wrappers/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -310,4 +311,22 @@ public Class<T> getSpecificType() {
}
};
}

public static <T> List<T> toList(Iterable<? extends T> iterable) {
if (iterable instanceof List) {
return (List<T>) iterable;
}

List<T> result = new ArrayList<>();
if (iterable instanceof Collection) {
Collection<T> coll = (Collection<T>) iterable;
result.addAll(coll);
} else {
for (T elem : iterable) {
result.add(elem);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@
public class WrappedServerPing implements ClonableWrapper {
private static final Class<?> GAME_PROFILE = MinecraftReflection.getGameProfileClass();

// For converting to the underlying array
private static final EquivalentConverter<Iterable<? extends WrappedGameProfile>> PROFILE_CONVERT =
BukkitConverters.getArrayConverter(GAME_PROFILE, BukkitConverters.getWrappedGameProfileConverter());

// Get profile from player
private static final FieldAccessor ENTITY_HUMAN_PROFILE = Accessors.getFieldAccessor(
MinecraftReflection.getEntityPlayerClass().getSuperclass(), GAME_PROFILE, true);

// Server ping fields
private static final Class<?> SERVER_PING = MinecraftReflection.getServerPingClass();
MinecraftReflection.getEntityPlayerClass().getSuperclass(), GAME_PROFILE, true);

private final ServerPingImpl impl;

Expand All @@ -59,9 +52,6 @@ public class WrappedServerPing implements ClonableWrapper {
*/
public WrappedServerPing() {
this.impl = newImpl();

resetPlayers();
resetVersion();
}

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

/**
* Retrieve the message of the day.
* @return The messge of the day.
* @return The message of the day.
*/
public WrappedChatComponent getMotD() {
return WrappedChatComponent.fromHandle(impl.getMotD());
Object handle = impl.getMotD();
return handle != null ? WrappedChatComponent.fromHandle(handle) : null;
}

/**
* Set the message of the day.
* @param description - message of the day.
*/
public void setMotD(WrappedChatComponent description) {
impl.setMotD(description.getHandle());
impl.setMotD(description != null ? description.getHandle() : null);
}

/**
Expand Down Expand Up @@ -267,12 +258,7 @@ public boolean isPlayersVisible() {
* @return Logged in players or an empty list if no player names will be displayed.
*/
public ImmutableList<WrappedGameProfile> getPlayers() {
if (!isPlayersVisible())
return ImmutableList.of();
Object playerProfiles = impl.getPlayers();
if (playerProfiles == null)
return ImmutableList.of();
return ImmutableList.copyOf(PROFILE_CONVERT.getSpecific(playerProfiles));
return impl.getPlayers();
}

/**
Expand All @@ -282,7 +268,7 @@ public ImmutableList<WrappedGameProfile> getPlayers() {
public void setPlayers(Iterable<? extends WrappedGameProfile> profile) {
if (!isPlayersVisible())
resetPlayers();
impl.setPlayers((profile != null) ? PROFILE_CONVERT.getGeneric(profile) : null);
impl.setPlayers(profile);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.comphenix.protocol.wrappers.BukkitConverters;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedServerPing;

import com.google.common.collect.ImmutableList;

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

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

/**
* Retrieve the message of the day.
* @return The messge of the day.
* @return The message of the day.
*/
public WrappedChatComponent getMotD() {
return WrappedChatComponent.fromHandle(DESCRIPTION.get(handle));
@Override
public Object getMotD() {
return DESCRIPTION.get(handle);
}

/**
* Set the message of the day.
* @param description - message of the day.
*/
@Override
public void setMotD(Object description) {
DESCRIPTION.set(handle, description);
}

/**
* Set the message of the day.
* @param message - the message.
*/
public void setMotD(String message) {
setMotD(WrappedChatComponent.fromLegacyText(message));
}

/**
* Retrieve the compressed PNG file that is being displayed as a favicon.
* @return The favicon, or NULL if no favicon will be displayed.
*/
@Override
public String getFavicon() {
return (String) FAVICON.get(handle);
}
Expand All @@ -166,6 +163,7 @@ public String getFavicon() {
* Set the compressed PNG file that is being displayed.
* @param image - the new compressed image or NULL if no favicon should be displayed.
*/
@Override
public void setFavicon(String image) {
FAVICON.set(handle, image);
}
Expand Down Expand Up @@ -197,6 +195,7 @@ public void setChatPreviewEnabled(boolean chatPreviewEnabled) {
* @return whether the server enforces secure chat.
* @since 1.19.1
*/
@Override
public boolean isEnforceSecureChat() {
int index = MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove() ? 0 : 1;
return (Boolean) BOOLEAN_ACCESSORS[index].get(handle);
Expand All @@ -207,6 +206,7 @@ public boolean isEnforceSecureChat() {
* @param enforceSecureChat true if enabled, false otherwise.
* @since 1.19.1
*/
@Override
public void setEnforceSecureChat(boolean enforceSecureChat) {
int index = MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove() ? 0 : 1;
BOOLEAN_ACCESSORS[index].set(handle, enforceSecureChat);
Expand All @@ -218,6 +218,7 @@ public void setEnforceSecureChat(boolean enforceSecureChat) {
* @throws IllegalStateException If the player count has been hidden via {@link #setPlayersVisible(boolean)}.
* @see #setPlayersOnline(int)
*/
@Override
public int getPlayersOnline() {
if (players == null)
throw new IllegalStateException("The player count has been hidden.");
Expand All @@ -231,6 +232,7 @@ public int getPlayersOnline() {
* negative, as well as higher than the player maximum.
* @param online - online players.
*/
@Override
public void setPlayersOnline(int online) {
if (players == null)
resetPlayers();
Expand All @@ -243,6 +245,7 @@ public void setPlayersOnline(int online) {
* @throws IllegalStateException If the player maximum has been hidden via {@link #setPlayersVisible(boolean)}.
* @see #setPlayersMaximum(int)
*/
@Override
public int getPlayersMaximum() {
if (players == null)
throw new IllegalStateException("The player maximum has been hidden.");
Expand All @@ -256,6 +259,7 @@ public int getPlayersMaximum() {
* is less than the player count.
* @param maximum - maximum player count.
*/
@Override
public void setPlayersMaximum(int maximum) {
if (players == null)
resetPlayers();
Expand All @@ -268,6 +272,7 @@ public void setPlayersMaximum(int maximum) {
* Note that this may set the current player count and maximum to their respective real values.
* @param visible - TRUE if it should be visible, FALSE otherwise.
*/
@Override
public void setPlayersVisible(boolean visible) {
if (arePlayersVisible() != visible) {
if (visible) {
Expand All @@ -287,6 +292,7 @@ public void setPlayersVisible(boolean visible) {
* If not, the client will display ??? in the same location.
* @return TRUE if the player statistics is visible, FALSE otherwise.
*/
@Override
public boolean arePlayersVisible() {
return players != null;
}
Expand All @@ -295,6 +301,7 @@ public boolean arePlayersVisible() {
* Retrieve a copy of all the logged in players.
* @return Logged in players or an empty list if no player names will be displayed.
*/
@Override
public ImmutableList<WrappedGameProfile> getPlayers() {
if (players == null)
return ImmutableList.of();
Expand All @@ -306,33 +313,21 @@ public ImmutableList<WrappedGameProfile> getPlayers() {

/**
* Set the displayed list of logged in players.
* @param profile - every logged in player.
* @param playerSample - every logged in player.
*/
public void setPlayers(Object profile) {
@Override
public void setPlayers(Iterable<? extends WrappedGameProfile> playerSample) {
if (players == null)
resetPlayers();
PLAYERS_PROFILES.set(players, profile);
}

/**
* Set the displayed lst of logged in players.
* @param players - the players to display.
*/
public void setBukkitPlayers(Iterable<? extends Player> players) {
final List<WrappedGameProfile> profiles = new ArrayList<>();

for (Player player : players) {
Object profile = ENTITY_HUMAN_PROFILE.get(BukkitUnwrapper.getInstance().unwrapItem(player));
profiles.add(WrappedGameProfile.fromHandle(profile));
}

setPlayers(profiles);
PLAYERS_PROFILES.set(players, PROFILE_CONVERT.getGeneric(playerSample));
}

/**
* Retrieve the version name of the current server.
* @return The version name.
*/
@Override
public String getVersionName() {
return (String) VERSION_NAME.get(version);
}
Expand All @@ -341,6 +336,7 @@ public String getVersionName() {
* Set the version name of the current server.
* @param name - the new version name.
*/
@Override
public void setVersionName(String name) {
VERSION_NAME.set(version, name);
}
Expand All @@ -349,6 +345,7 @@ public void setVersionName(String name) {
* Retrieve the protocol number.
* @return The protocol.
*/
@Override
public int getVersionProtocol() {
return (Integer) VERSION_PROTOCOL.get(version);
}
Expand All @@ -357,6 +354,7 @@ public int getVersionProtocol() {
* Set the version protocol
* @param protocol - the protocol number.
*/
@Override
public void setVersionProtocol(int protocol) {
VERSION_PROTOCOL.set(version, protocol);
}
Expand All @@ -367,11 +365,11 @@ public void setVersionProtocol(int protocol) {
*/
public LegacyServerPing deepClone() {
LegacyServerPing copy = new LegacyServerPing();
WrappedChatComponent motd = getMotD();
Object motd = getMotD();

copy.setPlayers(getPlayers());
copy.setFavicon(getFavicon());
copy.setMotD(motd != null ? motd.deepClone() : null);
copy.setMotD(motd != null ? WrappedChatComponent.fromHandle(motd).getHandle() : null);
copy.setVersionName(getVersionName());
copy.setVersionProtocol(getVersionProtocol());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.comphenix.protocol.wrappers.ping;

import java.util.List;
import java.util.Optional;

import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;

public interface ServerPingImpl {
import com.google.common.collect.ImmutableList;

public interface ServerPingImpl extends Cloneable {
Object getMotD();
void setMotD(Object description);
int getPlayersMaximum();
void setPlayersMaximum(int maxPlayers);
int getPlayersOnline();
void setPlayersOnline(int onlineCount);
Object getPlayers();
void setPlayers(Object playerSample);
ImmutableList<WrappedGameProfile> getPlayers();
void setPlayers(Iterable<? extends WrappedGameProfile> playerSample);
String getVersionName();
void setVersionName(String versionName);
int getVersionProtocol();
Expand Down
Loading

0 comments on commit 1912a9c

Please sign in to comment.