AsyncTabCompleteEvent

Let plugins be able to control tab completion of commands and chat async.

This will be useful for frameworks like ACF so we can define async safe completion handlers,
and avoid going to main for tab completions.

Especially useful if you need to query a database in order to obtain the results for tab
completion, such as offline players.

Also Enforces mutability of the existing TabCompleteEvent.

Co-authored-by: Aikar <aikar@aikar.co>
This commit is contained in:
Jason Penilla
2017-11-26 13:17:09 -05:00
parent 8306cc5b4f
commit 7132df4810
4 changed files with 537 additions and 2 deletions

View File

@@ -29,13 +29,20 @@ public class TabCompleteEvent extends Event implements Cancellable {
private boolean cancelled;
public TabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, @NotNull List<String> completions) {
// Paper start
this(sender, buffer, completions, sender instanceof org.bukkit.command.ConsoleCommandSender || buffer.startsWith("/"), null);
}
public TabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, @NotNull List<String> completions, boolean isCommand, @org.jetbrains.annotations.Nullable org.bukkit.Location location) {
this.isCommand = isCommand;
this.loc = location;
// Paper end
Preconditions.checkArgument(sender != null, "sender");
Preconditions.checkArgument(buffer != null, "buffer");
Preconditions.checkArgument(completions != null, "completions");
this.sender = sender;
this.buffer = buffer;
this.completions = completions;
this.completions = new java.util.ArrayList<>(completions); // Paper - Completions must be mutable
}
/**
@@ -69,14 +76,35 @@ public class TabCompleteEvent extends Event implements Cancellable {
return completions;
}
// Paper start
private final boolean isCommand;
private final org.bukkit.Location loc;
/**
* @return True if it is a command being tab completed, false if it is a chat message.
*/
public boolean isCommand() {
return isCommand;
}
/**
* @return The position looked at by the sender, or null if none
*/
@org.jetbrains.annotations.Nullable
public org.bukkit.Location getLocation() {
return this.loc != null ? this.loc.clone() : null;
}
// Paper end
/**
* Set the completions offered, overriding any already set.
*
* The passed collection will be cloned to a new List. You must call {{@link #getCompletions()}} to mutate from here
*
* @param completions the new completions
*/
public void setCompletions(@NotNull List<String> completions) {
Preconditions.checkArgument(completions != null);
this.completions = completions;
this.completions = new java.util.ArrayList<>(completions); // Paper - completions must be mutable
}
@Override