Skip to content

Commit

Permalink
Implement 'hide-player-count' option
Browse files Browse the repository at this point in the history
Setting this to true will disable the player list hover (same as 'disable-player-list-hover'), but will also cause the player count to appear as '???'.
  • Loading branch information
jpenilla committed Mar 16, 2021
1 parent c92c718 commit 09683f0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
55 changes: 29 additions & 26 deletions bungee/src/main/java/xyz/jpenilla/minimotd/bungee/PingListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,45 @@ public PingListener(final @NonNull MiniMOTD<Favicon> miniMOTD) {
@EventHandler(priority = EventPriority.LOWEST)
public void onPing(final @NonNull ProxyPingEvent e) {
final ServerPing response = e.getResponse();
if (response == null) {
return;
}

if (response != null) {
final String configString = this.miniMOTD.configManager().pluginSettings().configStringForHost(
Optional.ofNullable(e.getConnection().getVirtualHost())
.map(inetSocketAddress -> String.format("%s:%s", inetSocketAddress.getHostName(), inetSocketAddress.getPort()))
.orElse("default")
).orElse("default");
final MiniMOTDConfig cfg = this.miniMOTD.configManager().resolveConfig(configString);
final ServerPing.Players players = response.getPlayers();
final int onlinePlayers = this.miniMOTD.calculateOnlinePlayers(cfg, players.getOnline());
players.setOnline(onlinePlayers);
final String configString = this.miniMOTD.configManager().pluginSettings().configStringForHost(
Optional.ofNullable(e.getConnection().getVirtualHost())
.map(inetSocketAddress -> String.format("%s:%s", inetSocketAddress.getHostName(), inetSocketAddress.getPort()))
.orElse("default")
).orElse("default");
final MiniMOTDConfig cfg = this.miniMOTD.configManager().resolveConfig(configString);
final ServerPing.Players players = response.getPlayers();
final int onlinePlayers = this.miniMOTD.calculateOnlinePlayers(cfg, players.getOnline());
final int maxPlayers = cfg.adjustedMaxPlayers(onlinePlayers, players.getMax());

final int maxPlayers = cfg.adjustedMaxPlayers(onlinePlayers, players.getMax());
if (cfg.hidePlayerCount()) {
response.setPlayers(null);
} else {
players.setOnline(onlinePlayers);
players.setMax(maxPlayers);

if (cfg.disablePlayerListHover()) {
players.setSample(new ServerPing.PlayerInfo[]{});
}
}

final MOTDIconPair<Favicon> pair = this.miniMOTD.createMOTD(cfg, onlinePlayers, maxPlayers);

Component motdComponent = pair.motd();
if (motdComponent != null) {
if (e.getConnection().getVersion() < Constants.MINECRAFT_1_16_PROTOCOL_VERSION) {
motdComponent = ComponentColorDownsampler.downsampler().downsample(motdComponent);
}
response.setDescriptionComponent(new TextComponent(BungeeComponentSerializer.get().serialize(motdComponent)));
}
final MOTDIconPair<Favicon> pair = this.miniMOTD.createMOTD(cfg, onlinePlayers, maxPlayers);

final Favicon favicon = pair.icon();
if (favicon != null) {
response.setFavicon(favicon);
Component motdComponent = pair.motd();
if (motdComponent != null) {
if (e.getConnection().getVersion() < Constants.MINECRAFT_1_16_PROTOCOL_VERSION) {
motdComponent = ComponentColorDownsampler.downsampler().downsample(motdComponent);
}
response.setDescriptionComponent(new TextComponent(BungeeComponentSerializer.get().serialize(motdComponent)));
}

response.setPlayers(players);
e.setResponse(response);
final Favicon favicon = pair.icon();
if (favicon != null) {
response.setFavicon(favicon);
}

e.setResponse(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public static final class PlayerCountSettings {
@Comment("Setting this to true will disable the hover text showing online player usernames")
private boolean disablePlayerListHover = false;

@Comment("Setting this to true will disable the player list hover (same as 'disable-player-list-hover'),\n"
+ "but will also cause the player count to appear as '???'")
private boolean hidePlayerCount = false;

@Comment("Settings for the fake player count feature")
private FakePlayers fakePlayers = new FakePlayers();

Expand Down Expand Up @@ -176,4 +180,8 @@ public boolean fakePlayersEnabled() {
public boolean disablePlayerListHover() {
return this.playerCountSettings.disablePlayerListHover;
}

public boolean hidePlayerCount() {
return this.playerCountSettings.hidePlayerCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ public ServerStatus injectHandleStatusRequest(final MinecraftServer minecraftSer
final ServerStatus modifiedStatus = new ServerStatus();
modifiedStatus.setDescription(vanillaStatus.getDescription());
modifiedStatus.setFavicon(vanillaStatus.getFavicon());
modifiedStatus.setPlayers(vanillaStatus.getPlayers());
modifiedStatus.setVersion(vanillaStatus.getVersion());

final MiniMOTDFabric miniMOTDFabric = MiniMOTDFabric.get();
final MiniMOTD<String> miniMOTD = miniMOTDFabric.miniMOTD();
final MiniMOTDConfig config = miniMOTD.configManager().mainConfig();

final int onlinePlayers = miniMOTD.calculateOnlinePlayers(config, minecraftServer.getPlayerCount());
final int maxPlayers = config.adjustedMaxPlayers(onlinePlayers, modifiedStatus.getPlayers().getMaxPlayers());
final int maxPlayers = config.adjustedMaxPlayers(onlinePlayers, vanillaStatus.getPlayers().getMaxPlayers());

final MOTDIconPair<String> pair = miniMOTD.createMOTD(config, onlinePlayers, maxPlayers);

Expand All @@ -83,17 +82,17 @@ public ServerStatus injectHandleStatusRequest(final MinecraftServer minecraftSer
modifiedStatus.setFavicon(favicon);
}

final GameProfile[] oldSample = modifiedStatus.getPlayers().getSample();
final ServerStatus.Players newPlayers = new ServerStatus.Players(maxPlayers, onlinePlayers);

if (config.disablePlayerListHover()) {
newPlayers.setSample(new GameProfile[]{});
} else {
newPlayers.setSample(oldSample);
if (!config.hidePlayerCount()) {
final GameProfile[] oldSample = vanillaStatus.getPlayers().getSample();
final ServerStatus.Players newPlayers = new ServerStatus.Players(maxPlayers, onlinePlayers);
if (config.disablePlayerListHover()) {
newPlayers.setSample(new GameProfile[]{});
} else {
newPlayers.setSample(oldSample);
}
modifiedStatus.setPlayers(newPlayers);
}

modifiedStatus.setPlayers(newPlayers);

return modifiedStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ public void onPing(final @NonNull PaperServerListPingEvent e) {
if (cfg.disablePlayerListHover()) {
e.getPlayerSample().clear();
}
if (cfg.hidePlayerCount()) {
e.setHidePlayers(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ final class ClientPingServerEventListener implements EventListener<ClientPingSer
@Override
public void handle(final @NonNull ClientPingServerEvent event) {
final ClientPingServerEvent.Response response = event.getResponse();
final ClientPingServerEvent.Response.Players players = response.getPlayers().orElse(null);
if (players == null) {
return;

final ClientPingServerEvent.Response.Players players;
final ClientPingServerEvent.Response.Players players0 = response.getPlayers().orElse(null);
if (players0 != null) {
players = players0;
} else {
response.setHidePlayers(false);
players = response.getPlayers().orElse(null);
if (players == null) {
this.miniMOTD.logger().warn(String.format("Failed to handle ClientPingServerEvent: '%s', response.getPlayers() was null.", event.toString()));
return;
}
}

final MiniMOTDConfig config = this.miniMOTD.configManager().mainConfig();
Expand Down Expand Up @@ -88,5 +97,8 @@ public void handle(final @NonNull ClientPingServerEvent event) {
if (config.disablePlayerListHover()) {
players.getProfiles().clear();
}
if (config.hidePlayerCount()) {
response.setHidePlayers(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public void onServerListPing(final @NonNull ProxyPingEvent ping) {
if (config.disablePlayerListHover()) {
pong.clearSamplePlayers();
}
if (config.hidePlayerCount()) {
pong.nullPlayers();
}

ping.setPing(pong.build());
}
Expand Down

0 comments on commit 09683f0

Please sign in to comment.