Add Support for Async Tab Completions for Paper Servers

This adds the ability for plugins to define completion handlers as async safe (not on games main thread)

When they are defined async safe, and ran on a Paper 1.12.2+ server, with a Paper ACF manager,
completions will be handled mostly async, letting you safely do heavier operations in tab completions.
This commit is contained in:
Aikar
2017-11-26 23:21:15 -05:00
parent 229192f99c
commit fbed6f2be3
31 changed files with 278 additions and 103 deletions
@@ -1,7 +1,6 @@
package co.aikar.commands;
import com.google.common.collect.Iterables;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
@@ -31,11 +31,9 @@ import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.format.TextStyle;
import org.spongepowered.api.world.World;
import javax.swing.plaf.ActionMapUIResource;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
@@ -25,8 +25,6 @@ package co.aikar.commands;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.serializer.TextSerializers;
import java.util.Objects;
@@ -172,13 +172,14 @@ public class SpongeCommandManager extends CommandManager<CommandSource, SpongeCo
}
@Override
public CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args) {
CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) {
return new SpongeCommandOperationContext(
this,
issuer,
command,
commandLabel,
args
args,
isAsync
);
}
}
@@ -4,8 +4,8 @@ import org.spongepowered.api.command.CommandResult;
public class SpongeCommandOperationContext extends CommandOperationContext {
private CommandResult result = CommandResult.success();
SpongeCommandOperationContext(CommandManager manager, CommandIssuer issuer, BaseCommand command, String commandLabel, String[] args) {
super(manager, issuer, command, commandLabel, args);
SpongeCommandOperationContext(CommandManager manager, CommandIssuer issuer, BaseCommand command, String commandLabel, String[] args, boolean isAsync) {
super(manager, issuer, command, commandLabel, args, isAsync);
}
public CommandResult getResult() {
@@ -34,13 +34,10 @@ import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
public class SpongeRootCommand implements CommandCallable, RootCommand {
@@ -70,7 +67,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
@Override
public List<String> getSuggestions(@NotNull CommandSource source, @NotNull String arguments, @Nullable Location<World> location) throws CommandException {
String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
return tabComplete(manager.getCommandIssuer(source), this.name, args);
return getTabCompletions(manager.getCommandIssuer(source), this.name, args);
}
@Override
@@ -93,12 +90,6 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
return Text.of();
}
private List<String> tabComplete(CommandIssuer sender, String alias, String[] args) throws IllegalArgumentException {
Set<String> completions = new HashSet<>();
this.children.forEach(child -> completions.addAll(child.tabComplete(sender, alias, args)));
return new ArrayList<>(completions);
}
private CommandResult executeSponge(CommandIssuer sender, String commandLabel, String[] args) {
BaseCommand cmd = execute(sender, commandLabel, args);
return ((SpongeCommandOperationContext) cmd.lastCommandOperationContext).getResult();
@@ -125,4 +116,9 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
public SetMultimap<String, RegisteredCommand> getSubCommands() {
return subCommands;
}
@Override
public List<BaseCommand> getChildren() {
return children;
}
}