Skip to content

Commit 46923da

Browse files
committed
Remove nodes from PaperCommandRegistration#registeredNodes when commands are unregistered
Avoids reintroducing unregistered commands after `/minecraft:reload`
1 parent feb3188 commit 46923da

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandRegistrationStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public abstract class CommandRegistrationStrategy<Source> {
3131
}
3232

3333
// Utility methods
34-
protected void removeBrigadierCommands(CommandDispatcher<Source> dispatcher, String commandName,
34+
protected void removeBrigadierCommands(RootCommandNode<Source> root, String commandName,
3535
boolean unregisterNamespaces, Predicate<CommandNode<Source>> extraCheck) {
36-
RootCommandNode<?> root = dispatcher.getRoot();
3736
Map<String, CommandNode<Source>> children = (Map<String, CommandNode<Source>>) commandNodeChildren.get(root);
3837
Map<String, CommandNode<Source>> literals = (Map<String, CommandNode<Source>>) commandNodeLiterals.get(root);
3938
Map<String, CommandNode<Source>> arguments = (Map<String, CommandNode<Source>>) commandNodeArguments.get(root);

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/PaperCommandRegistration.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
55
import com.mojang.brigadier.tree.CommandNode;
66
import com.mojang.brigadier.tree.LiteralCommandNode;
7+
import com.mojang.brigadier.tree.RootCommandNode;
78

8-
import java.util.ArrayList;
99
import java.util.List;
1010
import java.util.function.Supplier;
1111

@@ -18,7 +18,7 @@ public class PaperCommandRegistration<Source> extends CommandRegistrationStrateg
1818
private final Supplier<CommandDispatcher<Source>> getBrigadierDispatcher;
1919

2020
// Store registered commands nodes for eventual reloads
21-
private final List<LiteralCommandNode<Source>> registeredNodes = new ArrayList<>();
21+
private final RootCommandNode<Source> registeredNodes = new RootCommandNode<>();
2222

2323
public PaperCommandRegistration(Supplier<CommandDispatcher<Source>> getBrigadierDispatcher) {
2424
this.getBrigadierDispatcher = getBrigadierDispatcher;
@@ -45,8 +45,8 @@ public LiteralCommandNode<Source> registerCommandNode(LiteralArgumentBuilder<Sou
4545
LiteralCommandNode<Source> namespacedCommandNode = CommandAPIHandler.getInstance().namespaceNode(commandNode, namespace);
4646

4747
// Add to registered command nodes
48-
registeredNodes.add(commandNode);
49-
registeredNodes.add(namespacedCommandNode);
48+
registeredNodes.addChild(commandNode);
49+
registeredNodes.addChild(namespacedCommandNode);
5050

5151
// Namespace is not empty on Bukkit forks
5252
getBrigadierDispatcher.get().getRoot().addChild(namespacedCommandNode);
@@ -57,16 +57,20 @@ public LiteralCommandNode<Source> registerCommandNode(LiteralArgumentBuilder<Sou
5757
@Override
5858
public void unregister(String commandName, boolean unregisterNamespaces, boolean unregisterBukkit) {
5959
// Remove nodes from the dispatcher
60-
removeBrigadierCommands(getBrigadierDispatcher.get(), commandName, unregisterNamespaces, c -> true);
60+
removeBrigadierCommands(getBrigadierDispatcher.get().getRoot(), commandName, unregisterNamespaces, c -> true);
61+
62+
// Don't add nodes back after a reload
63+
removeBrigadierCommands(registeredNodes, commandName, unregisterNamespaces, c -> true);
6164

6265
// Update the dispatcher file
6366
CommandAPIHandler.getInstance().writeDispatcherToFile();
6467
}
6568

6669
@Override
6770
public void preReloadDataPacks() {
68-
for (LiteralCommandNode<Source> commandNode : registeredNodes) {
69-
getBrigadierDispatcher.get().getRoot().addChild(commandNode);
71+
RootCommandNode<Source> root = getBrigadierDispatcher.get().getRoot();
72+
for (CommandNode<Source> commandNode : registeredNodes.getChildren()) {
73+
root.addChild(commandNode);
7074
}
7175
}
7276
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/SpigotCommandRegistration.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ public void runTasksAfterServerStart() {
7070

7171
private void fixNamespaces() {
7272
Map<String, Command> knownCommands = commandMapKnownCommands.get((SimpleCommandMap) commandAPIBukkit.getPaper().getCommandMap());
73-
CommandDispatcher<Source> resourcesDispatcher = getResourcesDispatcher.get();
73+
RootCommandNode<Source> resourcesRootNode = getResourcesDispatcher.get().getRoot();
7474

7575
// Remove namespaces
7676
for (String command : namespacesToFix) {
7777
knownCommands.remove(command);
78-
removeBrigadierCommands(resourcesDispatcher, command, false, c -> true);
78+
removeBrigadierCommands(resourcesRootNode, command, false, c -> true);
7979
}
8080

8181
// Add back certain minecraft: namespace commands
82-
RootCommandNode<Source> resourcesRootNode = resourcesDispatcher.getRoot();
8382
RootCommandNode<Source> brigadierRootNode = brigadierDispatcher.getRoot();
8483
for (CommandNode<Source> node : minecraftCommandNamespaces.getChildren()) {
8584
knownCommands.put(node.getName(), commandAPIBukkit.wrapToVanillaCommandWrapper(node));
@@ -92,7 +91,7 @@ private void fixNamespaces() {
9291
brigadierRootNode.addChild(node);
9392
}
9493
// Clear minecraftCommandNamespaces for dealing with command conflicts after the server is enabled
95-
// See `CommandAPIBukkit#postCommandRegistration`
94+
// See `SpigotCommandRegistration#postCommandRegistration`
9695
minecraftCommandNamespaces = new RootCommandNode<>();
9796
}
9897

@@ -264,7 +263,7 @@ public void unregister(String commandName, boolean unregisterNamespaces, boolean
264263
// Remove nodes from the Vanilla dispatcher
265264
// This dispatcher doesn't usually have namespaced version of commands (those are created when commands
266265
// are transferred to Bukkit's CommandMap), but if they ask, we'll do it
267-
removeBrigadierCommands(brigadierDispatcher, commandName, unregisterNamespaces, c -> true);
266+
removeBrigadierCommands(brigadierDispatcher.getRoot(), commandName, unregisterNamespaces, c -> true);
268267

269268
// Update the dispatcher file
270269
CommandAPIHandler.getInstance().writeDispatcherToFile();
@@ -292,7 +291,7 @@ public void unregister(String commandName, boolean unregisterNamespaces, boolean
292291
// Remove commands from the resources dispatcher
293292
// If we are unregistering a Bukkit command, ONLY unregister BukkitCommandWrappers
294293
// If we are unregistering a Vanilla command, DO NOT unregister BukkitCommandWrappers
295-
removeBrigadierCommands(getResourcesDispatcher.get(), commandName, unregisterNamespaces,
294+
removeBrigadierCommands(getResourcesDispatcher.get().getRoot(), commandName, unregisterNamespaces,
296295
c -> !unregisterBukkit ^ commandAPIBukkit.isBukkitCommandWrapper(c));
297296
}
298297
}

0 commit comments

Comments
 (0)