mirror of
https://github.com/PaperMC/Paper.git
synced 2025-03-02 11:22:01 +01:00
emulate old syncCommands behavior for CommandRegisteredEvent
This commit is contained in:
parent
d9fb3effa1
commit
5f066a9c47
@ -4,32 +4,111 @@ Date: Wed, 29 May 2024 13:15:43 -0700
|
||||
Subject: [PATCH] Fix CommandRegisteredEvent not called
|
||||
|
||||
|
||||
diff --git a/src/main/java/io/papermc/paper/command/brigadier/LazyCommandNode.java b/src/main/java/io/papermc/paper/command/brigadier/LazyCommandNode.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..2ca3ca97874c7a8184ee0ef951c15bacf4d4eadb
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/command/brigadier/LazyCommandNode.java
|
||||
@@ -0,0 +1,21 @@
|
||||
+package io.papermc.paper.command.brigadier;
|
||||
+
|
||||
+import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
+import java.util.function.Supplier;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public class LazyCommandNode<S> extends LiteralCommandNode<S> {
|
||||
+ private final Supplier<@Nullable LiteralCommandNode<S>> nodeSupplier;
|
||||
+
|
||||
+ public LazyCommandNode(final String literal, final Supplier<@Nullable LiteralCommandNode<S>> nodeSupplier) {
|
||||
+ super(literal, null, null, null, null, false);
|
||||
+ this.nodeSupplier = nodeSupplier;
|
||||
+ }
|
||||
+
|
||||
+ public @Nullable LiteralCommandNode<S> create() {
|
||||
+ return this.nodeSupplier.get();
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitBrigForwardingMap.java b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitBrigForwardingMap.java
|
||||
index f0cc27640bb3db275295a298d608c9d9f88df617..604438b10e3746837cda26cbf0ae21056f05c245 100644
|
||||
index f0cc27640bb3db275295a298d608c9d9f88df617..566bf6729e4e0a768b8ec3a2e574ae681f8eaf7b 100644
|
||||
--- a/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitBrigForwardingMap.java
|
||||
+++ b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitBrigForwardingMap.java
|
||||
@@ -3,7 +3,6 @@ package io.papermc.paper.command.brigadier.bukkit;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
@@ -5,6 +5,7 @@ import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
-import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
+import io.papermc.paper.command.brigadier.LazyCommandNode;
|
||||
import io.papermc.paper.command.brigadier.PaperBrigadier;
|
||||
import io.papermc.paper.command.brigadier.PaperCommands;
|
||||
@@ -95,7 +94,10 @@ public class BukkitBrigForwardingMap extends HashMap<String, Command> {
|
||||
import java.util.AbstractCollection;
|
||||
@@ -13,6 +14,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
+import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
@@ -33,6 +35,7 @@ public class BukkitBrigForwardingMap extends HashMap<String, Command> {
|
||||
private final EntrySet entrySet = new EntrySet();
|
||||
private final KeySet keySet = new KeySet();
|
||||
private final Values values = new Values();
|
||||
+ public boolean synced = false;
|
||||
|
||||
// Previous dispatcher used to get commands to migrate to another dispatcher
|
||||
|
||||
@@ -40,6 +43,27 @@ public class BukkitBrigForwardingMap extends HashMap<String, Command> {
|
||||
return PaperCommands.INSTANCE.getDispatcherInternal();
|
||||
}
|
||||
|
||||
+ public void onSync() {
|
||||
+ this.synced = true;
|
||||
+ final List<String> remove = new ArrayList<>();
|
||||
+ final List<LiteralCommandNode<CommandSourceStack>> list = new ArrayList<>();
|
||||
+ for (final CommandNode<CommandSourceStack> child : this.getDispatcher().getRoot().getChildren()) {
|
||||
+ if (child instanceof LazyCommandNode<CommandSourceStack> lazy) {
|
||||
+ remove.add(child.getName());
|
||||
+ final LiteralCommandNode<CommandSourceStack> newNode = lazy.create();
|
||||
+ if (newNode != null) {
|
||||
+ list.add(newNode);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ for (final String removeName : remove) {
|
||||
+ this.getDispatcher().getRoot().removeCommand(removeName);
|
||||
+ }
|
||||
+ for (final LiteralCommandNode<CommandSourceStack> node : list) {
|
||||
+ this.getDispatcher().getRoot().addChild(node);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
@Override
|
||||
public int size() {
|
||||
return this.getDispatcher().getRoot().getChildren().size();
|
||||
@@ -95,7 +119,18 @@ public class BukkitBrigForwardingMap extends HashMap<String, Command> {
|
||||
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));
|
||||
+ final BukkitCommandNode node = BukkitCommandNode.create(key, value, this.getDispatcher());
|
||||
+ if (node != null) {
|
||||
+ if (!this.synced) {
|
||||
+ final LazyCommandNode<CommandSourceStack> node = new LazyCommandNode<>(
|
||||
+ key,
|
||||
+ () -> BukkitCommandNode.create(key, value, this.getDispatcher())
|
||||
+ );
|
||||
+ this.getDispatcher().getRoot().addChild(node);
|
||||
+ } else {
|
||||
+ final BukkitCommandNode node = BukkitCommandNode.create(key, value, this.getDispatcher());
|
||||
+ if (node != null) {
|
||||
+ this.getDispatcher().getRoot().addChild(node);
|
||||
+ }
|
||||
+ }
|
||||
return old;
|
||||
}
|
||||
|
||||
diff --git a/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java
|
||||
index 10a113b057b0a4d27cce3bae975e1108aaa7b517..ca9572bc5d742978932118b7703e807c67ecc9a8 100644
|
||||
index 10a113b057b0a4d27cce3bae975e1108aaa7b517..5d2d2bf7e170a4546d687f10305d28ebc7c02037 100644
|
||||
--- a/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java
|
||||
+++ b/src/main/java/io/papermc/paper/command/brigadier/bukkit/BukkitCommandNode.java
|
||||
@@ -1,6 +1,7 @@
|
||||
@ -65,7 +144,7 @@ index 10a113b057b0a4d27cce3bae975e1108aaa7b517..ca9572bc5d742978932118b7703e807c
|
||||
|
||||
public class BukkitCommandNode extends LiteralCommandNode<CommandSourceStack> {
|
||||
|
||||
@@ -43,16 +53,48 @@ public class BukkitCommandNode extends LiteralCommandNode<CommandSourceStack> {
|
||||
@@ -43,16 +53,51 @@ public class BukkitCommandNode extends LiteralCommandNode<CommandSourceStack> {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
@ -83,7 +162,7 @@ index 10a113b057b0a4d27cce3bae975e1108aaa7b517..ca9572bc5d742978932118b7703e807c
|
||||
+ return newNode;
|
||||
+ }
|
||||
+
|
||||
+ @SuppressWarnings({"rawtypes", "removal", "deprecation", "unchecked"})
|
||||
+ @SuppressWarnings({"rawtypes", "removal", "deprecation", "unchecked", "ConstantValue"})
|
||||
+ public static BukkitCommandNode create(String name, Command command, CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
BukkitBrigCommand bukkitBrigCommand = new BukkitBrigCommand(command, name);
|
||||
- BukkitCommandNode commandNode = new BukkitCommandNode(name, command, bukkitBrigCommand);
|
||||
@ -99,6 +178,9 @@ index 10a113b057b0a4d27cce3bae975e1108aaa7b517..ca9572bc5d742978932118b7703e807c
|
||||
+ .executes(bukkitBrigCommand)
|
||||
+ .build();
|
||||
+ commandNode.addChild(defaultArgs);
|
||||
+ if (Bukkit.getPluginManager() == null) {
|
||||
+ return (BukkitCommandNode) commandNode;
|
||||
+ }
|
||||
+ LiteralCommandNode<CommandSourceStack> defaultNode = commandNode;
|
||||
+ com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent<net.minecraft.commands.CommandSourceStack> event =
|
||||
+ new com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent<>(
|
||||
@ -121,3 +203,15 @@ index 10a113b057b0a4d27cce3bae975e1108aaa7b517..ca9572bc5d742978932118b7703e807c
|
||||
}
|
||||
|
||||
public Command getBukkitCommand() {
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
||||
index 94a31c8f903eb61eb6d203e8e6fe8fb0beca28b1..d0adb649af02a38cf9c1738d5513ef89eebded73 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
||||
@@ -584,6 +584,7 @@ public final class CraftServer implements Server {
|
||||
for (ServerPlayer player : this.getHandle().players) {
|
||||
dispatcher.sendCommands(player);
|
||||
}
|
||||
+ io.papermc.paper.command.brigadier.bukkit.BukkitBrigForwardingMap.INSTANCE.onSync(); // Paper - CommandRegisteredEvent
|
||||
}
|
||||
|
||||
private void enablePlugin(Plugin plugin) {
|
||||
|
Loading…
Reference in New Issue
Block a user