summaryrefslogtreecommitdiffstats
path: root/nms-patches/CommandDispatcher.patch
blob: 5939296b65bb29c559d9126a83820d82bfcd3c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
--- a/net/minecraft/server/CommandDispatcher.java
+++ b/net/minecraft/server/CommandDispatcher.java
@@ -27,12 +27,21 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import com.google.common.base.Joiner;
+import java.util.LinkedHashSet;
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
+import org.bukkit.event.player.PlayerCommandSendEvent;
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
 public class CommandDispatcher {
 
     private static final Logger a = LogManager.getLogger();
     private final com.mojang.brigadier.CommandDispatcher<CommandListenerWrapper> b = new com.mojang.brigadier.CommandDispatcher();
 
-    public CommandDispatcher(boolean flag) {
+    // CraftBukkit start
+    public final CommandDispatcher init(boolean flag) {
         CommandAdvancement.a(this.b);
         CommandExecute.a(this.b);
         CommandBossBar.a(this.b);
@@ -102,6 +111,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);
         });
+        return this;
+    }
+
+    public CommandDispatcher() {
+        // CraftBukkit end
         this.b.setConsumer((commandcontext, flag, i) -> {
             ((CommandListenerWrapper) commandcontext.getSource()).a(commandcontext, flag, i);
         });
@@ -116,7 +130,47 @@
 
     }
 
+    // CraftBukkit start
+    public int dispatchServerCommand(CommandListenerWrapper sender, String command) {
+        Joiner joiner = Joiner.on(" ");
+        if (command.startsWith("/")) {
+            command = command.substring(1);
+        }
+
+        ServerCommandEvent event = new ServerCommandEvent(sender.getBukkitSender(), command);
+        org.bukkit.Bukkit.getPluginManager().callEvent(event);
+        if (event.isCancelled()) {
+            return 0;
+        }
+        command = event.getCommand();
+
+        String[] args = command.split(" ");
+
+        String cmd = args[0];
+        if (cmd.startsWith("minecraft:")) cmd = cmd.substring("minecraft:".length());
+        if (cmd.startsWith("bukkit:")) cmd = cmd.substring("bukkit:".length());
+
+        // Block disallowed commands
+        if (cmd.equalsIgnoreCase("stop") || cmd.equalsIgnoreCase("kick") || cmd.equalsIgnoreCase("op")
+                || cmd.equalsIgnoreCase("deop") || cmd.equalsIgnoreCase("ban") || cmd.equalsIgnoreCase("ban-ip")
+                || cmd.equalsIgnoreCase("pardon") || cmd.equalsIgnoreCase("pardon-ip") || cmd.equalsIgnoreCase("reload")) {
+            return 0;
+        }
+
+        // Handle vanilla commands;
+        if (sender.getWorld().getServer().getCommandBlockOverride(args[0])) {
+            args[0] = "minecraft:" + args[0];
+        }
+
+        return this.a(sender, joiner.join(args));
+    }
+
     public int a(CommandListenerWrapper commandlistenerwrapper, String s) {
+        return this.a(commandlistenerwrapper, s, s);
+    }
+
+    public int a(CommandListenerWrapper commandlistenerwrapper, String s, String label) {
+        // CraftBukkit end
         StringReader stringreader = new StringReader(s);
 
         if (stringreader.canRead() && stringreader.peek() == 47) {
@@ -128,7 +182,6 @@
         byte b0;
 
         try {
-            byte b1;
             ChatComponentText chatcomponenttext;
 
             try {
@@ -137,52 +190,56 @@
                 return i;
             } catch (CommandException commandexception) {
                 commandlistenerwrapper.sendFailureMessage(commandexception.a());
-                b1 = 0;
-                return b1;
+                b0 = 0;
+                return b0;
             } catch (CommandSyntaxException commandsyntaxexception) {
                 commandlistenerwrapper.sendFailureMessage(ChatComponentUtils.a(commandsyntaxexception.getRawMessage()));
                 if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
                     int j = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
-                    IChatBaseComponent ichatbasecomponent = (new ChatComponentText("")).a(EnumChatFormat.GRAY).a((chatmodifier) -> {
-                        chatmodifier.setChatClickable(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, s));
-                    });
 
+                    chatcomponenttext = new ChatComponentText("");
                     if (j > 10) {
-                        ichatbasecomponent.a("...");
+                        chatcomponenttext.a("...");
                     }
 
-                    ichatbasecomponent.a(commandsyntaxexception.getInput().substring(Math.max(0, j - 10), j));
+                    chatcomponenttext.a(commandsyntaxexception.getInput().substring(Math.max(0, j - 10), j));
                     if (j < commandsyntaxexception.getInput().length()) {
-                        IChatBaseComponent ichatbasecomponent1 = (new ChatComponentText(commandsyntaxexception.getInput().substring(j))).a(new EnumChatFormat[] { EnumChatFormat.RED, EnumChatFormat.UNDERLINE});
+                        ChatComponentText chatcomponenttext1 = new ChatComponentText(commandsyntaxexception.getInput().substring(j));
 
-                        ichatbasecomponent.addSibling(ichatbasecomponent1);
+                        chatcomponenttext1.getChatModifier().setColor(EnumChatFormat.RED);
+                        chatcomponenttext1.getChatModifier().setUnderline(Boolean.valueOf(true));
+                        chatcomponenttext.addSibling(chatcomponenttext1);
                     }
 
-                    ichatbasecomponent.addSibling((new ChatMessage("command.context.here", new Object[0])).a(new EnumChatFormat[] { EnumChatFormat.RED, EnumChatFormat.ITALIC}));
-                    commandlistenerwrapper.sendFailureMessage(ichatbasecomponent);
+                    ChatMessage chatmessage = new ChatMessage("command.context.here", new Object[0]);
+
+                    chatmessage.getChatModifier().setItalic(Boolean.valueOf(true));
+                    chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
+                    chatcomponenttext.addSibling(chatmessage);
+                    chatcomponenttext.getChatModifier().setColor(EnumChatFormat.GRAY);
+                    chatcomponenttext.getChatModifier().setChatClickable(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, label)); // CraftBukkit
+                    commandlistenerwrapper.sendFailureMessage(chatcomponenttext);
                 }
 
-                b1 = 0;
-                return b1;
+                b0 = 0;
             } catch (Exception exception) {
-                chatcomponenttext = new ChatComponentText;
-            }
-
-            chatcomponenttext.<init>(exception.getMessage() == null ? exception.getClass().getName() : exception.getMessage());
-            ChatComponentText chatcomponenttext1 = chatcomponenttext;
+                ChatMessage chatmessage1 = new ChatMessage("command.failed", new Object[0]);
 
-            if (CommandDispatcher.a.isDebugEnabled()) {
-                StackTraceElement[] astacktraceelement = exception.getStackTrace();
+                chatcomponenttext = new ChatComponentText(exception.getMessage() == null ? exception.getClass().getName() : exception.getMessage());
+                if (CommandDispatcher.a.isDebugEnabled()) {
+                    StackTraceElement[] astacktraceelement = exception.getStackTrace();
 
-                for (int k = 0; k < Math.min(astacktraceelement.length, 3); ++k) {
-                    chatcomponenttext1.a("\n\n").a(astacktraceelement[k].getMethodName()).a("\n ").a(astacktraceelement[k].getFileName()).a(":").a(String.valueOf(astacktraceelement[k].getLineNumber()));
+                    for (int k = 0; k < Math.min(astacktraceelement.length, 3); ++k) {
+                        chatcomponenttext.a("\n\n" + astacktraceelement[k].getMethodName() + "\n " + astacktraceelement[k].getFileName() + ":" + astacktraceelement[k].getLineNumber());
+                    }
                 }
-            }
 
-            commandlistenerwrapper.sendFailureMessage((new ChatMessage("command.failed", new Object[0])).a((chatmodifier) -> {
-                chatmodifier.setChatHoverable(new ChatHoverable(ChatHoverable.EnumHoverAction.SHOW_TEXT, ichatbasecomponent));
-            }));
-            b0 = 0;
+                chatmessage1.getChatModifier().setChatHoverable(new ChatHoverable(ChatHoverable.EnumHoverAction.SHOW_TEXT, chatcomponenttext));
+                commandlistenerwrapper.sendFailureMessage(chatmessage1);
+                byte b1 = 0;
+
+                return b1;
+            }
         } finally {
             commandlistenerwrapper.getServer().methodProfiler.e();
         }
@@ -191,11 +248,36 @@
     }
 
     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
+        RootCommandNode vanillaRoot = new RootCommandNode();
+
+        RootCommandNode<CommandListenerWrapper> vanilla = entityplayer.server.vanillaCommandDispatcher.a().getRoot();
+        hashmap.put(vanilla, vanillaRoot);
+        this.a(vanilla, vanillaRoot, entityplayer.getCommandListener(), (Map) hashmap);
+
+        // Now build the global commands in a second pass
+        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));
     }