From c0f7da5316cb6696e87bf6385e7c8850a0ea9697 Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Sat, 4 Feb 2023 16:17:02 -0600 Subject: [PATCH] Show proxy-wide online players in server ping --- .../api/proxy/server/ServerPing.java | 4 ++++ .../proxy/config/VelocityConfiguration.java | 10 ++++++++++ .../util/ServerListPingHandler.java | 20 ++++++++++++++++++- .../src/main/resources/default-velocity.toml | 5 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java index a7d9518f03..f4179f4550 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java @@ -429,6 +429,10 @@ public int hashCode() { */ public static final class SamplePlayer { + public static final SamplePlayer ANONYMOUS = new SamplePlayer( + "Anonymous Player", + new UUID(0L, 0L) + ); private final String name; private final UUID id; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index 3afd9fc107..3d3394309f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -75,6 +75,8 @@ public class VelocityConfiguration implements ProxyConfig { private boolean onlineModeKickExistingPlayers = false; @Expose private PingPassthroughMode pingPassthrough = PingPassthroughMode.DISABLED; + @Expose + private boolean samplePlayersInPing = false; private final Servers servers; private final ForcedHosts forcedHosts; @Expose @@ -102,6 +104,7 @@ private VelocityConfiguration(String bind, String motd, int showMaxPlayers, bool boolean preventClientProxyConnections, boolean announceForge, PlayerInfoForwarding playerInfoForwardingMode, byte[] forwardingSecret, boolean onlineModeKickExistingPlayers, PingPassthroughMode pingPassthrough, + boolean samplePlayersInPing, boolean enablePlayerAddressLogging, Servers servers, ForcedHosts forcedHosts, Advanced advanced, Query query, Metrics metrics, boolean forceKeyAuthentication) { this.bind = bind; @@ -114,6 +117,7 @@ private VelocityConfiguration(String bind, String motd, int showMaxPlayers, bool this.forwardingSecret = forwardingSecret; this.onlineModeKickExistingPlayers = onlineModeKickExistingPlayers; this.pingPassthrough = pingPassthrough; + this.samplePlayersInPing = samplePlayersInPing; this.enablePlayerAddressLogging = enablePlayerAddressLogging; this.servers = servers; this.forcedHosts = forcedHosts; @@ -364,6 +368,10 @@ public PingPassthroughMode getPingPassthrough() { return pingPassthrough; } + public boolean getSamplePlayersInPing() { + return samplePlayersInPing; + } + public boolean isPlayerAddressLoggingEnabled() { return enablePlayerAddressLogging; } @@ -554,6 +562,7 @@ public static VelocityConfiguration read(Path path) throws IOException { PlayerInfoForwarding.NONE); PingPassthroughMode pingPassthroughMode = config.getEnumOrElse("ping-passthrough", PingPassthroughMode.DISABLED); + boolean samplePlayersInPing = config.getOrElse("sample-players-in-ping", false); String bind = config.getOrElse("bind", "0.0.0.0:25577"); int maxPlayers = config.getIntOrElse("show-max-players", 500); @@ -584,6 +593,7 @@ public static VelocityConfiguration read(Path path) throws IOException { forwardingSecret, kickExisting, pingPassthroughMode, + samplePlayersInPing, enablePlayerAddressLogging, new Servers(serversConfig), new ForcedHosts(forcedHostsConfig), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java index d253d35945..6cb2ff9b01 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java @@ -30,10 +30,12 @@ import com.velocitypowered.proxy.server.VelocityRegisteredServer; import java.net.InetSocketAddress; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; /** * Common utilities for handling server list ping results. @@ -51,11 +53,27 @@ private ServerPing constructLocalPing(ProtocolVersion version) { version = ProtocolVersion.MAXIMUM_VERSION; } VelocityConfiguration configuration = server.getConfiguration(); + List samplePlayers; + if (configuration.getSamplePlayersInPing()) { + List unshuffledPlayers = server.getAllPlayers().stream() + .map(p -> { + if (p.getPlayerSettings().isClientListingAllowed()) { + return new ServerPing.SamplePlayer(p.getUsername(), p.getUniqueId()); + } else { + return ServerPing.SamplePlayer.ANONYMOUS; + } + }) + .collect(Collectors.toList()); + Collections.shuffle(unshuffledPlayers); + samplePlayers = unshuffledPlayers.subList(0, Math.min(12, server.getPlayerCount())); + } else { + samplePlayers = ImmutableList.of(); + } return new ServerPing( new ServerPing.Version(version.getProtocol(), "Velocity " + ProtocolVersion.SUPPORTED_VERSION_STRING), new ServerPing.Players(server.getPlayerCount(), configuration.getShowMaxPlayers(), - ImmutableList.of()), + samplePlayers), configuration.getMotd(), configuration.getFavicon().orElse(null), configuration.isAnnounceForge() ? ModInfo.DEFAULT : null diff --git a/proxy/src/main/resources/default-velocity.toml b/proxy/src/main/resources/default-velocity.toml index 0f18208b55..73b82e487c 100644 --- a/proxy/src/main/resources/default-velocity.toml +++ b/proxy/src/main/resources/default-velocity.toml @@ -66,6 +66,11 @@ kick-existing-players = false # configuration is used if no servers could be contacted. ping-passthrough = "DISABLED" +# If enabled (default is false), then a sample of the online players on the proxy will be visible +# when hovering over the player count in the server list. +# This doesn't have any effect when ping passthrough is set to either "description" or "all". +sample-players-in-ping = false + # If not enabled (default is true) player IP addresses will be replaced by in logs enable-player-address-logging = true