From 3ea0dd4d0abc91452143ae512e0a452aad39ccd7 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 9 Jul 2024 15:06:05 +0200 Subject: Add default requires and permission methods to BasicCommand --- patches/api/0469-Brigadier-based-command-API.patch | 28 ++++++++++++++++++++-- .../server/0975-Brigadier-based-command-API.patch | 7 +++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/patches/api/0469-Brigadier-based-command-API.patch b/patches/api/0469-Brigadier-based-command-API.patch index a47c1b82c5..0f9cf5b83c 100644 --- a/patches/api/0469-Brigadier-based-command-API.patch +++ b/patches/api/0469-Brigadier-based-command-API.patch @@ -502,16 +502,18 @@ index 0000000000000000000000000000000000000000..9df87708206e26167a2c4934deff7fc6 +} diff --git a/src/main/java/io/papermc/paper/command/brigadier/BasicCommand.java b/src/main/java/io/papermc/paper/command/brigadier/BasicCommand.java new file mode 100644 -index 0000000000000000000000000000000000000000..0f6b921b4bcf983cf25188823f78a061eec5263d +index 0000000000000000000000000000000000000000..712db7acae2f9818a3b112f770ee77029c6c4e04 --- /dev/null +++ b/src/main/java/io/papermc/paper/command/brigadier/BasicCommand.java -@@ -0,0 +1,36 @@ +@@ -0,0 +1,60 @@ +package io.papermc.paper.command.brigadier; + +import java.util.Collection; +import java.util.Collections; ++import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; ++import org.jetbrains.annotations.Nullable; + +/** + * Implementing this interface allows for easily creating "Bukkit-style" {@code String[] args} commands. @@ -541,6 +543,28 @@ index 0000000000000000000000000000000000000000..0f6b921b4bcf983cf25188823f78a061 + default @NotNull Collection suggest(final @NotNull CommandSourceStack commandSourceStack, final @NotNull String[] args) { + return Collections.emptyList(); + } ++ ++ /** ++ * Checks whether a command sender can receive and run the root command. ++ * ++ * @param sender the command sender trying to execute the command ++ * @return whether the command sender fulfill the root command requirement ++ * @see #permission() ++ */ ++ @ApiStatus.OverrideOnly ++ default boolean requires(final @NotNull CommandSender sender) { ++ return this.permission() == null || sender.hasPermission(this.permission()); ++ } ++ ++ /** ++ * Returns the permission for the root command used in {@link #requires(CommandSender)} by default. ++ * ++ * @return the permission for the root command used in {@link #requires(CommandSender)} ++ */ ++ @ApiStatus.OverrideOnly ++ default @Nullable String permission() { ++ return null; ++ } +} diff --git a/src/main/java/io/papermc/paper/command/brigadier/CommandRegistrationFlag.java b/src/main/java/io/papermc/paper/command/brigadier/CommandRegistrationFlag.java new file mode 100644 diff --git a/patches/server/0975-Brigadier-based-command-API.patch b/patches/server/0975-Brigadier-based-command-API.patch index 71f121c880..8be8329438 100644 --- a/patches/server/0975-Brigadier-based-command-API.patch +++ b/patches/server/0975-Brigadier-based-command-API.patch @@ -686,10 +686,10 @@ index 0000000000000000000000000000000000000000..1b1642f306771f029e6214a2e2ebebb6 +} diff --git a/src/main/java/io/papermc/paper/command/brigadier/PaperCommands.java b/src/main/java/io/papermc/paper/command/brigadier/PaperCommands.java new file mode 100644 -index 0000000000000000000000000000000000000000..27509813a90980be1dfc7bde27d0eba60adfc820 +index 0000000000000000000000000000000000000000..ac83eefcea3f22b7980bf7b4f1f22a5ae18d82c1 --- /dev/null +++ b/src/main/java/io/papermc/paper/command/brigadier/PaperCommands.java -@@ -0,0 +1,193 @@ +@@ -0,0 +1,194 @@ +package io.papermc.paper.command.brigadier; + +import com.google.common.base.Preconditions; @@ -860,11 +860,12 @@ index 0000000000000000000000000000000000000000..27509813a90980be1dfc7bde27d0eba6 + @Override + public @Unmodifiable Set register(final PluginMeta pluginMeta, final String label, final @Nullable String description, final Collection aliases, final BasicCommand basicCommand) { + final LiteralArgumentBuilder builder = Commands.literal(label) ++ .requires(stack -> basicCommand.requires(stack.getSender())) + .then( + Commands.argument("args", StringArgumentType.greedyString()) + .suggests((context, suggestionsBuilder) -> { + final String[] args = StringUtils.split(suggestionsBuilder.getRemaining()); -+ final SuggestionsBuilder offsetSuggestionsBuilder = suggestionsBuilder.createOffset(suggestionsBuilder.getInput().lastIndexOf(' ') + 1);; ++ final SuggestionsBuilder offsetSuggestionsBuilder = suggestionsBuilder.createOffset(suggestionsBuilder.getInput().lastIndexOf(' ') + 1); + + final Collection suggestions = basicCommand.suggest(context.getSource(), args); + suggestions.forEach(offsetSuggestionsBuilder::suggest); -- cgit v1.2.3