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

@ -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 {