Skip to content

Commit a42c9ad

Browse files
committed
Make PaperCommandRegistration#unregister respect unregisterBukkit parameter
1 parent a4450b7 commit a42c9ad

File tree

2 files changed

+42
-7
lines changed
  • commandapi-platforms/commandapi-bukkit

2 files changed

+42
-7
lines changed

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,40 @@
77
import com.mojang.brigadier.tree.RootCommandNode;
88

99
import java.util.List;
10+
import java.util.function.Predicate;
1011
import java.util.function.Supplier;
1112

1213
/**
1314
* 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>
1415
* changed a bunch of the behind-the-scenes logic.
1516
*/
1617
public class PaperCommandRegistration<Source> extends CommandRegistrationStrategy<Source> {
17-
// References to necessary objects
18+
// References to necessary methods
1819
private final Supplier<CommandDispatcher<Source>> getBrigadierDispatcher;
20+
private final Predicate<CommandNode<Source>> isBukkitCommand;
1921

2022
// Store registered commands nodes for eventual reloads
2123
private final RootCommandNode<Source> registeredNodes = new RootCommandNode<>();
2224

23-
public PaperCommandRegistration(Supplier<CommandDispatcher<Source>> getBrigadierDispatcher) {
25+
public PaperCommandRegistration(
26+
Supplier<CommandDispatcher<Source>> getBrigadierDispatcher, Predicate<CommandNode<Source>> isBukkitCommand
27+
) {
2428
this.getBrigadierDispatcher = getBrigadierDispatcher;
29+
this.isBukkitCommand = isBukkitCommand;
2530
}
2631

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
2744
@Override
2845
public CommandDispatcher<Source> getBrigadierDispatcher() {
2946
return getBrigadierDispatcher.get();
@@ -57,10 +74,16 @@ public LiteralCommandNode<Source> registerCommandNode(LiteralArgumentBuilder<Sou
5774
@Override
5875
public void unregister(String commandName, boolean unregisterNamespaces, boolean unregisterBukkit) {
5976
// 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+
}
6487

6588
// Update the dispatcher file
6689
CommandAPIHandler.getInstance().writeDispatcherToFile();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,19 @@ public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistration
10591059
node -> node.getCommand() instanceof BukkitCommandWrapper
10601060
);
10611061
} else {
1062-
return new PaperCommandRegistration<>(() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher());
1062+
// This class is Paper-server specific, so we need to use paper's userdev plugin to
1063+
// access it directly. That might need gradle, but there might also be a maven version?
1064+
// https://discord.com/channels/289587909051416579/1121227200277004398/1246910745761812480
1065+
Class<?> bukkitCommandNode_bukkitBrigCommand;
1066+
try {
1067+
bukkitCommandNode_bukkitBrigCommand = Class.forName("io.papermc.paper.command.brigadier.bukkit.BukkitCommandNode$BukkitBrigCommand");
1068+
} catch (ClassNotFoundException e) {
1069+
throw new IllegalStateException("Expected to find class", e);
1070+
}
1071+
return new PaperCommandRegistration<>(
1072+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher(),
1073+
node -> bukkitCommandNode_bukkitBrigCommand.isInstance(node.getCommand())
1074+
);
10631075
}
10641076
}
10651077
}

0 commit comments

Comments
 (0)