Skip to content

Commit

Permalink
Improved arguments builder
Browse files Browse the repository at this point in the history
- Added Argument#suggest() method
- Updated child argument classes
- Updated Command class
  • Loading branch information
josemmo committed Jan 4, 2024
1 parent 2a8cd05 commit d99e037
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 61 deletions.
7 changes: 6 additions & 1 deletion src/main/java/io/josemmo/bukkit/plugin/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Suggestions> suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) {
return builder.buildFuture();
}

/**
* Parse argument value
* @param sender Command sender
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,8 +20,11 @@ public ImageFileArgument(@NotNull String name) {
}

@Override
public @NotNull RequiredArgumentBuilder<?, ?> build() {
return super.build().suggests(this::getSuggestions);
public @NotNull CompletableFuture<Suggestions> suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) {
for (String filename : YamipaPlugin.getInstance().getStorage().getAllFilenames()) {
builder.suggest(StringArgumentType.escapeIfRequired(filename));
}
return builder.buildFuture();
}

@Override
Expand All @@ -33,15 +35,4 @@ public ImageFileArgument(@NotNull String name) {
}
return imageFile;
}

private @NotNull CompletableFuture<Suggestions> getSuggestions(
@NotNull CommandContext<?> ctx,
@NotNull SuggestionsBuilder builder
) {
for (String filename : YamipaPlugin.getInstance().getStorage().getAllFilenames()) {
String suggestion = "\"" + filename.replaceAll("\"","\\\\\"") + "\"";
builder.suggest(suggestion);
}
return builder.buildFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Suggestions> getSuggestions(
@NotNull CommandContext<?> ctx,
@NotNull SuggestionsBuilder builder
) {
@Override
public @NotNull CompletableFuture<Suggestions> 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("-"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,8 +21,9 @@ public OnlinePlayerArgument(@NotNull String name) {
}

@Override
public @NotNull RequiredArgumentBuilder<?, ?> build() {
return super.build().suggests(this::getSuggestions);
public @NotNull CompletableFuture<Suggestions> suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) {
getAllowedValues().keySet().forEach(builder::suggest);
return builder.buildFuture();
}

@Override
Expand All @@ -36,14 +35,6 @@ public OnlinePlayerArgument(@NotNull String name) {
return player;
}

private @NotNull CompletableFuture<Suggestions> getSuggestions(
@NotNull CommandContext<?> ctx,
@NotNull SuggestionsBuilder builder
) {
getAllowedValues().keySet().forEach(builder::suggest);
return builder.buildFuture();
}

private @NotNull Map<String, Player> getAllowedValues() {
Map<String, Player> values = new HashMap<>();
for (Player player : Bukkit.getOnlinePlayers()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,8 +22,9 @@ public PlacedByArgument(@NotNull String name) {
}

@Override
public @NotNull RequiredArgumentBuilder<?, ?> build() {
return super.build().suggests(this::getSuggestions);
public @NotNull CompletableFuture<Suggestions> suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) {
getAllowedValues().keySet().forEach(builder::suggest);
return builder.buildFuture();
}

@Override
Expand All @@ -37,14 +36,6 @@ public PlacedByArgument(@NotNull String name) {
return player;
}

private @NotNull CompletableFuture<Suggestions> getSuggestions(
@NotNull CommandContext<?> ctx,
@NotNull SuggestionsBuilder builder
) {
getAllowedValues().keySet().forEach(builder::suggest);
return builder.buildFuture();
}

private @NotNull Map<String, OfflinePlayer> getAllowedValues() {
Map<String, OfflinePlayer> values = new HashMap<>();
ImageRenderer renderer = YamipaPlugin.getInstance().getRenderer();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,8 +19,11 @@ public WorldArgument(@NotNull String name) {
}

@Override
public @NotNull RequiredArgumentBuilder<?, ?> build() {
return super.build().suggests(this::getSuggestions);
public @NotNull CompletableFuture<Suggestions> suggest(@NotNull CommandSender sender, @NotNull SuggestionsBuilder builder) {
for (World world : Bukkit.getWorlds()) {
builder.suggest(world.getName());
}
return builder.buildFuture();
}

@Override
Expand All @@ -33,14 +34,4 @@ public WorldArgument(@NotNull String name) {
}
return world;
}

private @NotNull CompletableFuture<Suggestions> getSuggestions(
@NotNull CommandContext<?> ctx,
@NotNull SuggestionsBuilder builder
) {
for (World world : Bukkit.getWorlds()) {
builder.suggest(world.getName());
}
return builder.buildFuture();
}
}

0 comments on commit d99e037

Please sign in to comment.