Skip to content

Commit

Permalink
Filter player lists with counts
Browse files Browse the repository at this point in the history
closes #161

Co-authored-by: ens-gijs <ens.gijs@gmail.com>
  • Loading branch information
jpenilla and ens-gijs committed Jan 14, 2024
1 parent 2abf0c1 commit b97ebcb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static final class PlayerCountSettings {
+ "If false, the online player count will be capped at the maximum player count")
private boolean allowExceedingMaximum = false;

@Comment("The list of server names that affect player counts.\n"
@Comment("The list of server names that affect player counts/listing.\n"
+ "Only applicable when running the plugin on a proxy (Velocity or Waterfall/Bungeecord).\n"
+ "When set to an empty list, the default count determined by the proxy will be used.")
+ "When set to an empty list, the default count & list as determined by the proxy will be used.")
private final List<String> servers = new ArrayList<>();

@ConfigSerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerPing;
import com.velocitypowered.api.util.Favicon;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
Expand Down Expand Up @@ -64,12 +67,18 @@ private void handle(final ProxyPingEvent event) {
if (targetServers.isEmpty()) {
playersCount = pong.getOnlinePlayers();
} else {
final Set<ServerPing.SamplePlayer> players = new HashSet<>();
for (final String serverName : targetServers) {
final @Nullable RegisteredServer server = this.proxy.getServer(serverName).orElse(null);
if (server != null) {
playersCount += server.getPlayersConnected().size();
players.addAll(server.getPlayersConnected().stream()
.map(p -> new ServerPing.SamplePlayer(p.getGameProfile().getName(), p.getUniqueId()))
.collect(Collectors.toList()));
}
}
pong.clearSamplePlayers();
pong.samplePlayers(players.toArray(new ServerPing.SamplePlayer[0]));
}

final PingResponse<Favicon> response = this.miniMOTD.createMOTD(config, playersCount, pong.getMaximumPlayers());
Expand Down

0 comments on commit b97ebcb

Please sign in to comment.