Add default canUse and permission methods to BasicCommand (#11047)
This commit is contained in:
		
					parent
					
						
							
								13940e5a97
							
						
					
				
			
			
				commit
				
					
						c1d9a67e3f
					
				
			
		
					 2 changed files with 26 additions and 1 deletions
				
			
		| 
						 | 
					@ -510,8 +510,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 | 
				
			||||||
+
 | 
					+
 | 
				
			||||||
+import java.util.Collection;
 | 
					+import java.util.Collection;
 | 
				
			||||||
+import java.util.Collections;
 | 
					+import java.util.Collections;
 | 
				
			||||||
 | 
					+import org.bukkit.command.CommandSender;
 | 
				
			||||||
+import org.jetbrains.annotations.ApiStatus;
 | 
					+import org.jetbrains.annotations.ApiStatus;
 | 
				
			||||||
+import org.jetbrains.annotations.NotNull;
 | 
					+import org.jetbrains.annotations.NotNull;
 | 
				
			||||||
 | 
					+import org.jetbrains.annotations.Nullable;
 | 
				
			||||||
+
 | 
					+
 | 
				
			||||||
+/**
 | 
					+/**
 | 
				
			||||||
+ * Implementing this interface allows for easily creating "Bukkit-style" {@code String[] args} commands.
 | 
					+ * Implementing this interface allows for easily creating "Bukkit-style" {@code String[] args} commands.
 | 
				
			||||||
| 
						 | 
					@ -541,6 +543,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 | 
				
			||||||
+    default @NotNull Collection<String> suggest(final @NotNull CommandSourceStack commandSourceStack, final @NotNull String[] args) {
 | 
					+    default @NotNull Collection<String> suggest(final @NotNull CommandSourceStack commandSourceStack, final @NotNull String[] args) {
 | 
				
			||||||
+        return Collections.emptyList();
 | 
					+        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 fulfills the root command requirement
 | 
				
			||||||
 | 
					+     * @see #permission()
 | 
				
			||||||
 | 
					+     */
 | 
				
			||||||
 | 
					+    @ApiStatus.OverrideOnly
 | 
				
			||||||
 | 
					+    default boolean canUse(final @NotNull CommandSender sender) {
 | 
				
			||||||
 | 
					+        return this.permission() == null || sender.hasPermission(this.permission());
 | 
				
			||||||
 | 
					+    }
 | 
				
			||||||
 | 
					+
 | 
				
			||||||
 | 
					+    /**
 | 
				
			||||||
 | 
					+     * Returns the permission for the root command used in {@link #canUse(CommandSender)} by default.
 | 
				
			||||||
 | 
					+     *
 | 
				
			||||||
 | 
					+     * @return the permission for the root command used in {@link #canUse(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
 | 
					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
 | 
					new file mode 100644
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -860,11 +860,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 | 
				
			||||||
+    @Override
 | 
					+    @Override
 | 
				
			||||||
+    public @Unmodifiable Set<String> register(final PluginMeta pluginMeta, final String label, final @Nullable String description, final Collection<String> aliases, final BasicCommand basicCommand) {
 | 
					+    public @Unmodifiable Set<String> register(final PluginMeta pluginMeta, final String label, final @Nullable String description, final Collection<String> aliases, final BasicCommand basicCommand) {
 | 
				
			||||||
+        final LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal(label)
 | 
					+        final LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal(label)
 | 
				
			||||||
 | 
					+            .requires(stack -> basicCommand.canUse(stack.getSender()))
 | 
				
			||||||
+            .then(
 | 
					+            .then(
 | 
				
			||||||
+                Commands.argument("args", StringArgumentType.greedyString())
 | 
					+                Commands.argument("args", StringArgumentType.greedyString())
 | 
				
			||||||
+                    .suggests((context, suggestionsBuilder) -> {
 | 
					+                    .suggests((context, suggestionsBuilder) -> {
 | 
				
			||||||
+                        final String[] args = StringUtils.split(suggestionsBuilder.getRemaining());
 | 
					+                        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<String> suggestions = basicCommand.suggest(context.getSource(), args);
 | 
					+                        final Collection<String> suggestions = basicCommand.suggest(context.getSource(), args);
 | 
				
			||||||
+                        suggestions.forEach(offsetSuggestionsBuilder::suggest);
 | 
					+                        suggestions.forEach(offsetSuggestionsBuilder::suggest);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue