From e322ee85fd860b1b782610b1a08a745559027166 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Fri, 1 Oct 2021 21:52:44 +0200 Subject: [PATCH] feat: Respect non-visible players in tab completion (Closes #3263) (#3264) * Respect non-visible players in tab completion (Closes #3263) * Deprecate old Tab-Complete methods instead of hard-replacing them * wtf git * Mark for removal, useless new-lines * Pass ConsolePlayer.getConsole() instead of Nullable param --- .../com/plotsquared/core/command/Add.java | 2 +- .../com/plotsquared/core/command/Area.java | 4 +- .../com/plotsquared/core/command/Cluster.java | 4 +- .../com/plotsquared/core/command/Deny.java | 2 +- .../com/plotsquared/core/command/Dislike.java | 4 +- .../plotsquared/core/command/Download.java | 4 +- .../com/plotsquared/core/command/Grant.java | 4 +- .../com/plotsquared/core/command/Inbox.java | 4 +- .../com/plotsquared/core/command/Info.java | 2 +- .../com/plotsquared/core/command/Like.java | 4 +- .../com/plotsquared/core/command/ListCmd.java | 2 +- .../com/plotsquared/core/command/Owner.java | 2 +- .../com/plotsquared/core/command/Rate.java | 4 +- .../com/plotsquared/core/command/Remove.java | 2 +- .../core/command/SchematicCmd.java | 4 +- .../com/plotsquared/core/command/Set.java | 2 +- .../plotsquared/core/command/Template.java | 4 +- .../com/plotsquared/core/command/Trust.java | 2 +- .../com/plotsquared/core/command/Visit.java | 2 +- .../plotsquared/core/util/TabCompletions.java | 72 +++++++++++++++++-- 20 files changed, 97 insertions(+), 33 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Add.java b/Core/src/main/java/com/plotsquared/core/command/Add.java index dc7f9ba38..30ac637b9 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Add.java +++ b/Core/src/main/java/com/plotsquared/core/command/Add.java @@ -166,7 +166,7 @@ public class Add extends Command { @Override public Collection tab(final PlotPlayer player, final String[] args, final boolean space) { - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Area.java b/Core/src/main/java/com/plotsquared/core/command/Area.java index 51df7bb9a..1451a8480 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Area.java +++ b/Core/src/main/java/com/plotsquared/core/command/Area.java @@ -856,11 +856,11 @@ public class Area extends SubCommand { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_AREA) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Cluster.java b/Core/src/main/java/com/plotsquared/core/command/Cluster.java index b1e017db0..7c6d9474e 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Cluster.java +++ b/Core/src/main/java/com/plotsquared/core/command/Cluster.java @@ -916,11 +916,11 @@ public class Cluster extends SubCommand { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_CLUSTER) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Deny.java b/Core/src/main/java/com/plotsquared/core/command/Deny.java index ce80a649e..3c16bddd8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Deny.java +++ b/Core/src/main/java/com/plotsquared/core/command/Deny.java @@ -157,7 +157,7 @@ public class Deny extends SubCommand { @Override public Collection tab(final PlotPlayer player, final String[] args, final boolean space) { - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } private void handleKick(PlotPlayer player, Plot plot) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Dislike.java b/Core/src/main/java/com/plotsquared/core/command/Dislike.java index 5edee5408..c79ec48b3 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Dislike.java +++ b/Core/src/main/java/com/plotsquared/core/command/Dislike.java @@ -70,11 +70,11 @@ public class Dislike extends SubCommand { .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.INFO) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_RATE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Download.java b/Core/src/main/java/com/plotsquared/core/command/Download.java index f81fd244b..40c6c7497 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Download.java +++ b/Core/src/main/java/com/plotsquared/core/command/Download.java @@ -173,11 +173,11 @@ public class Download extends SubCommand { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_DOWNLOAD) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } private void upload(PlotPlayer player, Plot plot) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Grant.java b/Core/src/main/java/com/plotsquared/core/command/Grant.java index 412cc1bae..3ef4ab006 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Grant.java +++ b/Core/src/main/java/com/plotsquared/core/command/Grant.java @@ -176,11 +176,11 @@ public class Grant extends Command { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_GRANT_SINGLE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Inbox.java b/Core/src/main/java/com/plotsquared/core/command/Inbox.java index 0cdcf4de9..682162c2f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Inbox.java +++ b/Core/src/main/java/com/plotsquared/core/command/Inbox.java @@ -307,11 +307,11 @@ public class Inbox extends SubCommand { .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.CHAT) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_INBOX) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Info.java b/Core/src/main/java/com/plotsquared/core/command/Info.java index 54450cacc..1b7196d14 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Info.java +++ b/Core/src/main/java/com/plotsquared/core/command/Info.java @@ -161,7 +161,7 @@ public class Info extends SubCommand { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_AREA_INFO_FORCE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; diff --git a/Core/src/main/java/com/plotsquared/core/command/Like.java b/Core/src/main/java/com/plotsquared/core/command/Like.java index 82303fd32..e35c7c1ff 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Like.java +++ b/Core/src/main/java/com/plotsquared/core/command/Like.java @@ -220,11 +220,11 @@ public class Like extends SubCommand { .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.INFO) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_RATE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java index 36d72d9e7..8b9f3c5b3 100644 --- a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java @@ -532,7 +532,7 @@ public class ListCmd extends SubCommand { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_LIST_PLAYER) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; diff --git a/Core/src/main/java/com/plotsquared/core/command/Owner.java b/Core/src/main/java/com/plotsquared/core/command/Owner.java index 9881afe4c..66a991413 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Owner.java +++ b/Core/src/main/java/com/plotsquared/core/command/Owner.java @@ -209,7 +209,7 @@ public class Owner extends SetCommand { @Override public Collection tab(final PlotPlayer player, final String[] args, final boolean space) { - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Rate.java b/Core/src/main/java/com/plotsquared/core/command/Rate.java index de22f4ce6..86e14fe71 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Rate.java +++ b/Core/src/main/java/com/plotsquared/core/command/Rate.java @@ -292,11 +292,11 @@ public class Rate extends SubCommand { .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.INFO) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_RATE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } private static class MutableInt { diff --git a/Core/src/main/java/com/plotsquared/core/command/Remove.java b/Core/src/main/java/com/plotsquared/core/command/Remove.java index 0e6967489..b671eab06 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Remove.java +++ b/Core/src/main/java/com/plotsquared/core/command/Remove.java @@ -142,7 +142,7 @@ public class Remove extends SubCommand { if (plot == null) { return Collections.emptyList(); } - return TabCompletions.completeAddedPlayers(plot, String.join(",", args).trim(), + return TabCompletions.completeAddedPlayers(player, plot, String.join(",", args).trim(), Collections.singletonList(player.getName()) ); } diff --git a/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java b/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java index e9fcf1e02..4a7017648 100644 --- a/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java @@ -317,11 +317,11 @@ public class SchematicCmd extends SubCommand { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_SCHEMATIC) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Set.java b/Core/src/main/java/com/plotsquared/core/command/Set.java index d5f7c65fe..9148b6418 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Set.java +++ b/Core/src/main/java/com/plotsquared/core/command/Set.java @@ -288,7 +288,7 @@ public class Set extends SubCommand { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_SET) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } else if (args.length > 1) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Template.java b/Core/src/main/java/com/plotsquared/core/command/Template.java index a12cb35c2..f4e7a7017 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Template.java +++ b/Core/src/main/java/com/plotsquared/core/command/Template.java @@ -305,11 +305,11 @@ public class Template extends SubCommand { ) { }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Permission.PERMISSION_TEMPLATE) && args[0].length() > 0) { - commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); } return commands; } - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Trust.java b/Core/src/main/java/com/plotsquared/core/command/Trust.java index 4f209a8b1..667643407 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Trust.java +++ b/Core/src/main/java/com/plotsquared/core/command/Trust.java @@ -168,7 +168,7 @@ public class Trust extends Command { @Override public Collection tab(final PlotPlayer player, final String[] args, final boolean space) { - return TabCompletions.completePlayers(String.join(",", args).trim(), Collections.emptyList()); + return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Visit.java b/Core/src/main/java/com/plotsquared/core/command/Visit.java index d71c30789..84aa179d4 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -334,7 +334,7 @@ public class Visit extends Command { public Collection tab(PlotPlayer player, String[] args, boolean space) { final List completions = new ArrayList<>(); switch (args.length - 1) { - case 0 -> completions.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); + case 0 -> completions.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); case 1 -> { completions.addAll( TabCompletions.completeAreas(args[1])); diff --git a/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java index 5f68b8323..33e623055 100644 --- a/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java +++ b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java @@ -32,11 +32,13 @@ import com.plotsquared.core.command.Command; import com.plotsquared.core.command.CommandCategory; import com.plotsquared.core.command.RequiredType; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.player.ConsolePlayer; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.uuid.UUIDMapping; import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.Arrays; @@ -80,12 +82,31 @@ public final class TabCompletions { * @param input Command input * @param existing Players that should not be included in completions * @return List of completions + * @deprecated In favor {@link #completePlayers(PlotPlayer, String, List)} */ + @Deprecated(forRemoval = true) public static @NonNull List completePlayers( final @NonNull String input, final @NonNull List existing ) { - return completePlayers("players", input, existing, uuid -> true); + return completePlayers(ConsolePlayer.getConsole(), input, existing); + } + + /** + * Get a list of tab completions corresponding to player names. This uses the UUID pipeline + * cache, so it will complete will all names known to PlotSquared + * + * @param input Command input + * @param issuer The player who issued the tab completion + * @param existing Players that should not be included in completions + * @return List of completions + */ + public static @NonNull List completePlayers( + final @NonNull PlotPlayer issuer, + final @NonNull String input, + final @NonNull List existing + ) { + return completePlayers("players", issuer, input, existing, uuid -> true); } /** @@ -95,12 +116,32 @@ public final class TabCompletions { * @param input Command input * @param existing Players that should not be included in completions * @return List of completions + * + * @deprecated In favor {@link #completeAddedPlayers(PlotPlayer, Plot, String, List)} */ + @Deprecated(forRemoval = true) public static @NonNull List completeAddedPlayers( final @NonNull Plot plot, final @NonNull String input, final @NonNull List existing ) { - return completePlayers("added" + plot, input, existing, + return completeAddedPlayers(ConsolePlayer.getConsole(), plot, input, existing); + } + + /** + * Get a list of tab completions corresponding to player names added to the given plot. + * + * @param issuer The player who issued the tab completion + * @param plot Plot to complete added players for + * @param input Command input + * @param existing Players that should not be included in completions + * @return List of completions + */ + public static @NonNull List completeAddedPlayers( + final @NonNull PlotPlayer issuer, + final @NonNull Plot plot, + final @NonNull String input, final @NonNull List existing + ) { + return completePlayers("added" + plot, issuer, input, existing, uuid -> plot.getMembers().contains(uuid) || plot.getTrusted().contains(uuid) || plot.getDenied().contains(uuid) @@ -222,9 +263,28 @@ public final class TabCompletions { * @param existing Players that should not be included in completions * @param uuidFilter Filter applied before caching values * @return List of completions + * @deprecated In favor {@link #completePlayers(String, PlotPlayer, String, List, Predicate)} + */ + @Deprecated(forRemoval = true) + private static List completePlayers( + final @NonNull String cacheIdentifier, + final @NonNull String input, final @NonNull List existing, + final @NonNull Predicate uuidFilter + ) { + return completePlayers(cacheIdentifier, ConsolePlayer.getConsole(), input, existing, uuidFilter); + } + + /** + * @param cacheIdentifier Cache key + * @param issuer The player who issued the tab completion + * @param input Command input + * @param existing Players that should not be included in completions + * @param uuidFilter Filter applied before caching values + * @return List of completions */ private static List completePlayers( final @NonNull String cacheIdentifier, + final @NonNull PlotPlayer issuer, final @NonNull String input, final @NonNull List existing, final @NonNull Predicate uuidFilter ) { @@ -246,9 +306,13 @@ public final class TabCompletions { final Collection> onlinePlayers = PlotSquared.platform().playerManager().getPlayers(); players = new ArrayList<>(onlinePlayers.size()); for (final PlotPlayer player : onlinePlayers) { - if (uuidFilter.test(player.getUUID())) { - players.add(player.getName()); + if (!uuidFilter.test(player.getUUID())) { + continue; } + if (issuer != null && !issuer.canSee(player)) { + continue; + } + players.add(player.getName()); } } return filterCached(players, input, existing);