Skip to content

Commit

Permalink
Merge pull request #16 from voruti/14-limit-number-of-players-shown
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Jul 15, 2023
2 parents be734c4 + af7e7e7 commit 52d34d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import voruti.velocityplayerlistquery.util.ServerListEntryBuilder;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Plugin(
id = "velocityplayerlistquery",
Expand All @@ -41,12 +47,13 @@ public class VelocityPlayerListQuery {
@DataDirectory
Path dataDirectory;

Config config;
ServerListEntryBuilder serverListEntryBuilder;


@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
final Config config = new PersistenceService(logger, dataDirectory)
this.config = new PersistenceService(logger, dataDirectory)
.loadConfig();
this.serverListEntryBuilder = new ServerListEntryBuilder(logger, config);

Expand All @@ -60,23 +67,49 @@ public EventTask onServerPing(final ProxyPingEvent event) {
return EventTask.async(() -> {
final ServerPing serverPing = event.getPing();

// check if server ping is valid:
// check if server ping is invalid:
if (serverPing.getVersion() == null || serverPing.getDescriptionComponent() == null) {
this.logger.info("Server ping is invalid, skipping");
return;
}

// collect players:
final Collection<Player> players = this.server.getAllPlayers();
if (!players.isEmpty()) {
event.setPing(serverPing.asBuilder()
.samplePlayers(
players.stream()
.map(player -> new ServerPing.SamplePlayer(
this.serverListEntryBuilder.buildForPlayer(player),
player.getGameProfile().getId()
))
.toArray(ServerPing.SamplePlayer[]::new))
.build());
final Stream<ServerPing.SamplePlayer> playerStream = players.stream()
// format players:
.map(player -> new ServerPing.SamplePlayer(
this.serverListEntryBuilder.buildForPlayer(player),
player.getGameProfile().getId()
))
// sort alphabetically:
.sorted(Comparator.comparing(ServerPing.SamplePlayer::getName));

// limit number of players shown in list, if configured:
final List<ServerPing.SamplePlayer> samplePlayers;
if (this.config.maxListEntries() > 0) {
samplePlayers = playerStream
.limit(this.config.maxListEntries())
.collect(Collectors.toCollection(ArrayList::new));

final int numberOfLeftOutPlayers = players.size() - this.config.maxListEntries();
if (numberOfLeftOutPlayers > 0) {
samplePlayers.add(
new ServerPing.SamplePlayer(
String.format("...and %d more", numberOfLeftOutPlayers),
UUID.randomUUID()
)
);
}
} else {
samplePlayers = playerStream.collect(Collectors.toList());
}

event.setPing(
serverPing.asBuilder()
.samplePlayers(samplePlayers.toArray(new ServerPing.SamplePlayer[0]))
.build()
);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Config {
public static final String FILE_NAME = "config.json";

public static final Config DEFAULT_CONFIG = new Config(
"%1$s"
"%1$s",
16
);


Expand All @@ -22,4 +23,9 @@ public class Config {
* </ul>
*/
String serverListEntryFormat;

/**
* Configure how many players are shown in the server list. {@code <= 0} for unlimited (for backwards compatibility).
*/
int maxListEntries;
}

0 comments on commit 52d34d1

Please sign in to comment.