From b37706fd97c59374c41eec894f929d2177dc2d39 Mon Sep 17 00:00:00 2001 From: jmp Date: Mon, 5 Oct 2020 12:55:11 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Respect=20permissions=20in=20help?= =?UTF-8?q?=20queries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commandframework/CommandHelpHandler.java | 41 +++++++++++++++---- .../cloud/commandframework/MinecraftHelp.java | 2 +- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandHelpHandler.java b/cloud-core/src/main/java/cloud/commandframework/CommandHelpHandler.java index 5b3f3b76..e892d9e3 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandHelpHandler.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandHelpHandler.java @@ -26,6 +26,7 @@ package cloud.commandframework; import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.StaticArgument; import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.Collections; @@ -133,12 +134,27 @@ public final class CommandHelpHandler { * @return Help topic, will return an empty {@link IndexHelpTopic} if no results were found */ public @NonNull HelpTopic queryHelp(final @NonNull String query) { + return this.queryHelp(null, query); + } + + /** + * Query for help + * + * @param recipient The recipient of this help query to check permissions against (if Non-Null) + * @param query Query string + * @return Help topic, will return an empty {@link IndexHelpTopic} if no results were found + */ + public @NonNull HelpTopic queryHelp(final @Nullable C recipient, + final @NonNull String query) { + final List> commands = this.getAllCommands(); + commands.removeIf(command -> recipient != null && !this.commandManager.hasPermission(recipient, + command.getCommand() + .getCommandPermission())); if (query.replace(" ", "").isEmpty()) { - return new IndexHelpTopic<>(this.getAllCommands()); + return new IndexHelpTopic<>(commands); } final String[] queryFragments = query.split(" "); - final List> verboseEntries = this.getAllCommands(); final String rootFragment = queryFragments[0]; /* Determine which command we are querying for */ @@ -147,7 +163,7 @@ public final class CommandHelpHandler { boolean exactMatch = false; - for (final VerboseHelpEntry entry : verboseEntries) { + for (final VerboseHelpEntry entry : commands) { final Command command = entry.getCommand(); @SuppressWarnings("unchecked") final StaticArgument staticArgument = (StaticArgument) command.getArguments() .get(0); @@ -189,6 +205,8 @@ public final class CommandHelpHandler { description)); } syntaxHints.sort(Comparator.comparing(VerboseHelpEntry::getSyntaxString)); + syntaxHints.removeIf(command -> recipient != null + && !this.commandManager.hasPermission(recipient, command.getCommand().getCommandPermission())); return new IndexHelpTopic<>(syntaxHints); } @@ -207,7 +225,11 @@ public final class CommandHelpHandler { if (head.getValue() != null && head.getValue().getOwningCommand() != null) { if (head.isLeaf() || index == queryFragments.length) { - return new VerboseHelpTopic<>(head.getValue().getOwningCommand()); + if (recipient == null || this.commandManager.hasPermission(recipient, head.getValue() + .getOwningCommand() + .getCommandPermission())) { + return new VerboseHelpTopic<>(head.getValue().getOwningCommand()); + } } } @@ -235,8 +257,14 @@ public final class CommandHelpHandler { final List childSuggestions = new LinkedList<>(); for (final CommandTree.Node> child : head.getChildren()) { final List> traversedNodesSub = new LinkedList<>(traversedNodes); - traversedNodesSub.add(child.getValue()); - childSuggestions.add(this.commandManager.getCommandSyntaxFormatter().apply(traversedNodesSub, child)); + if (recipient == null + || child.getValue() == null + || child.getValue().getOwningCommand() == null + || this.commandManager.hasPermission(recipient, + child.getValue().getOwningCommand().getCommandPermission())) { + traversedNodesSub.add(child.getValue()); + childSuggestions.add(this.commandManager.getCommandSyntaxFormatter().apply(traversedNodesSub, child)); + } } return new MultiHelpTopic<>(currentDescription, childSuggestions); } @@ -258,7 +286,6 @@ public final class CommandHelpHandler { * * @param Command sender type */ - @SuppressWarnings("unused") public interface HelpTopic { } diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftHelp.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftHelp.java index 2f5d4f42..dea74cce 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftHelp.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftHelp.java @@ -153,7 +153,7 @@ public final class MinecraftHelp { final @NonNull C recipient) { final Audience audience = this.getAudience(recipient); audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_HELP_HEADER))); - this.printTopic(recipient, query, this.commandManager.getCommandHelpHandler().queryHelp(query)); + this.printTopic(recipient, query, this.commandManager.getCommandHelpHandler().queryHelp(recipient, query)); audience.sendMessage(this.miniMessage.parse(this.messageMap.get(MESSAGE_HELP_FOOTER))); }