Skip to content

Commit

Permalink
Merge remote-tracking branch 'official/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/voruti/velocityplayerlistquery/VelocityPlayerListQuery.java
#	src/main/java/voruti/velocityplayerlistquery/model/Config.java
  • Loading branch information
C0D3-M4513R committed Jul 16, 2023
2 parents 97c4694 + 52d34d1 commit 0dd2fc1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 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,8 +47,8 @@ public class VelocityPlayerListQuery {
@DataDirectory
Path dataDirectory;

ServerListEntryBuilder serverListEntryBuilder;
Config config;
ServerListEntryBuilder serverListEntryBuilder;


@Subscribe
Expand All @@ -59,20 +65,50 @@ public EventTask onServerPing(final ProxyPingEvent event) {
this.logger.trace("Server ping event received, adding players to server list entry...");

return EventTask.async(() -> {
Collection<Player> players = this.server.getAllPlayers();
final ServerPing serverPing = event.getPing();

// 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()) {
final ServerPing.Builder ping = event.getPing().asBuilder()
.samplePlayers(
players.stream()
.map(player -> new ServerPing.SamplePlayer(
this.serverListEntryBuilder.buildForPlayer(player),
player.getGameProfile().getId()
))
.toArray(ServerPing.SamplePlayer[]::new)
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()
)
);
if (config.setMaxPlayers()) ping.maximumPlayers(players.size());
}
} else {
samplePlayers = playerStream.collect(Collectors.toList());
}
final ServerPing.Builder ping = serverPing.asBuilder()
.samplePlayers(samplePlayers.toArray(new ServerPing.SamplePlayer[0]));
if (config.setMaxPlayers()) ping.maximumPlayers(this.server.getConfiguration().getShowMaxPlayers());
if (config.setOnlinePlayers()) ping.onlinePlayers(players.size());

event.setPing(ping.build());
}
});
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/voruti/velocityplayerlistquery/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Config {

public static final Config DEFAULT_CONFIG = new Config(
"%1$s",
16,
false,
false
);
Expand All @@ -24,6 +25,17 @@ 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;
/**
* Should the online Players be replaced with Velocity Players?
*/
boolean setOnlinePlayers;
/**
* Should the maximum Player count be replaced with the Velocity configured amount?
*/
boolean setMaxPlayers;
}

0 comments on commit 0dd2fc1

Please sign in to comment.