Skip to content

Commit 5ab6789

Browse files
committed
Copy dispatcher root children before passing it into async tree building
1 parent 64828f3 commit 5ab6789

4 files changed

+54
-23
lines changed

patches/server/0273-Async-command-map-building.patch

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ commands if the server is restarting. Using the default async pool caused issues
99
due to the shutdown logic generally being much later.
1010

1111
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
12-
index 2008fd542eaf1c2fac776ae1751c227a3b6dde4b..34564f4118eab9e95176cb4ff4bc93fbe97d9b6b 100644
12+
index 2008fd542eaf1c2fac776ae1751c227a3b6dde4b..938db2436e0f099432263aba593e684eb89a44f9 100644
1313
--- a/src/main/java/net/minecraft/commands/Commands.java
1414
+++ b/src/main/java/net/minecraft/commands/Commands.java
15-
@@ -455,6 +455,19 @@ public class Commands {
15+
@@ -455,6 +455,21 @@ public class Commands {
1616
if ( org.spigotmc.SpigotConfig.tabComplete < 0 ) return; // Spigot
1717
// CraftBukkit start
1818
// Register Vanilla commands into builtRoot as before
1919
+ // Paper start - Perf: Async command map building
20-
+ COMMAND_SENDING_POOL.execute(() -> this.sendAsync(player));
20+
+ // Copy root children to avoid concurrent modification during building
21+
+ final Collection<CommandNode<CommandSourceStack>> commandNodes = new java.util.ArrayList<>(this.dispatcher.getRoot().getChildren());
22+
+ COMMAND_SENDING_POOL.execute(() -> this.sendAsync(player, commandNodes));
2123
+ }
2224
+
2325
+ public static final java.util.concurrent.ExecutorService COMMAND_SENDING_POOL = java.util.concurrent.Executors.newFixedThreadPool(2,
@@ -27,12 +29,19 @@ index 2008fd542eaf1c2fac776ae1751c227a3b6dde4b..34564f4118eab9e95176cb4ff4bc93fb
2729
+ .build()
2830
+ );
2931
+
30-
+ private void sendAsync(ServerPlayer player) {
32+
+ private void sendAsync(ServerPlayer player, Collection<CommandNode<CommandSourceStack>> dispatcherRootChildren) {
3133
+ // Paper end - Perf: Async command map building
3234
Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
3335
RootCommandNode vanillaRoot = new RootCommandNode();
3436

35-
@@ -472,7 +485,14 @@ public class Commands {
37+
@@ -466,13 +481,20 @@ public class Commands {
38+
RootCommandNode<SharedSuggestionProvider> rootcommandnode = new RootCommandNode();
39+
40+
map.put(this.dispatcher.getRoot(), rootcommandnode);
41+
- this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, player.createCommandSourceStack(), map);
42+
+ this.fillUsableCommands(dispatcherRootChildren, rootcommandnode, player.createCommandSourceStack(), map); // Paper - Perf: Async command map building; pass copy of children
43+
44+
Collection<String> bukkit = new LinkedHashSet<>();
3645
for (CommandNode node : rootcommandnode.getChildren()) {
3746
bukkit.add(node.getName());
3847
}
@@ -47,6 +56,28 @@ index 2008fd542eaf1c2fac776ae1751c227a3b6dde4b..34564f4118eab9e95176cb4ff4bc93fb
4756
PlayerCommandSendEvent event = new PlayerCommandSendEvent(player.getBukkitEntity(), new LinkedHashSet<>(bukkit));
4857
event.getPlayer().getServer().getPluginManager().callEvent(event);
4958

59+
@@ -486,8 +508,10 @@ public class Commands {
60+
player.connection.send(new ClientboundCommandsPacket(rootcommandnode));
61+
}
62+
63+
- private void fillUsableCommands(CommandNode<CommandSourceStack> tree, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
64+
- Iterator iterator = tree.getChildren().iterator();
65+
+ // Paper start - Perf: Async command map building; pass copy of children
66+
+ private void fillUsableCommands(Collection<CommandNode<CommandSourceStack>> children, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
67+
+ Iterator iterator = children.iterator();
68+
+ // Paper end - Perf: Async command map building
69+
70+
while (iterator.hasNext()) {
71+
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();
72+
@@ -522,7 +546,7 @@ public class Commands {
73+
resultNodes.put(commandnode2, commandnode3);
74+
result.addChild(commandnode3);
75+
if (!commandnode2.getChildren().isEmpty()) {
76+
- this.fillUsableCommands(commandnode2, commandnode3, source, resultNodes);
77+
+ this.fillUsableCommands(commandnode2.getChildren(), commandnode3, source, resultNodes); // Paper - Perf: Async command map building; pass children directly
78+
}
79+
}
80+
}
5081
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
5182
index 9a9291282f7ec99be9badf8b32fab5f0ea0f1037..feb45369b41b597fd603c12f3da23759923b6a6d 100644
5283
--- a/src/main/java/net/minecraft/server/MinecraftServer.java

patches/server/0274-Brigadier-Mojang-API.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,26 @@ index 4d5f1dd1c3bd742b1bc5e3914101a699041caa7e..5316f148f3f9128690f019d544e462b0
8787
public boolean hasPermission(int level) {
8888
// CraftBukkit start
8989
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
90-
index 34564f4118eab9e95176cb4ff4bc93fbe97d9b6b..c2c8193a3b3f7a224b05a09cd65108860aaae814 100644
90+
index 938db2436e0f099432263aba593e684eb89a44f9..71c8692487c9c1c5b956b4356501d523368c0507 100644
9191
--- a/src/main/java/net/minecraft/commands/Commands.java
9292
+++ b/src/main/java/net/minecraft/commands/Commands.java
93-
@@ -486,6 +486,7 @@ public class Commands {
93+
@@ -488,6 +488,7 @@ public class Commands {
9494
bukkit.add(node.getName());
9595
}
9696
// Paper start - Perf: Async command map building
9797
+ new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent<CommandSourceStack>(player.getBukkitEntity(), (RootCommandNode) rootcommandnode, false).callEvent(); // Paper - Brigadier API
9898
net.minecraft.server.MinecraftServer.getServer().execute(() -> {
9999
runSync(player, bukkit, rootcommandnode);
100100
});
101-
@@ -493,6 +494,7 @@ public class Commands {
101+
@@ -495,6 +496,7 @@ public class Commands {
102102

103103
private void runSync(ServerPlayer player, Collection<String> bukkit, RootCommandNode<SharedSuggestionProvider> rootcommandnode) {
104104
// Paper end - Perf: Async command map building
105105
+ new com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent<CommandSourceStack>(player.getBukkitEntity(), (RootCommandNode) rootcommandnode, true).callEvent(); // Paper - Brigadier API
106106
PlayerCommandSendEvent event = new PlayerCommandSendEvent(player.getBukkitEntity(), new LinkedHashSet<>(bukkit));
107107
event.getPlayer().getServer().getPluginManager().callEvent(event);
108108

109-
@@ -511,6 +513,11 @@ public class Commands {
109+
@@ -515,6 +517,11 @@ public class Commands {
110110

111111
while (iterator.hasNext()) {
112112
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();

patches/server/0953-Brigadier-based-command-API.patch

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ index fc0c60b22844ed010aede2fa125b9fa440d3de80..3549ffea451b932602efb113844ba21a
20682068
public org.bukkit.command.CommandSender getBukkitSender() {
20692069
return this.source.getBukkitSender(this);
20702070
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
2071-
index 26bc8d7075f8a9c411776ab91aeddcd776d21348..2fc5651d1546fd4bb10b0617540e906f483cb36c 100644
2071+
index f190a65ee2e091d4d5a1e114178eac42740075d4..622677fb281242681bf8ba39de34187bcc898a5f 100644
20722072
--- a/src/main/java/net/minecraft/commands/Commands.java
20732073
+++ b/src/main/java/net/minecraft/commands/Commands.java
20742074
@@ -159,7 +159,7 @@ public class Commands {
@@ -2135,8 +2135,8 @@ index 26bc8d7075f8a9c411776ab91aeddcd776d21348..2fc5651d1546fd4bb10b0617540e906f
21352135
StackTraceElement[] astacktraceelement = exception.getStackTrace();
21362136

21372137
for (int i = 0; i < Math.min(astacktraceelement.length, 3); ++i) {
2138-
@@ -473,13 +492,7 @@ public class Commands {
2139-
private void sendAsync(ServerPlayer player) {
2138+
@@ -475,13 +494,7 @@ public class Commands {
2139+
private void sendAsync(ServerPlayer player, Collection<CommandNode<CommandSourceStack>> dispatcherRootChildren) {
21402140
// Paper end - Perf: Async command map building
21412141
Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
21422142
- RootCommandNode vanillaRoot = new RootCommandNode();
@@ -2150,15 +2150,15 @@ index 26bc8d7075f8a9c411776ab91aeddcd776d21348..2fc5651d1546fd4bb10b0617540e906f
21502150
RootCommandNode<SharedSuggestionProvider> rootcommandnode = new RootCommandNode();
21512151

21522152
map.put(this.dispatcher.getRoot(), rootcommandnode);
2153-
@@ -513,6 +526,7 @@ public class Commands {
2154-
}
2153+
@@ -516,6 +529,7 @@ public class Commands {
21552154

2156-
private void fillUsableCommands(CommandNode<CommandSourceStack> tree, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
2155+
// Paper start - Perf: Async command map building; pass copy of children
2156+
private void fillUsableCommands(Collection<CommandNode<CommandSourceStack>> children, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
21572157
+ resultNodes.keySet().removeIf((node) -> !org.spigotmc.SpigotConfig.sendNamespaced && node.getName().contains( ":" )); // Paper - Remove namedspaced from result nodes to prevent redirect trimming ~ see comment below
2158-
Iterator iterator = tree.getChildren().iterator();
2158+
Iterator iterator = children.iterator();
2159+
// Paper end - Perf: Async command map building
21592160

2160-
while (iterator.hasNext()) {
2161-
@@ -526,6 +540,42 @@ public class Commands {
2161+
@@ -530,6 +544,42 @@ public class Commands {
21622162

21632163
if (commandnode2.canUse(source)) {
21642164
ArgumentBuilder argumentbuilder = commandnode2.createBuilder(); // CraftBukkit - decompile error

patches/server/0985-Fix-entity-type-tags-suggestions-in-selectors.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ index 3549ffea451b932602efb113844ba21a7bc72371..13bd145b1e8006a53c22f5dc0c78f29b
3737
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
3838
}
3939
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
40-
index 2fc5651d1546fd4bb10b0617540e906f483cb36c..f58275101fd5671bab0aa66875c989e568384647 100644
40+
index 622677fb281242681bf8ba39de34187bcc898a5f..daca005854ae2bb68300b19bceedadcbbe1e06fd 100644
4141
--- a/src/main/java/net/minecraft/commands/Commands.java
4242
+++ b/src/main/java/net/minecraft/commands/Commands.java
43-
@@ -529,6 +529,7 @@ public class Commands {
44-
resultNodes.keySet().removeIf((node) -> !org.spigotmc.SpigotConfig.sendNamespaced && node.getName().contains( ":" )); // Paper - Remove namedspaced from result nodes to prevent redirect trimming ~ see comment below
45-
Iterator iterator = tree.getChildren().iterator();
43+
@@ -533,6 +533,7 @@ public class Commands {
44+
Iterator iterator = children.iterator();
45+
// Paper end - Perf: Async command map building
4646

4747
+ boolean registeredAskServerSuggestionsForTree = false; // Paper - tell clients to ask server for suggestions for EntityArguments
4848
while (iterator.hasNext()) {
4949
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();
5050
// Paper start - Brigadier API
51-
@@ -591,6 +592,12 @@ public class Commands {
51+
@@ -595,6 +596,12 @@ public class Commands {
5252

5353
if (requiredargumentbuilder.getSuggestionsProvider() != null) {
5454
requiredargumentbuilder.suggests(SuggestionProviders.safelySwap(requiredargumentbuilder.getSuggestionsProvider()));

0 commit comments

Comments
 (0)