diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java b/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java index 1b457e1..bbcd959 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/Command.java @@ -129,7 +129,12 @@ public Command(@NotNull String name) { // Chain command elements from the bottom-up if (argIndex < arguments.size()) { - parent.then(buildElement(arguments.get(argIndex).build(), argIndex+1)).executes(ctx -> { + Argument argument = arguments.get(argIndex); + ArgumentBuilder argumentBuilder = argument.build().suggests((ctx, builder) -> { + CommandSender sender = Internals.getBukkitSender(ctx.getSource()); + return argument.suggest(sender, builder); + }); + parent.then(buildElement(argumentBuilder, argIndex+1)).executes(ctx -> { CommandSender sender = Internals.getBukkitSender(ctx.getSource()); sender.sendMessage(ChatColor.RED + "Missing required arguments"); return 1; diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/Argument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/Argument.java index f5bda89..5a840fe 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/Argument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/Argument.java @@ -4,8 +4,11 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.concurrent.CompletableFuture; public abstract class Argument { protected final String name; @@ -28,10 +31,20 @@ public Argument(@NotNull String name) { /** * Build argument - * @return Required Argument Builder instance + * @return Argument builder instance */ public abstract @NotNull RequiredArgumentBuilder build(); + /** + * Suggest argument values + * @param sender Command sender + * @param builder Suggestions builder instance + * @return Suggestions + */ + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { + return builder.buildFuture(); + } + /** * Parse argument value * @param sender Command sender @@ -42,6 +55,11 @@ public Argument(@NotNull String name) { return rawValue; } + /** + * Create new syntax exception + * @param message Message to show + * @return Syntax exception + */ protected @NotNull CommandSyntaxException newException(@NotNull String message) { return new SimpleCommandExceptionType(new LiteralMessage(message)).create(); } diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFileArgument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFileArgument.java index a2dfd9f..263a5fb 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFileArgument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFileArgument.java @@ -1,7 +1,6 @@ package io.josemmo.bukkit.plugin.commands.arguments; -import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -21,8 +20,11 @@ public ImageFileArgument(@NotNull String name) { } @Override - public @NotNull RequiredArgumentBuilder build() { - return super.build().suggests(this::getSuggestions); + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { + for (String filename : YamipaPlugin.getInstance().getStorage().getAllFilenames()) { + builder.suggest(StringArgumentType.escapeIfRequired(filename)); + } + return builder.buildFuture(); } @Override @@ -33,15 +35,4 @@ public ImageFileArgument(@NotNull String name) { } return imageFile; } - - private @NotNull CompletableFuture getSuggestions( - @NotNull CommandContext ctx, - @NotNull SuggestionsBuilder builder - ) { - for (String filename : YamipaPlugin.getInstance().getStorage().getAllFilenames()) { - String suggestion = "\"" + filename.replaceAll("\"","\\\\\"") + "\""; - builder.suggest(suggestion); - } - return builder.buildFuture(); - } } diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFlagsArgument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFlagsArgument.java index cfe4576..48cd985 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFlagsArgument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/ImageFlagsArgument.java @@ -2,7 +2,6 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -28,13 +27,11 @@ public ImageFlagsArgument(@NotNull String name, int defaultFlags) { @Override public @NotNull RequiredArgumentBuilder build() { - return RequiredArgumentBuilder.argument(name, StringArgumentType.greedyString()).suggests(this::getSuggestions); + return RequiredArgumentBuilder.argument(name, StringArgumentType.greedyString()); } - private @NotNull CompletableFuture getSuggestions( - @NotNull CommandContext ctx, - @NotNull SuggestionsBuilder builder - ) { + @Override + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { String input = builder.getRemaining().replaceAll("[^A-Z+\\-,]", ""); int lastIndex = Collections.max( Arrays.asList(input.lastIndexOf(","), input.lastIndexOf("+"), input.lastIndexOf("-")) diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/OnlinePlayerArgument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/OnlinePlayerArgument.java index 51350f2..6957534 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/OnlinePlayerArgument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/OnlinePlayerArgument.java @@ -1,7 +1,5 @@ package io.josemmo.bukkit.plugin.commands.arguments; -import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -23,8 +21,9 @@ public OnlinePlayerArgument(@NotNull String name) { } @Override - public @NotNull RequiredArgumentBuilder build() { - return super.build().suggests(this::getSuggestions); + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { + getAllowedValues().keySet().forEach(builder::suggest); + return builder.buildFuture(); } @Override @@ -36,14 +35,6 @@ public OnlinePlayerArgument(@NotNull String name) { return player; } - private @NotNull CompletableFuture getSuggestions( - @NotNull CommandContext ctx, - @NotNull SuggestionsBuilder builder - ) { - getAllowedValues().keySet().forEach(builder::suggest); - return builder.buildFuture(); - } - private @NotNull Map getAllowedValues() { Map values = new HashMap<>(); for (Player player : Bukkit.getOnlinePlayers()) { diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/PlacedByArgument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/PlacedByArgument.java index 45f87a8..a575bf1 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/PlacedByArgument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/PlacedByArgument.java @@ -1,7 +1,5 @@ package io.josemmo.bukkit.plugin.commands.arguments; -import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -24,8 +22,9 @@ public PlacedByArgument(@NotNull String name) { } @Override - public @NotNull RequiredArgumentBuilder build() { - return super.build().suggests(this::getSuggestions); + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { + getAllowedValues().keySet().forEach(builder::suggest); + return builder.buildFuture(); } @Override @@ -37,14 +36,6 @@ public PlacedByArgument(@NotNull String name) { return player; } - private @NotNull CompletableFuture getSuggestions( - @NotNull CommandContext ctx, - @NotNull SuggestionsBuilder builder - ) { - getAllowedValues().keySet().forEach(builder::suggest); - return builder.buildFuture(); - } - private @NotNull Map getAllowedValues() { Map values = new HashMap<>(); ImageRenderer renderer = YamipaPlugin.getInstance().getRenderer(); diff --git a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/WorldArgument.java b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/WorldArgument.java index 388cf95..523a79f 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/WorldArgument.java +++ b/src/main/java/io/josemmo/bukkit/plugin/commands/arguments/WorldArgument.java @@ -1,7 +1,5 @@ package io.josemmo.bukkit.plugin.commands.arguments; -import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -21,8 +19,11 @@ public WorldArgument(@NotNull String name) { } @Override - public @NotNull RequiredArgumentBuilder build() { - return super.build().suggests(this::getSuggestions); + public @NotNull CompletableFuture suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) { + for (World world : Bukkit.getWorlds()) { + builder.suggest(world.getName()); + } + return builder.buildFuture(); } @Override @@ -33,14 +34,4 @@ public WorldArgument(@NotNull String name) { } return world; } - - private @NotNull CompletableFuture getSuggestions( - @NotNull CommandContext ctx, - @NotNull SuggestionsBuilder builder - ) { - for (World world : Bukkit.getWorlds()) { - builder.suggest(world.getName()); - } - return builder.buildFuture(); - } }