From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar 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/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index fb681f9607f2b17a5edafc60c40342532e33021c..9e7a3a58bf944e612fa4c3bcb3b87efe5eb21c5b 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -740,10 +740,10 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser @Override public void handleCustomCommandSuggestions(ServerboundCommandSuggestionPacket packet) { - PacketUtils.ensureRunningOnSameThread(packet, this, this.player.getLevel()); + // PlayerConnectionUtils.ensureMainThread(packetplayintabcomplete, this, this.player.getWorldServer()); // Paper - run this async // CraftBukkit start if (this.chatSpamTickCount.addAndGet(1) > 500 && !this.server.getPlayerList().isOp(this.player.getGameProfile())) { - this.disconnect(Component.translatable("disconnect.spam")); + server.scheduleOnMain(() -> this.disconnect(Component.translatable("disconnect.spam", new Object[0]))); // Paper return; } // CraftBukkit end @@ -753,12 +753,35 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser stringreader.skip(); } - ParseResults parseresults = this.server.getCommands().getDispatcher().parse(stringreader, this.player.createCommandSourceStack()); + // Paper start - async tab completion + com.destroystokyo.paper.event.server.AsyncTabCompleteEvent event; + java.util.List completions = new java.util.ArrayList<>(); + String buffer = packet.getCommand(); + event = new com.destroystokyo.paper.event.server.AsyncTabCompleteEvent(this.getCraftPlayer(), completions, + buffer, true, null); + event.callEvent(); + completions = event.isCancelled() ? com.google.common.collect.ImmutableList.of() : event.getCompletions(); + // If the event isn't handled, we can assume that we have no completions, and so we'll ask the server + if (!event.isHandled()) { + if (!event.isCancelled()) { + + this.server.scheduleOnMain(() -> { // Paper - This needs to be on main + ParseResults parseresults = this.server.getCommands().getDispatcher().parse(stringreader, this.player.createCommandSourceStack()); - this.server.getCommands().getDispatcher().getCompletionSuggestions(parseresults).thenAccept((suggestions) -> { - if (suggestions.isEmpty()) return; // CraftBukkit - don't send through empty suggestions - prevents [] from showing for plugins with nothing more to offer - this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), suggestions)); - }); + this.server.getCommands().getDispatcher().getCompletionSuggestions(parseresults).thenAccept((suggestions) -> { + if (suggestions.isEmpty()) return; // CraftBukkit - don't send through empty suggestions - prevents [] from showing for plugins with nothing more to offer + this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), suggestions)); + }); + }); + } + } else if (!completions.isEmpty()) { + com.mojang.brigadier.suggestion.SuggestionsBuilder builder = new com.mojang.brigadier.suggestion.SuggestionsBuilder(packet.getCommand(), stringreader.getTotalLength()); + + builder = builder.createOffset(builder.getInput().lastIndexOf(' ') + 1); + completions.forEach(builder::suggest); + player.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.buildFuture().join())); + } + // Paper end - async tab completion } @Override diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index fbe26f8a592ed0a3b970b6f02126b6e3e454d8af..5d62881b974081d7207773580697c5d6ccdc8880 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -2061,7 +2061,7 @@ public final class CraftServer implements Server { offers = this.tabCompleteChat(player, message); } - TabCompleteEvent tabEvent = new TabCompleteEvent(player, message, offers); + TabCompleteEvent tabEvent = new TabCompleteEvent(player, message, offers, message.startsWith("/") || forceCommand, pos != null ? net.minecraft.server.MCUtil.toLocation(((CraftWorld) player.getWorld()).getHandle(), new BlockPos(pos)) : null); // Paper this.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 index b996fde481cebbbcce80a6c267591136db7cc0bc..e5af155d75f717d33c23e22ff8b96bb3ff87844d 100644 --- a/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java +++ b/src/main/java/org/bukkit/craftbukkit/command/ConsoleCommandCompleter.java @@ -28,6 +28,39 @@ public class ConsoleCommandCompleter implements Completer { public void complete(LineReader reader, ParsedLine line, List 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 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 finalCompletions = completions; + Waitable> syncCompletions = new Waitable>() { + @Override + protected List 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> waitable = new Waitable>() { @Override