Skip to content

Commit

Permalink
Add playerListMode config option
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Jul 22, 2023
1 parent d7f5ece commit 00cd160
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/main/java/voruti/velocityplayerlistquery/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Config {
public static final String FILE_NAME = "config.json";

public static final Config DEFAULT = new ConfigBuilder()
.playerListMode(PlayerListMode.ADD)
.serverListEntryFormat("%1$s")
.maxListEntries(16)
.replaceOnlinePlayerCount(false)
Expand All @@ -24,6 +25,16 @@ public class Config {
.build();


/**
* Configure how players are shown in the player list.
*
* @see PlayerListMode#UNCHANGED
* @see PlayerListMode#ADD
* @see PlayerListMode#REPLACE
*/
@Nullable
PlayerListMode playerListMode;

/**
* Configure how players are shown in the server list.
* <ul>
Expand Down Expand Up @@ -76,4 +87,21 @@ public class Config {
*/
@Nullable
String versionName;


public enum PlayerListMode {
/**
* Keep the player list as it is.
*/
UNCHANGED,
/**
* Add players to the player list.
* {@link #serverListEntryFormat} and {@link #maxListEntries} are only applied for the added players.
*/
ADD,
/**
* Replace the player list with players according to {@link #serverListEntryFormat} and {@link #maxListEntries}.
*/
REPLACE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.velocitypowered.api.proxy.server.ServerPing;
import lombok.NonNull;
import lombok.experimental.FieldDefaults;
import voruti.velocityplayerlistquery.model.Config;
import voruti.velocityplayerlistquery.model.Config.PlayerListMode;
import voruti.velocityplayerlistquery.service.ConfigService;
import voruti.velocityplayerlistquery.service.ServerListEntryBuilderService;

Expand All @@ -13,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -34,10 +37,13 @@ public class PlayerListServerPingProcessor extends ServerPingProcessor {

@Override
public boolean isEnabled() {
return this.configService.getConfig().serverListEntryFormat() != null
final Config config = this.configService.getConfig();
final PlayerListMode playerListMode = config.playerListMode();

return playerListMode != null
&& EnumSet.of(PlayerListMode.ADD, PlayerListMode.REPLACE).contains(playerListMode)
&& config.serverListEntryFormat() != null
&& !this.proxyServer.getAllPlayers().isEmpty();
// TODO: 3 options: keep player list, add to player list, replace player list; currently: add to
// TODO: .clearSamplePlayers()
}

@Override
Expand All @@ -55,9 +61,11 @@ public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) {
// sort alphabetically:
.sorted(Comparator.comparing(ServerPing.SamplePlayer::getName));

final Config config = this.configService.getConfig();
final int maxListEntries = config.maxListEntries();

// limit number of players shown in list, if configured:
final List<ServerPing.SamplePlayer> samplePlayers;
final int maxListEntries = this.configService.getConfig().maxListEntries();
if (maxListEntries > 0) {
samplePlayers = playerStream
.limit(maxListEntries)
Expand All @@ -76,6 +84,10 @@ public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) {
samplePlayers = playerStream.collect(Collectors.toList());
}

if (config.playerListMode() == PlayerListMode.REPLACE) {
serverPing.clearSamplePlayers();
}

serverPing.samplePlayers(samplePlayers.toArray(new ServerPing.SamplePlayer[0]));
}
}

0 comments on commit 00cd160

Please sign in to comment.