From 09683f06ce4ec88b1c036d2e7acd01972795b2f3 Mon Sep 17 00:00:00 2001 From: jmp Date: Tue, 16 Mar 2021 15:31:22 -0700 Subject: [PATCH] Implement 'hide-player-count' option 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 '???'. --- .../minimotd/bungee/PingListener.java | 55 ++++++++++--------- .../common/config/MiniMOTDConfig.java | 8 +++ .../ServerStatusPacketListenerImplMixin.java | 21 ++++--- .../minimotd/spigot/PaperPingListener.java | 3 + .../ClientPingServerEventListener.java | 18 +++++- .../minimotd/velocity/MiniMOTDPlugin.java | 3 + 6 files changed, 68 insertions(+), 40 deletions(-) diff --git a/bungee/src/main/java/xyz/jpenilla/minimotd/bungee/PingListener.java b/bungee/src/main/java/xyz/jpenilla/minimotd/bungee/PingListener.java index b5d6ce7d..46c1f32e 100644 --- a/bungee/src/main/java/xyz/jpenilla/minimotd/bungee/PingListener.java +++ b/bungee/src/main/java/xyz/jpenilla/minimotd/bungee/PingListener.java @@ -51,42 +51,45 @@ public PingListener(final @NonNull MiniMOTD 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 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 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); } } diff --git a/common/src/main/java/xyz/jpenilla/minimotd/common/config/MiniMOTDConfig.java b/common/src/main/java/xyz/jpenilla/minimotd/common/config/MiniMOTDConfig.java index e382fe51..4ec7df4f 100644 --- a/common/src/main/java/xyz/jpenilla/minimotd/common/config/MiniMOTDConfig.java +++ b/common/src/main/java/xyz/jpenilla/minimotd/common/config/MiniMOTDConfig.java @@ -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(); @@ -176,4 +180,8 @@ public boolean fakePlayersEnabled() { public boolean disablePlayerListHover() { return this.playerCountSettings.disablePlayerListHover; } + + public boolean hidePlayerCount() { + return this.playerCountSettings.hidePlayerCount; + } } diff --git a/fabric/src/main/java/xyz/jpenilla/minimotd/fabric/mixin/ServerStatusPacketListenerImplMixin.java b/fabric/src/main/java/xyz/jpenilla/minimotd/fabric/mixin/ServerStatusPacketListenerImplMixin.java index 52d6d7c4..9ad38a76 100644 --- a/fabric/src/main/java/xyz/jpenilla/minimotd/fabric/mixin/ServerStatusPacketListenerImplMixin.java +++ b/fabric/src/main/java/xyz/jpenilla/minimotd/fabric/mixin/ServerStatusPacketListenerImplMixin.java @@ -55,7 +55,6 @@ 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(); @@ -63,7 +62,7 @@ public ServerStatus injectHandleStatusRequest(final MinecraftServer minecraftSer 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 pair = miniMOTD.createMOTD(config, onlinePlayers, maxPlayers); @@ -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; } } diff --git a/spigot/src/main/java/xyz/jpenilla/minimotd/spigot/PaperPingListener.java b/spigot/src/main/java/xyz/jpenilla/minimotd/spigot/PaperPingListener.java index 34952812..582e86ae 100644 --- a/spigot/src/main/java/xyz/jpenilla/minimotd/spigot/PaperPingListener.java +++ b/spigot/src/main/java/xyz/jpenilla/minimotd/spigot/PaperPingListener.java @@ -73,5 +73,8 @@ public void onPing(final @NonNull PaperServerListPingEvent e) { if (cfg.disablePlayerListHover()) { e.getPlayerSample().clear(); } + if (cfg.hidePlayerCount()) { + e.setHidePlayers(true); + } } } diff --git a/sponge8/src/main/java/xyz/jpenilla/minimotd/sponge8/ClientPingServerEventListener.java b/sponge8/src/main/java/xyz/jpenilla/minimotd/sponge8/ClientPingServerEventListener.java index 99a28765..4b7eccaa 100644 --- a/sponge8/src/main/java/xyz/jpenilla/minimotd/sponge8/ClientPingServerEventListener.java +++ b/sponge8/src/main/java/xyz/jpenilla/minimotd/sponge8/ClientPingServerEventListener.java @@ -48,9 +48,18 @@ final class ClientPingServerEventListener implements EventListener