summaryrefslogtreecommitdiffstats
path: root/nms-patches/CommandBlockListenerAbstract.patch
blob: 359289f0724f8801b2db53501b75e9837bb3bb1b (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
--- a/net/minecraft/server/CommandBlockListenerAbstract.java
+++ b/net/minecraft/server/CommandBlockListenerAbstract.java
@@ -4,6 +4,15 @@
 import java.util.Date;
 import javax.annotation.Nullable;
 
+// CraftBukkit start
+import java.util.ArrayList;
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
+import com.google.common.base.Joiner;
+import java.util.logging.Level;
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
 public abstract class CommandBlockListenerAbstract implements ICommandListener {
 
     private static final SimpleDateFormat a = new SimpleDateFormat("HH:mm:ss");
@@ -15,6 +24,7 @@
     private String g = "";
     private String h = "@";
     private final CommandObjectiveExecutor i = new CommandObjectiveExecutor();
+    protected org.bukkit.command.CommandSender sender; // CraftBukkit - add sender
 
     public CommandBlockListenerAbstract() {}
 
@@ -107,7 +117,9 @@
                 if (minecraftserver != null && minecraftserver.M() && minecraftserver.getEnableCommandBlock()) {
                     try {
                         this.f = null;
-                        this.d = minecraftserver.getCommandHandler().a(this, this.g);
+                        // CraftBukkit start - Handle command block commands using Bukkit dispatcher
+                        this.d = executeSafely(this, sender, this.g);
+                        // CraftBukkit end
                     } catch (Throwable throwable) {
                         CrashReport crashreport = CrashReport.a(throwable, "Executing command block");
                         CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Command to be executed");
@@ -149,6 +161,171 @@
         }
     }
 
+    public static int executeSafely(ICommandListener sender, org.bukkit.command.CommandSender bSender, String command) {
+        try {
+            return executeCommand(sender, bSender, command);
+        } catch (CommandException commandexception) {
+            // Taken from CommandHandler
+            ChatMessage chatmessage = new ChatMessage(commandexception.getMessage(), commandexception.getArgs());
+            chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
+            sender.sendMessage(chatmessage);
+        }
+
+        return 0;
+    }
+
+    // CraftBukkit start
+    public static int executeCommand(ICommandListener sender, org.bukkit.command.CommandSender bSender, String command) throws CommandException {
+        org.bukkit.command.SimpleCommandMap commandMap = sender.getWorld().getServer().getCommandMap();
+        Joiner joiner = Joiner.on(" ");
+        if (command.startsWith("/")) {
+            command = command.substring(1);
+        }
+
+        ServerCommandEvent event = new ServerCommandEvent(bSender, command);
+        org.bukkit.Bukkit.getPluginManager().callEvent(event);
+        if (event.isCancelled()) {
+            return 0;
+        }
+        command = event.getCommand();
+
+        String[] args = command.split(" ");
+        ArrayList<String[]> commands = new ArrayList<String[]>();
+
+        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;
+        org.bukkit.command.Command commandBlockCommand = commandMap.getCommand(args[0]);
+        if (sender.getWorld().getServer().getCommandBlockOverride(args[0])) {
+            commandBlockCommand = commandMap.getCommand("minecraft:" + args[0]);
+        }
+        if (commandBlockCommand instanceof VanillaCommandWrapper) {
+            command = command.trim();
+            if (command.startsWith("/")) {
+                command = command.substring(1);
+            }
+            String as[] = command.split(" ");
+            as = VanillaCommandWrapper.dropFirstArgument(as);
+            if (!sender.getWorld().getServer().getPermissionOverride(sender) && !((VanillaCommandWrapper) commandBlockCommand).testPermission(bSender)) {
+                return 0;
+            }
+            return ((VanillaCommandWrapper) commandBlockCommand).dispatchVanillaCommand(bSender, sender, as);
+        }
+
+        // Make sure this is a valid command
+        if (commandMap.getCommand(args[0]) == null) {
+            return 0;
+        }
+
+        commands.add(args);
+
+        // Find positions of command block syntax, if any        
+        WorldServer[] prev = MinecraftServer.getServer().worldServer;
+        MinecraftServer server = MinecraftServer.getServer();
+        server.worldServer = new WorldServer[server.worlds.size()];
+        server.worldServer[0] = (WorldServer) sender.getWorld();
+        int bpos = 0;
+        for (int pos = 1; pos < server.worldServer.length; pos++) {
+            WorldServer world = server.worlds.get(bpos++);
+            if (server.worldServer[0] == world) {
+                pos--;
+                continue;
+            }
+            server.worldServer[pos] = world;
+        }
+        try {
+            ArrayList<String[]> newCommands = new ArrayList<String[]>();
+            for (int i = 0; i < args.length; i++) {
+                if (PlayerSelector.isPattern(args[i])) {
+                    for (int j = 0; j < commands.size(); j++) {
+                        newCommands.addAll(buildCommands(sender, commands.get(j), i));
+                    }
+                    ArrayList<String[]> temp = commands;
+                    commands = newCommands;
+                    newCommands = temp;
+                    newCommands.clear();
+                }
+            }
+        } finally {
+            MinecraftServer.getServer().worldServer = prev;
+        }
+
+        int completed = 0;
+
+        // Now dispatch all of the commands we ended up with
+        for (int i = 0; i < commands.size(); i++) {
+            try {
+                if (commandMap.dispatch(bSender, joiner.join(java.util.Arrays.asList(commands.get(i))))) {
+                    completed++;
+                }
+            } catch (Throwable exception) {
+                if (sender.f() instanceof EntityMinecartCommandBlock) {
+                    MinecraftServer.getServer().server.getLogger().log(Level.WARNING, String.format("MinecartCommandBlock at (%d,%d,%d) failed to handle command", sender.getChunkCoordinates().getX(), sender.getChunkCoordinates().getY(), sender.getChunkCoordinates().getZ()), exception);
+                } else if (sender instanceof CommandBlockListenerAbstract) {
+                    CommandBlockListenerAbstract listener = (CommandBlockListenerAbstract) sender;
+                    MinecraftServer.getServer().server.getLogger().log(Level.WARNING, String.format("CommandBlock at (%d,%d,%d) failed to handle command", listener.getChunkCoordinates().getX(), listener.getChunkCoordinates().getY(), listener.getChunkCoordinates().getZ()), exception);
+                } else {
+                    MinecraftServer.getServer().server.getLogger().log(Level.WARNING, String.format("Unknown CommandBlock failed to handle command"), exception);
+                }
+            }
+        }
+
+        return completed;
+    }
+
+    private static ArrayList<String[]> buildCommands(ICommandListener sender, String[] args, int pos) throws CommandException {
+        ArrayList<String[]> commands = new ArrayList<String[]>();
+        java.util.List<EntityPlayer> players = (java.util.List<EntityPlayer>)PlayerSelector.getPlayers(sender, args[pos], EntityPlayer.class);
+
+        if (players != null) {
+            for (EntityPlayer player : players) {
+                if (player.world != sender.getWorld()) {
+                    continue;
+                }
+                String[] command = args.clone();
+                command[pos] = player.getName();
+                commands.add(command);
+            }
+        }
+
+        return commands;
+    }
+
+    public static CommandSender unwrapSender(ICommandListener listener) {
+        org.bukkit.command.CommandSender sender = null;
+        while (sender == null) {
+            if (listener instanceof DedicatedServer) {
+                sender = ((DedicatedServer) listener).console;
+            } else if (listener instanceof RemoteControlCommandListener) {
+                sender = ((RemoteControlCommandListener) listener).C_().remoteConsole;
+            } else if (listener instanceof CommandBlockListenerAbstract) {
+                sender = ((CommandBlockListenerAbstract) listener).sender;
+            } else if (listener instanceof CustomFunctionData.CustomFunctionListener) {
+                sender = ((CustomFunctionData.CustomFunctionListener) listener).sender;
+            } else if (listener instanceof CommandListenerWrapper) {
+                listener = ((CommandListenerWrapper) listener).base; // Search deeper
+            } else if (VanillaCommandWrapper.lastSender != null) {
+                sender = VanillaCommandWrapper.lastSender;
+            } else if (listener.f() != null) {
+                sender = listener.f().getBukkitEntity();
+            } else {
+                throw new RuntimeException("Unhandled executor " + listener.getClass().getSimpleName());
+            }
+        }
+
+        return sender;
+    }
+    // CraftBukkit end
+
     public String getName() {
         return this.h;
     }