From 49024bf3f8fb889c5fbefe2e2f990204cbfb9fb7 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 13 Dec 2022 15:17:51 +0100 Subject: [PATCH] New methods in AbstractPlayerManager + new suggestions suppliers in PaperBrigadierCommand --- .../paper/commands/PaperBrigadierCommand.java | 18 +++++++++++++ .../standalone/AbstractPlayerManager.java | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/pandalib-paper-commands/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java b/pandalib-paper-commands/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java index e162679..0d9e582 100644 --- a/pandalib-paper-commands/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java +++ b/pandalib-paper-commands/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java @@ -26,6 +26,9 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Vec3Argument; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerPlayer; import fr.pandacube.lib.paper.reflect.wrapper.paper.PaperAdventure; +import fr.pandacube.lib.players.standalone.AbstractOffPlayer; +import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; +import fr.pandacube.lib.players.standalone.AbstractPlayerManager; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.util.Log; import net.kyori.adventure.text.Component; @@ -52,6 +55,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Stream; /** * Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher. @@ -409,6 +413,20 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand TAB_PLAYER_CURRENT_SERVER = (sender, ti, token, a) -> { + @SuppressWarnings("unchecked") + AbstractPlayerManager pm = (AbstractPlayerManager) AbstractPlayerManager.getInstance(); + Stream plStream; + if (pm == null) + plStream = Bukkit.getOnlinePlayers().stream().filter(p -> !(sender instanceof Player senderP) || senderP.canSee(p)).map(Player::getName); + else + plStream = pm.getNamesOnlyVisible(sender instanceof Player senderP ? pm.getOffline(senderP.getUniqueId()) : null).stream(); + return SuggestionsSupplier.collectFilteredStream(plStream, token); + }; + /** * A suggestions supplier that suggests the names of the worlds currently loaded on this server. */ diff --git a/pandalib-players/src/main/java/fr/pandacube/lib/players/standalone/AbstractPlayerManager.java b/pandalib-players/src/main/java/fr/pandacube/lib/players/standalone/AbstractPlayerManager.java index dfcf82b..54d9016 100644 --- a/pandalib-players/src/main/java/fr/pandacube/lib/players/standalone/AbstractPlayerManager.java +++ b/pandalib-players/src/main/java/fr/pandacube/lib/players/standalone/AbstractPlayerManager.java @@ -117,8 +117,33 @@ public abstract class AbstractPlayerManager(onlinePlayers.values()); } + /** + * Get all the players that the provided player can see. + * The default implementation returns all the players. + * Concrete subclasses should override this method, especially + * on Paper server, using the {@code Player.canSee(Player)} API. + * @return the players that the provided player can see. + */ + public List getOnlyVisibleFor(OF viewer) { + return getAll(); + } + + /** + * Get all the players that the provided player can see. + * The default implementation returns all the players. + * Concrete subclasses should override this method, especially + * on Paper server, using the {@code Player.canSee(Player)} API. + * @return the players that the provided player can see. + */ + public List getNamesOnlyVisible(OF viewer) { + return getOnlyVisibleFor(viewer).stream() + .map(AbstractOnlinePlayer::getName) + .toList(); + } + /** * Returns an instance of {@link AbstractOffPlayer} corresponding to a player with the provided {@link UUID}. + * * @param p the UUID of the player. * @return an instance of {@link AbstractOffPlayer}. It can be a new instance, an {@link AbstractOnlinePlayer} * instance if the player is online, or a cached instance of {@link AbstractOffPlayer}.