Skip to content

Commit

Permalink
Show proxy-wide online players in server ping
Browse files Browse the repository at this point in the history
  • Loading branch information
BBaoVanC committed Feb 4, 2023
1 parent 8761d02 commit c6773fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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
Expand Down Expand Up @@ -105,6 +107,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;
Expand All @@ -117,6 +120,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;
Expand Down Expand Up @@ -371,6 +375,10 @@ public PingPassthroughMode getPingPassthrough() {
return pingPassthrough;
}

public boolean getSamplePlayersInPing() {
return samplePlayersInPing;
}

public boolean isPlayerAddressLoggingEnabled() {
return enablePlayerAddressLogging;
}
Expand Down Expand Up @@ -546,6 +554,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");
String motd = config.getOrElse("motd", "&#09add3A Velocity Server");
Expand Down Expand Up @@ -577,6 +586,7 @@ public static VelocityConfiguration read(Path path) throws IOException {
forwardingSecret,
kickExisting,
pingPassthroughMode,
samplePlayersInPing,
enablePlayerAddressLogging,
new Servers(serversConfig),
new ForcedHosts(forcedHostsConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,11 +50,27 @@ public ServerListPingHandler(VelocityServer server) {

private ServerPing constructLocalPing(ProtocolVersion version) {
VelocityConfiguration configuration = server.getConfiguration();
List<ServerPing.SamplePlayer> samplePlayers;
if (configuration.getSamplePlayersInPing()) {
List<ServerPing.SamplePlayer> 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
Expand Down
5 changes: 5 additions & 0 deletions proxy/src/main/resources/default-velocity.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ip address withheld> in logs
enable-player-address-logging = true

Expand Down

0 comments on commit c6773fb

Please sign in to comment.