Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting a limit on the player list #16

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}