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
This commit is contained in:
Pierre Maurice Schwang 2022-02-08 15:56:29 +01:00 committed by GitHub
parent fff14b05cb
commit 16928b05f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 10 deletions

View File

@ -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();
}
}

View File

@ -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<Command> 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<Command> 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;
}
}

View File

@ -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;
}