SPIGOT-4029: Add event for commands being sent to client

This commit is contained in:
md_5 2018-07-31 10:46:09 +10:00
parent 5cfa68387f
commit 549d9b0120
2 changed files with 31 additions and 7 deletions

View File

@ -1,13 +1,14 @@
--- a/net/minecraft/server/CommandDispatcher.java
+++ b/net/minecraft/server/CommandDispatcher.java
@@ -26,12 +26,20 @@
@@ -26,12 +26,21 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+// CraftBukkit start
+import com.google.common.base.Joiner;
+import org.apache.logging.log4j.Level;
+import java.util.LinkedHashSet;
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
+import org.bukkit.event.player.PlayerCommandSendEvent;
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
@ -22,7 +23,7 @@
CommandAdvancement.a(this.b);
CommandExecute.a(this.b);
CommmandBossBar.a(this.b);
@@ -100,6 +108,11 @@
@@ -100,6 +109,11 @@
this.b.findAmbiguities((commandnode, commandnode1, commandnode2, collection) -> {
CommandDispatcher.a.warn("Ambiguity between arguments {} and {} with inputs: {}", this.b.getPath(commandnode1), this.b.getPath(commandnode2), collection);
});
@ -34,7 +35,7 @@
this.b.setConsumer((commandcontext, flag, i) -> {
((CommandListenerWrapper) commandcontext.getSource()).a(commandcontext, flag, i);
});
@@ -114,8 +127,49 @@
@@ -114,8 +128,49 @@
}
@ -85,7 +86,7 @@
if (s.startsWith("/")) {
s = s.substring(1);
@@ -126,7 +180,6 @@
@@ -126,7 +181,6 @@
byte b0;
try {
@ -93,7 +94,7 @@
ChatComponentText chatcomponenttext;
try {
@@ -135,65 +188,80 @@
@@ -135,65 +189,95 @@
return i;
} catch (CommandException commandexception) {
commandlistenerwrapper.sendFailureMessage(commandexception.a());
@ -184,6 +185,7 @@
public void a(EntityPlayer entityplayer) {
- HashMap hashmap = Maps.newHashMap();
- RootCommandNode rootcommandnode = new RootCommandNode();
+ // CraftBukkit start
+ // Register Vanilla commands into builtRoot as before
+ Map hashmap = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
@ -194,10 +196,25 @@
+ this.a(vanilla, vanillaRoot, entityplayer.getCommandListener(), (Map) hashmap);
+
+ // Now build the global commands in a second pass
RootCommandNode rootcommandnode = new RootCommandNode();
+ RootCommandNode<ICompletionProvider> rootcommandnode = new RootCommandNode();
hashmap.put(this.b.getRoot(), rootcommandnode);
this.a(this.b.getRoot(), rootcommandnode, entityplayer.getCommandListener(), (Map) hashmap);
+
+ Collection<String> bukkit = new LinkedHashSet<>();
+ for (CommandNode node : rootcommandnode.getChildren()) {
+ bukkit.add(node.getName());
+ }
+
+ PlayerCommandSendEvent event = new PlayerCommandSendEvent(entityplayer.getBukkitEntity(), new LinkedHashSet<>(bukkit));
+ event.getPlayer().getServer().getPluginManager().callEvent(event);
+
+ // Remove labels that were removed during the event
+ for (String orig : bukkit) {
+ if (!event.getCommands().contains(orig)) {
+ rootcommandnode.removeCommand(orig);
+ }
+ }
+ // CraftBukkit end
entityplayer.playerConnection.sendPacket(new PacketPlayOutCommands(rootcommandnode));
}

View File

@ -34,6 +34,13 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private final RedirectModifier<S> modifier;
private final boolean forks;
private Command<S> command;
// CraftBukkit start
public void removeCommand(String name) {
children.remove(name);
literals.remove(name);
arguments.remove(name);
}
// CraftBukkit end
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
this.command = command;