CommandAPI
CommandAPI copied to clipboard
Add CommandArgument
Resolves #307 This PR adds the CommandArgument, based on this example for Brigadier Suggestions in the documentation. Improving upon that example, this CommandArgument uses the server's CommandMap to provide tab-completions for all commands registered on the server, plugin and Brigadier alike.
The object returned by the CommandArgument is CommandResult. This is just a simple record that bundles the Command and the arguments together, like so:
public record CommandResult(Command command, String[] args) {
public void execute(CommandSender sender) {
command.execute(sender, command.getLabel(), args);
}
}
CommandResult#execute makes it easier for developers to use the CommandResult without picking apart the record if they don't need to be that specific.
The usual replaceSuggestions(ArgumentSuggestions) method was upgraded to replaceSuggestions(ArgumentSuggesstions...) for the CommandArgument. This allows developers to override each argument in the command with their own suggestions, like so:
new CommandAPICommand("restrictedcommand")
.withArguments(new CommandArgument("command")
.replaceSuggestions(
ArgumentSuggestions.strings("give"),
ArgumentSuggestions.strings(info -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new)),
ArgumentSuggestions.strings("diamond", "minecraft:diamond"),
ArgumentSuggestions.empty()
)
)
The CommandArgument of /restrictedcommand will only allow commands of the form give [online player] (diamond/minecraft:diamond). If a user tries to enter any other command the next suggestions will stop showing up and when the command is executed an appropriate CommandSyntaxException will be thrown. Developers can also pass in null instead of an ArgumentSuggestions to use the regular tab completions provided by the server.
In order to make an ArgumentTest for the CommandArgument, NMS#getSimpleCommandMap was implemented in MockNMS. I also added the method public <T> void assertStoresResult(CommandSender sender, String command, Mut<T> queue, T expected) to the ArgumentTests class to reduce code redundancy when asserting the parsed result from running a command.
Documentation for the CommandArgument still needs to be written. Additionally, the example for Brigadier suggestions should probably be updated. Maybe this code from discord would be a good replacement.
Multiple paths for command suggestions are now possible with this syntax (taken from ArgumentTests):
new CommandArgument("command")
.branchSuggestions(
SuggestionsBranch.suggest(
ArgumentSuggestions.strings("give"),
ArgumentSuggestions.strings(info -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new))
).branch(
SuggestionsBranch.suggest(
ArgumentSuggestions.strings("diamond", "minecraft:diamond"),
ArgumentSuggestions.empty()
),
SuggestionsBranch.suggest(
ArgumentSuggestions.strings("dirt", "minecraft:dirt"),
null,
ArgumentSuggestions.empty()
)
),
SuggestionsBranch.suggest(
ArgumentSuggestions.strings("tp"),
ArgumentSuggestions.strings(info -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new)),
ArgumentSuggestions.strings(info -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new))
)
)
CommandArgument#replaceSuggestions and SuggestionsBranch#suggest add ArgumentSuggestions, then CommandArgument#branchSuggestions and SuggestionsBranch#branch are used to create new paths which themselves can be filled with ArgumentSuggestions then a branch.