Skip to content

Commit

Permalink
GH-696 Don't count vanished players in list and online command. (#697)
Browse files Browse the repository at this point in the history
* Don't count vanished players in list and online command.

* Use method with Player parameter.
  • Loading branch information
vLuckyyy authored Feb 19, 2024
1 parent a1f201b commit 21afc0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Player> online = this.server.getOnlinePlayers();
Collection<? extends Player> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 21afc0c

Please sign in to comment.