From 21afc0c963da0513ffd7654f1b9f082154f551b4 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Mon, 19 Feb 2024 19:40:50 +0100 Subject: [PATCH] GH-696 Don't count vanished players in list and online command. (#697) * Don't count vanished players in list and online command. * Use method with Player parameter. --- .../playerinfo/OnlinePlayerCountCommand.java | 12 +++++++++--- .../playerinfo/OnlinePlayersListCommand.java | 11 +++++++++-- .../core/feature/vanish/VanishService.java | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayerCountCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayerCountCommand.java index fe2372fcc..d4786e2e4 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayerCountCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayerCountCommand.java @@ -1,6 +1,7 @@ package com.eternalcode.core.feature.essentials.playerinfo; import com.eternalcode.annotations.scan.command.DescriptionDocs; +import com.eternalcode.core.feature.vanish.VanishService; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.notice.NoticeService; import com.eternalcode.core.viewer.Viewer; @@ -10,28 +11,33 @@ import dev.rollczi.litecommands.annotations.command.Command; import org.bukkit.Server; - @Command(name = "online") @Permission("eternalcore.online") class OnlinePlayerCountCommand { private final NoticeService noticeService; + private final VanishService vanishService; private final Server server; @Inject - OnlinePlayerCountCommand(NoticeService noticeService, Server server) { + OnlinePlayerCountCommand(NoticeService noticeService, VanishService vanishService, Server server) { this.noticeService = noticeService; + this.vanishService = vanishService; this.server = server; } @Execute @DescriptionDocs(description = "Shows online players count") void execute(@Context Viewer viewer) { + long visiblePlayerCount = this.server.getOnlinePlayers().stream() + .filter(player -> !this.vanishService.isVanished(player)) + .count(); + this.noticeService .create() .notice(translation -> translation.player().onlinePlayersCountMessage()) .viewer(viewer) - .placeholder("{ONLINE}", String.valueOf(this.server.getOnlinePlayers().size())) + .placeholder("{ONLINE}", String.valueOf(visiblePlayerCount)) .send(); } } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayersListCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayersListCommand.java index e95fe91a9..b1f71db66 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayersListCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/playerinfo/OnlinePlayersListCommand.java @@ -2,6 +2,7 @@ import com.eternalcode.annotations.scan.command.DescriptionDocs; import com.eternalcode.core.configuration.implementation.PluginConfiguration; +import com.eternalcode.core.feature.vanish.VanishService; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.notice.NoticeService; import com.eternalcode.core.viewer.Viewer; @@ -22,19 +23,25 @@ class OnlinePlayersListCommand { private final NoticeService noticeService; private final PluginConfiguration config; + private final VanishService vanishService; private final Server server; @Inject - OnlinePlayersListCommand(PluginConfiguration config, NoticeService noticeService, Server server) { + OnlinePlayersListCommand(PluginConfiguration config, NoticeService noticeService, + VanishService vanishService, Server server) { this.config = config; this.noticeService = noticeService; + this.vanishService = vanishService; this.server = server; } @Execute @DescriptionDocs(description = "Shows online players list") void execute(@Context Viewer viewer) { - Collection online = this.server.getOnlinePlayers(); + Collection online = this.server.getOnlinePlayers() + .stream() + .filter(player -> !this.vanishService.isVanished(player)) + .toList(); String onlineCount = String.valueOf(online.size()); String players = Joiner.on(this.config.format.separator) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/vanish/VanishService.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/vanish/VanishService.java index ce56690e6..1b9c9ab7f 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/vanish/VanishService.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/vanish/VanishService.java @@ -26,7 +26,7 @@ public boolean isVanished(UUID playerUniqueId) { return this.isVanished(player); } - private boolean isVanished(Player player) { + public boolean isVanished(Player player) { for (MetadataValue isVanished : player.getMetadata(METADATA_VANISHED_KEY)) { if (isVanished.asBoolean()) { return true;