Add asynchronous tab completion to the paper module

This commit is contained in:
Alexander Söderberg 2020-09-20 19:49:25 +02:00
parent 9d5f007e37
commit 6f0dba0bf0
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
14 changed files with 203 additions and 21 deletions

View file

@ -122,7 +122,7 @@ public abstract class CommandManager<C> {
*/
@Nonnull
public CompletableFuture<CommandResult<C>> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
final CommandContext<C> context = this.commandContextFactory.create(commandSender);
final CommandContext<C> context = this.commandContextFactory.create(false, commandSender);
final LinkedList<String> inputQueue = this.tokenize(input);
try {
if (this.preprocessContext(context, inputQueue) == State.ACCEPTED) {
@ -147,7 +147,7 @@ public abstract class CommandManager<C> {
*/
@Nonnull
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
final CommandContext<C> context = this.commandContextFactory.create(commandSender);
final CommandContext<C> context = this.commandContextFactory.create(true, commandSender);
final LinkedList<String> inputQueue = this.tokenize(input);
if (this.preprocessContext(context, inputQueue) == State.ACCEPTED) {
return this.commandSuggestionProcessor.apply(new CommandPreprocessingContext<>(context, inputQueue),

View file

@ -131,7 +131,7 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
private static <C> BiFunction<CommandContext<C>, String, List<String>> buildDefaultSuggestionsProvider(
@Nonnull final CommandArgument<C, ?> argument) {
return (context, s) -> argument.getParser().suggestions(context, s);
return new DelegatingSuggestionsProvider<>(argument.getName(), argument.getParser());
}
/**
@ -404,8 +404,8 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
this.parser = (c, i) -> ArgumentParseResult
.failure(new UnsupportedOperationException("No parser was specified"));
}
if (suggestionsProvider == null) {
suggestionsProvider = this.parser::suggestions;
if (this.suggestionsProvider == null) {
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
}
return new CommandArgument<>(this.required, this.name, this.parser,
this.defaultValue, this.valueType, this.suggestionsProvider);

View file

@ -0,0 +1,54 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.arguments;
import com.intellectualsites.commands.arguments.parser.ArgumentParser;
import com.intellectualsites.commands.context.CommandContext;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.BiFunction;
final class DelegatingSuggestionsProvider<C> implements BiFunction<CommandContext<C>, String, List<String>> {
private final String argumentName;
private final ArgumentParser<C, ?> parser;
DelegatingSuggestionsProvider(@Nonnull final String argumentName, @Nonnull final ArgumentParser<C, ?> parser) {
this.argumentName = argumentName;
this.parser = parser;
}
@Override
public List<String> apply(final CommandContext<C> context, final String s) {
return this.parser.suggestions(context, s);
}
@Override
public String toString() {
return String.format("DelegatingSuggestionsProvider{name='%s',parser='%s'}", this.argumentName,
this.parser.getClass().getCanonicalName());
}
}

View file

@ -215,6 +215,14 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
}
if (this.stringMode == StringMode.SINGLE) {
if (commandContext.isSuggestions()) {
final List<String> suggestions = this.suggestionsProvider.apply(commandContext, inputQueue.peek());
if (!suggestions.isEmpty() && !suggestions.contains(input)) {
return ArgumentParseResult.failure(new IllegalArgumentException(
String.format("'%s' is not one of: %s", input, String.join(", ", suggestions))
));
}
}
inputQueue.remove();
return ArgumentParseResult.success(input);
}
@ -222,7 +230,6 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
final StringJoiner sj = new StringJoiner(" ");
final int size = inputQueue.size();
boolean started = false;
boolean finished = false;

View file

@ -37,6 +37,7 @@ public final class CommandContext<C> {
private final Map<String, Object> internalStorage = new HashMap<>();
private final C commandSender;
private final boolean suggestions;
/**
* Create a new command context instance
@ -44,7 +45,18 @@ public final class CommandContext<C> {
* @param commandSender Sender of the command
*/
public CommandContext(@Nonnull final C commandSender) {
this(false, commandSender);
}
/**
* Create a new command context instance
*
* @param suggestions Whether or not the context is created for command suggestions
* @param commandSender Sender of the command
*/
public CommandContext(final boolean suggestions, @Nonnull final C commandSender) {
this.commandSender = commandSender;
this.suggestions = suggestions;
}
/**
@ -57,6 +69,15 @@ public final class CommandContext<C> {
return this.commandSender;
}
/**
* Check if this context was created for tab completion purposes
*
* @return {@code true} if this context is requesting suggestions, else {@code false}
*/
public boolean isSuggestions() {
return this.suggestions;
}
/**
* Store a value in the context map
*

View file

@ -35,10 +35,11 @@ public interface CommandContextFactory<C> {
/**
* Create a new command context
*
* @param suggestions Whether or not the sender is requesting suggestions
* @param sender Command sender
* @return Command context
*/
@Nonnull
CommandContext<C> create(@Nonnull C sender);
CommandContext<C> create(boolean suggestions, @Nonnull C sender);
}

View file

@ -30,13 +30,14 @@ public final class StandardCommandContextFactory<C> implements CommandContextFac
/**
* Construct a new command context
*
* @param suggestions Whether or not the sender is requesting suggestions
* @param sender Command sender
* @return Created context
*/
@Nonnull
@Override
public CommandContext<C> create(@Nonnull final C sender) {
return new CommandContext<>(sender);
public CommandContext<C> create(final boolean suggestions, @Nonnull final C sender) {
return new CommandContext<>(suggestions, sender);
}
}

View file

@ -73,6 +73,9 @@ public class CommandSuggestionsTest {
final String input2 = "test ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("alt", "comb", "one", "two", "var"), suggestions2);
final String input3 = "test a";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Collections.singletonList("alt"), suggestions3);
}
@Test
@ -82,7 +85,7 @@ public class CommandSuggestionsTest {
Assertions.assertTrue(suggestions.isEmpty());
final String input2 = "test var one";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertTrue(suggestions2.isEmpty());
Assertions.assertEquals(Collections.emptyList(), suggestions2);
final String input3 = "test var one f";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Collections.singletonList("foo"), suggestions3);

View file

@ -88,6 +88,12 @@ public final class BukkitTest extends JavaPlugin {
getLogger().warning("Failed to initialize Brigadier support: " + e.getMessage());
}
try {
((PaperCommandManager<CommandSender>) mgr).registerAsynchronousCompletions();
} catch (final Throwable e) {
getLogger().warning("Failed to register asynchronous command completions: " + e.getMessage());
}
final AnnotationParser<CommandSender> annotationParser
= new AnnotationParser<>(mgr, CommandSender.class, p ->
BukkitCommandMetaBuilder.builder().withDescription(p.get(StandardParameters.DESCRIPTION,
@ -180,7 +186,8 @@ public final class BukkitTest extends JavaPlugin {
@CommandMethod(value = "annotation|a <input> [number]", permission = "some.permission.node")
private void annotatedCommand(@Nonnull final Player player,
@Argument("input") @Completions("one,two,duck") @Nonnull final String input,
@Argument("number") @Range(min = "10", max = "100") final int number) {
@Argument(value = "number", defaultValue = "5") @Range(min = "10", max = "100")
final int number) {
player.sendMessage(ChatColor.GOLD + "Your input was: " + ChatColor.AQUA + input + ChatColor.GREEN + " (" + number + ")");
}

View file

@ -37,6 +37,7 @@ import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.concurrent.CompletionException;
final class BukkitCommand<C> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
@ -76,6 +77,10 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
@ -84,7 +89,7 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) throwable)
+ ((InvalidSyntaxException) finalThrowable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
@ -92,7 +97,7 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + throwable.getMessage())
ChatColor.RED + finalThrowable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
@ -112,7 +117,7 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + throwable.getCause()
+ ChatColor.GRAY + finalThrowable.getCause()
.getMessage())
);
} else {

View file

@ -37,6 +37,7 @@ import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;
import javax.annotation.Nonnull;
import java.util.concurrent.CompletionException;
public final class BungeeCommand<C> extends Command implements TabExecutor {
@ -73,6 +74,10 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
@ -82,16 +87,17 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
.append(((InvalidSyntaxException) finalThrowable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
)
);
} else if (throwable instanceof InvalidCommandSenderException) {
final Throwable finalThrowable1 = throwable;
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
commandSender.sendMessage(new ComponentBuilder(finalThrowable1.getMessage())
.color(ChatColor.RED)
.create())
);
@ -117,7 +123,7 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(throwable.getCause().getMessage())
.append(finalThrowable.getCause().getMessage())
.create())
);
} else {

View file

@ -0,0 +1,62 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.paper;
import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
class AsyncCommandSuggestionsListener<C> implements Listener {
private static final long CACHE_EXPIRATION_TIME = 30L;
private final PaperCommandManager<C> paperCommandManager;
AsyncCommandSuggestionsListener(@Nonnull final PaperCommandManager<C> paperCommandManager) {
this.paperCommandManager = paperCommandManager;
}
@EventHandler
void onTabCompletion(@Nonnull final AsyncTabCompleteEvent event) throws Exception {
if (event.getBuffer().isEmpty() || !event.getBuffer().startsWith("/")) {
return;
}
final String[] arguments = event.getBuffer().substring(1).split(" ");
if (paperCommandManager.getCommandTree().getNamedNode(arguments[0]) == null) {
return;
}
final CommandSender sender = event.getSender();
final C cloudSender = this.paperCommandManager.getCommandSenderMapper().apply(sender);
final List<String> suggestions = new ArrayList<>(this.paperCommandManager.suggest(cloudSender,
event.getBuffer().substring(1)));
event.setCompletions(suggestions);
event.setHandled(true);
}
}

View file

@ -82,4 +82,12 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
}
}
/**
* Register asynchronous completions. This requires all argument parsers to be thread safe, and it
* is up to the caller to guarantee that such is the case
*/
public void registerAsynchronousCompletions() {
Bukkit.getServer().getPluginManager().registerEvents(new AsyncCommandSuggestionsListener<>(this), this.getOwningPlugin());
}
}

View file

@ -44,6 +44,7 @@ import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionException;
final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationHandler {
@ -84,6 +85,10 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
final C sender = this.manager.getCommandSenderMapper().apply(source);
this.manager.executeCommand(sender, input).whenComplete((result, throwable) -> {
if (throwable != null) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
@ -96,7 +101,7 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(throwable.getMessage()).color(NamedTextColor.RED))
source.sendMessage(TextComponent.of(finalThrowable.getMessage()).color(NamedTextColor.RED))
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
@ -114,8 +119,10 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
source.sendMessage(TextComponent.builder("Invalid Command Argument: ", NamedTextColor.RED)
.append(throwable.getCause().getMessage(), NamedTextColor.GRAY)
source.sendMessage(TextComponent.builder("Invalid Command Argument: ",
NamedTextColor.RED)
.append(finalThrowable.getCause().getMessage(),
NamedTextColor.GRAY)
.build())
);
} else {