From 16928b05f13b4bac629b6184f5d613d92b8079e6 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Tue, 8 Feb 2022 15:56:29 +0100 Subject: [PATCH] Display "/plot help" categories only, if the player has permission to access these commands (#3490) * feat: only show categories with access in help-menu * chore: cleanup imports * feat: tab complete should respect category permissions as well * chore: cleanup imports again * chore: rename ambiguous method name and update access modifier --- .../core/command/CommandCategory.java | 13 ++++++++ .../com/plotsquared/core/command/Help.java | 33 ++++++++++++++----- .../core/util/helpmenu/HelpMenu.java | 3 +- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/CommandCategory.java b/Core/src/main/java/com/plotsquared/core/command/CommandCategory.java index 146f1914b..13d86f26c 100644 --- a/Core/src/main/java/com/plotsquared/core/command/CommandCategory.java +++ b/Core/src/main/java/com/plotsquared/core/command/CommandCategory.java @@ -28,6 +28,7 @@ package com.plotsquared.core.command; import com.plotsquared.core.configuration.caption.Caption; import com.plotsquared.core.configuration.caption.LocaleHolder; import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.player.PlotPlayer; import org.checkerframework.checker.nullness.qual.NonNull; /** @@ -95,4 +96,16 @@ public enum CommandCategory implements Caption { public String getComponent(@NonNull LocaleHolder localeHolder) { return this.caption.getComponent(localeHolder); } + + /** + * Checks if a player has access to this command category + * + * @param player The player to check against + * @return {@code true} if at least one command of this category can be executed by the player, {@code false} otherwise + * @since TODO + */ + boolean canAccess(PlotPlayer player) { + return !MainCommand.getInstance().getCommands(this, player).isEmpty(); + } + } diff --git a/Core/src/main/java/com/plotsquared/core/command/Help.java b/Core/src/main/java/com/plotsquared/core/command/Help.java index 67e70f636..6694e4fde 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Help.java +++ b/Core/src/main/java/com/plotsquared/core/command/Help.java @@ -37,11 +37,11 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.minimessage.Template; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Locale; import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import java.util.stream.Stream; @CommandDeclaration(command = "help", aliases = "?", @@ -119,6 +119,9 @@ public class Help extends Command { TextComponent.Builder builder = Component.text(); builder.append(MINI_MESSAGE.parse(TranslatableCaption.of("help.help_header").getComponent(player))); for (CommandCategory c : CommandCategory.values()) { + if (!c.canAccess(player)) { + continue; + } builder.append(Component.newline()).append(MINI_MESSAGE .parse( TranslatableCaption.of("help.help_info_item").getComponent(player), @@ -152,12 +155,26 @@ public class Help extends Command { @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { - return Stream.of("claiming", "teleport", "settings", "chat", "schematic", "appearance", "info", "debug", - "administration", "all" - ) - .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH))) - .map(value -> new Command(null, false, value, "", RequiredType.NONE, null) { - }).collect(Collectors.toList()); + final String argument = args[0].toLowerCase(Locale.ENGLISH); + List result = new ArrayList<>(); + + for (final CommandCategory category : CommandCategory.values()) { + if (!category.canAccess(player)) { + continue; + } + String name = category.name().toLowerCase(); + if (!name.startsWith(argument)) { + continue; + } + result.add(new Command(null, false, name, "", RequiredType.NONE, null) { + }); + } + // add the category "all" + if ("all".startsWith(argument)) { + result.add(new Command(null, false, "all", "", RequiredType.NONE, null) { + }); + } + return result; } } diff --git a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java index e05994b56..fad8b7d40 100644 --- a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java +++ b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java @@ -52,8 +52,7 @@ public class HelpMenu { } public HelpMenu getCommands() { - this.commands = - MainCommand.getInstance().getCommands(this.commandCategory, this.commandCaller); + this.commands = MainCommand.getInstance().getCommands(this.commandCategory, this.commandCaller); return this; }