Preserve command node when re-registering modern commands through old API (#11184)

* Preserve command node when re-registering modern commands through old API

* make fix more targeted

mainly because it didn't work for vanilla commands :)
This commit is contained in:
Jason Penilla 2024-07-30 11:42:55 -07:00
parent 301caeb6d0
commit 86d0c737cc

View file

@ -1517,10 +1517,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import com.google.common.collect.Iterators;
+import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.tree.CommandNode;
+import com.mojang.brigadier.tree.LiteralCommandNode;
+import io.papermc.paper.command.brigadier.CommandSourceStack;
+import io.papermc.paper.command.brigadier.PaperBrigadier;
+import io.papermc.paper.command.brigadier.PaperCommands;
+import io.papermc.paper.command.brigadier.PluginVanillaCommandWrapper;
+import java.util.AbstractCollection;
+import java.util.AbstractSet;
+import java.util.ArrayList;
@ -1604,12 +1604,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return PaperBrigadier.wrapNode(node);
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Nullable
+ @Override
+ public Command put(String key, Command value) {
+ Command old = this.get(key);
+ this.getDispatcher().getRoot().removeCommand(key); // Override previous command
+ this.getDispatcher().getRoot().addChild(BukkitCommandNode.of(key, value));
+ if (value instanceof PluginVanillaCommandWrapper wrapper && wrapper.getName().equals(key)) {
+ // Don't break when some plugin tries to remove and add back a plugin command registered with modern API...
+ this.getDispatcher().getRoot().addChild((CommandNode) wrapper.vanillaCommand);
+ } else {
+ this.getDispatcher().getRoot().addChild(BukkitCommandNode.of(key, value));
+ }
+ return old;
+ }
+