|
7 | 7 | import com.mojang.brigadier.tree.RootCommandNode; |
8 | 8 |
|
9 | 9 | import java.util.List; |
| 10 | +import java.util.function.Predicate; |
10 | 11 | import java.util.function.Supplier; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Handles logic for registering commands after Paper build 65, where <a href="https://github.com/PaperMC/Paper/pull/8235">https://github.com/PaperMC/Paper/pull/8235</a> |
14 | 15 | * changed a bunch of the behind-the-scenes logic. |
15 | 16 | */ |
16 | 17 | public class PaperCommandRegistration<Source> extends CommandRegistrationStrategy<Source> { |
17 | | - // References to necessary objects |
| 18 | + // References to necessary methods |
18 | 19 | private final Supplier<CommandDispatcher<Source>> getBrigadierDispatcher; |
| 20 | + private final Predicate<CommandNode<Source>> isBukkitCommand; |
19 | 21 |
|
20 | 22 | // Store registered commands nodes for eventual reloads |
21 | 23 | private final RootCommandNode<Source> registeredNodes = new RootCommandNode<>(); |
22 | 24 |
|
23 | | - public PaperCommandRegistration(Supplier<CommandDispatcher<Source>> getBrigadierDispatcher) { |
| 25 | + public PaperCommandRegistration( |
| 26 | + Supplier<CommandDispatcher<Source>> getBrigadierDispatcher, Predicate<CommandNode<Source>> isBukkitCommand |
| 27 | + ) { |
24 | 28 | this.getBrigadierDispatcher = getBrigadierDispatcher; |
| 29 | + this.isBukkitCommand = isBukkitCommand; |
25 | 30 | } |
26 | 31 |
|
| 32 | + // Provide access to internal functions that may be useful to developers |
| 33 | + /** |
| 34 | + * Checks if a Brigadier command node came from wrapping a Bukkit command |
| 35 | + * |
| 36 | + * @param node The CommandNode to check |
| 37 | + * @return true if the CommandNode is being handled by Paper's BukkitCommandNode |
| 38 | + */ |
| 39 | + public boolean isBukkitCommand(CommandNode<Source> node) { |
| 40 | + return isBukkitCommand.test(node); |
| 41 | + } |
| 42 | + |
| 43 | + // Implement CommandRegistrationStrategy methods |
27 | 44 | @Override |
28 | 45 | public CommandDispatcher<Source> getBrigadierDispatcher() { |
29 | 46 | return getBrigadierDispatcher.get(); |
@@ -57,10 +74,16 @@ public LiteralCommandNode<Source> registerCommandNode(LiteralArgumentBuilder<Sou |
57 | 74 | @Override |
58 | 75 | public void unregister(String commandName, boolean unregisterNamespaces, boolean unregisterBukkit) { |
59 | 76 | // Remove nodes from the dispatcher |
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); |
| 77 | + removeBrigadierCommands(getBrigadierDispatcher.get().getRoot(), commandName, unregisterNamespaces, |
| 78 | + // If we are unregistering a Bukkit command, ONLY unregister BukkitCommandNodes |
| 79 | + // If we are unregistering a Vanilla command, DO NOT unregister BukkitCommandNodes |
| 80 | + c -> !unregisterBukkit ^ isBukkitCommand.test(c)); |
| 81 | + |
| 82 | + // CommandAPI commands count as non-Bukkit |
| 83 | + if (!unregisterBukkit) { |
| 84 | + // Don't add nodes back after a reload |
| 85 | + removeBrigadierCommands(registeredNodes, commandName, unregisterNamespaces, c -> true); |
| 86 | + } |
64 | 87 |
|
65 | 88 | // Update the dispatcher file |
66 | 89 | CommandAPIHandler.getInstance().writeDispatcherToFile(); |
|
0 commit comments