2020-01-22 02:02:07 +00:00
From 92d990fcfc05f56d1d7d54982cd86e0c4439478a Mon Sep 17 00:00:00 2001
2018-07-18 00:37:30 +00:00
From: Aikar <aikar@aikar.co>
Date: Sun, 26 Nov 2017 13:19:58 -0500
Subject: [PATCH] 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 adds isCommand and getLocation to the sync TabCompleteEvent
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-01-22 02:02:07 +00:00
index 9af8e947e..ad2a36627 100644
2018-07-18 00:37:30 +00:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2019-12-11 23:43:22 +00:00
@@ -524,10 +524,10 @@ public class PlayerConnection implements PacketListenerPlayIn {
2018-07-18 00:37:30 +00:00
2019-04-27 23:23:53 +00:00
@Override
2018-07-18 04:52:33 +00:00
public void a(PacketPlayInTabComplete packetplayintabcomplete) {
- PlayerConnectionUtils.ensureMainThread(packetplayintabcomplete, this, this.player.getWorldServer());
+ // PlayerConnectionUtils.ensureMainThread(packetplayintabcomplete, this, this.player.getWorldServer()); // Paper - run this async
2018-07-18 00:37:30 +00:00
// CraftBukkit start
2018-08-29 16:26:24 +00:00
if (chatSpamField.addAndGet(this, 1) > 500 && !this.minecraftServer.getPlayerList().isOp(this.player.getProfile())) {
2018-09-18 02:04:47 +00:00
- this.disconnect(new ChatMessage("disconnect.spam", new Object[0]));
2019-05-10 17:42:33 +00:00
+ minecraftServer.scheduleOnMain(() -> this.disconnect(new ChatMessage("disconnect.spam", new Object[0]))); // Paper
2018-07-18 00:37:30 +00:00
return;
}
2018-08-26 18:11:49 +00:00
// CraftBukkit end
2019-12-11 23:43:22 +00:00
@@ -537,12 +537,37 @@ public class PlayerConnection implements PacketListenerPlayIn {
2018-08-26 18:11:49 +00:00
stringreader.skip();
}
2019-01-01 03:15:55 +00:00
- ParseResults<CommandListenerWrapper> parseresults = this.minecraftServer.getCommandDispatcher().a().parse(stringreader, this.player.getCommandListener());
2018-10-02 05:38:51 +00:00
+ // Paper start - async tab completion
2018-07-18 00:37:30 +00:00
+ com.destroystokyo.paper.event.server.AsyncTabCompleteEvent event;
2018-07-18 04:52:33 +00:00
+ java.util.List<String> completions = new java.util.ArrayList<>();
+ String buffer = packetplayintabcomplete.c();
2018-07-18 00:37:30 +00:00
+ event = new com.destroystokyo.paper.event.server.AsyncTabCompleteEvent(this.getPlayer(), completions,
2018-07-18 04:52:33 +00:00
+ buffer, true, null);
2018-07-18 00:37:30 +00:00
+ event.callEvent();
+ completions = event.isCancelled() ? com.google.common.collect.ImmutableList.of() : event.getCompletions();
2018-08-29 03:07:50 +00:00
+ // If the event isn't handled, we can assume that we have no completions, and so we'll ask the server
+ if (!event.isHandled()) {
2018-10-02 05:38:51 +00:00
+ if (!event.isCancelled()) {
+ // Paper end - async tab completion
2019-06-03 02:19:44 +00:00
+ this.minecraftServer.scheduleOnMain(() -> { // Paper - This needs to be on main
2019-01-01 03:15:55 +00:00
+ ParseResults<CommandListenerWrapper> parseresults = this.minecraftServer.getCommandDispatcher().a().parse(stringreader, this.player.getCommandListener());
2018-10-02 05:38:51 +00:00
this.minecraftServer.getCommandDispatcher().a().getCompletionSuggestions(parseresults).thenAccept((suggestions) -> {
if (((Suggestions) suggestions).isEmpty()) return; // CraftBukkit - don't send through empty suggestions - prevents [<args>] from showing for plugins with nothing more to offer
- this.networkManager.sendPacket(new PacketPlayOutTabComplete(packetplayintabcomplete.b(), (Suggestions) suggestions)); // CraftBukkit - decompile error
- });
+ this.networkManager.sendPacket(new PacketPlayOutTabComplete(packetplayintabcomplete.b(), (Suggestions) suggestions)); // CraftBukkit - decompile error
+ });
2019-06-03 02:19:44 +00:00
+ }); // Paper - This needs to be on main
2018-07-18 00:37:30 +00:00
+ }
2018-10-02 05:38:51 +00:00
+ // Paper start - async tab completion
2018-08-29 03:07:50 +00:00
+ } else if (!completions.isEmpty()) {
2019-02-05 03:39:00 +00:00
+ com.mojang.brigadier.suggestion.SuggestionsBuilder builder = new com.mojang.brigadier.suggestion.SuggestionsBuilder(packetplayintabcomplete.c(), stringreader.getTotalLength());
2018-08-29 03:07:50 +00:00
+
2019-02-05 03:39:00 +00:00
+ builder = builder.createOffset(builder.getInput().lastIndexOf(' ') + 1);
+ completions.forEach(builder::suggest);
+ player.playerConnection.sendPacket(new PacketPlayOutTabComplete(packetplayintabcomplete.b(), builder.buildFuture().join()));
2018-07-18 04:52:33 +00:00
+ }
2018-10-02 05:38:51 +00:00
+ // Paper end - async tab completion
2018-08-26 18:11:49 +00:00
+
2018-07-18 00:37:30 +00:00
}
2019-04-27 23:23:53 +00:00
@Override
2018-07-18 00:37:30 +00:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2020-01-22 02:02:07 +00:00
index 96112243a..3fa327eb3 100644
2018-07-18 00:37:30 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2019-12-11 23:43:22 +00:00
@@ -1671,7 +1671,7 @@ public final class CraftServer implements Server {
2018-07-18 00:37:30 +00:00
offers = tabCompleteChat(player, message);
}
2018-07-18 04:52:33 +00:00
2018-07-18 00:37:30 +00:00
- TabCompleteEvent tabEvent = new TabCompleteEvent(player, message, offers);
2019-05-06 03:53:47 +00:00
+ TabCompleteEvent tabEvent = new TabCompleteEvent(player, message, offers, message.startsWith("/") || forceCommand, pos != null ? net.minecraft.server.MCUtil.toLocation(((CraftWorld) player.getWorld()).getHandle(), new BlockPosition(pos)) : null); // Paper
2018-07-18 00:37:30 +00:00
getPluginManager().callEvent(tabEvent);
return tabEvent.isCancelled() ? Collections.EMPTY_LIST : tabEvent.getCompletions();
diff --git a/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java b/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java
2020-01-22 02:02:07 +00:00
index 5510266fb..a51202ed5 100644
2018-07-18 00:37:30 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java
+++ b/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java
2019-05-06 02:58:04 +00:00
@@ -28,6 +28,39 @@ public class ConsoleCommandCompleter implements Completer {
2018-07-18 00:37:30 +00:00
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
final CraftServer server = this.server.server;
final String buffer = line.line();
+ // Async Tab Complete
+ com.destroystokyo.paper.event.server.AsyncTabCompleteEvent event;
+ java.util.List<String> completions = new java.util.ArrayList<>();
+ event = new com.destroystokyo.paper.event.server.AsyncTabCompleteEvent(server.getConsoleSender(), completions,
+ buffer, true, null);
+ event.callEvent();
+ completions = event.isCancelled() ? com.google.common.collect.ImmutableList.of() : event.getCompletions();
+
+ if (event.isCancelled() || event.isHandled()) {
+ // Still fire sync event with the provided completions, if someone is listening
+ if (!event.isCancelled() && TabCompleteEvent.getHandlerList().getRegisteredListeners().length > 0) {
+ List<String> finalCompletions = completions;
+ Waitable<List<String>> syncCompletions = new Waitable<List<String>>() {
+ @Override
+ protected List<String> evaluate() {
+ org.bukkit.event.server.TabCompleteEvent syncEvent = new org.bukkit.event.server.TabCompleteEvent(server.getConsoleSender(), buffer, finalCompletions);
+ return syncEvent.callEvent() ? syncEvent.getCompletions() : com.google.common.collect.ImmutableList.of();
+ }
+ };
+ server.getServer().processQueue.add(syncCompletions);
+ try {
+ completions = syncCompletions.get();
+ } catch (InterruptedException | ExecutionException e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ if (!completions.isEmpty()) {
+ candidates.addAll(completions.stream().map(Candidate::new).collect(java.util.stream.Collectors.toList()));
+ }
+ return;
+ }
+
// Paper end
Waitable<List<String>> waitable = new Waitable<List<String>>() {
@Override
--
2020-01-22 02:02:07 +00:00
2.25.0.windows.1
2018-07-18 00:37:30 +00:00