2015-05-25 12:37:24 +02:00
|
|
|
--- a/net/minecraft/server/CommandBlockListenerAbstract.java
|
|
|
|
+++ b/net/minecraft/server/CommandBlockListenerAbstract.java
|
2017-07-28 10:10:38 +02:00
|
|
|
@@ -4,6 +4,15 @@
|
2014-11-25 22:32:16 +01:00
|
|
|
import java.util.Date;
|
2016-05-10 13:47:39 +02:00
|
|
|
import javax.annotation.Nullable;
|
2014-11-25 22:32:16 +01:00
|
|
|
|
|
|
|
+// CraftBukkit start
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
|
|
|
|
+import com.google.common.base.Joiner;
|
2015-02-26 23:41:06 +01:00
|
|
|
+import java.util.logging.Level;
|
2017-06-26 00:27:25 +02:00
|
|
|
+import org.bukkit.command.CommandSender;
|
2017-07-28 10:10:38 +02:00
|
|
|
+import org.bukkit.event.server.ServerCommandEvent;
|
2014-11-25 22:32:16 +01:00
|
|
|
+// CraftBukkit end
|
|
|
|
+
|
|
|
|
public abstract class CommandBlockListenerAbstract implements ICommandListener {
|
|
|
|
|
|
|
|
private static final SimpleDateFormat a = new SimpleDateFormat("HH:mm:ss");
|
2017-07-28 10:10:38 +02:00
|
|
|
@@ -15,6 +24,7 @@
|
2017-05-14 04:00:00 +02:00
|
|
|
private String g = "";
|
|
|
|
private String h = "@";
|
|
|
|
private final CommandObjectiveExecutor i = new CommandObjectiveExecutor();
|
2014-11-25 22:32:16 +01:00
|
|
|
+ protected org.bukkit.command.CommandSender sender; // CraftBukkit - add sender
|
|
|
|
|
|
|
|
public CommandBlockListenerAbstract() {}
|
|
|
|
|
2017-07-28 10:10:38 +02:00
|
|
|
@@ -107,7 +117,9 @@
|
2017-05-14 04:00:00 +02:00
|
|
|
if (minecraftserver != null && minecraftserver.M() && minecraftserver.getEnableCommandBlock()) {
|
|
|
|
try {
|
|
|
|
this.f = null;
|
2017-05-19 13:00:13 +02:00
|
|
|
- this.d = minecraftserver.getCommandHandler().a(this, this.g);
|
2017-05-14 04:00:00 +02:00
|
|
|
+ // 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");
|
2017-07-28 10:10:38 +02:00
|
|
|
@@ -149,6 +161,171 @@
|
2016-02-29 22:32:46 +01:00
|
|
|
}
|
2015-02-26 23:41:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:03 +01:00
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ // CraftBukkit start
|
2016-11-17 02:41:03 +01:00
|
|
|
+ public static int executeCommand(ICommandListener sender, org.bukkit.command.CommandSender bSender, String command) throws CommandException {
|
2014-12-06 00:10:47 +01:00
|
|
|
+ org.bukkit.command.SimpleCommandMap commandMap = sender.getWorld().getServer().getCommandMap();
|
|
|
|
+ Joiner joiner = Joiner.on(" ");
|
|
|
|
+ if (command.startsWith("/")) {
|
|
|
|
+ command = command.substring(1);
|
|
|
|
+ }
|
2017-07-28 10:10:38 +02:00
|
|
|
+
|
|
|
|
+ ServerCommandEvent event = new ServerCommandEvent(bSender, command);
|
|
|
|
+ org.bukkit.Bukkit.getPluginManager().callEvent(event);
|
|
|
|
+ if (event.isCancelled()) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ command = event.getCommand();
|
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ String[] args = command.split(" ");
|
|
|
|
+ ArrayList<String[]> commands = new ArrayList<String[]>();
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-23 02:00:17 +01:00
|
|
|
+ String cmd = args[0];
|
|
|
|
+ if (cmd.startsWith("minecraft:")) cmd = cmd.substring("minecraft:".length());
|
|
|
|
+ if (cmd.startsWith("bukkit:")) cmd = cmd.substring("bukkit:".length());
|
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ // Block disallowed commands
|
2014-12-23 02:00:17 +01:00
|
|
|
+ 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")) {
|
2014-12-06 00:10:47 +01:00
|
|
|
+ return 0;
|
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ // 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) {
|
2014-12-11 21:05:48 +01:00
|
|
|
+ command = command.trim();
|
|
|
|
+ if (command.startsWith("/")) {
|
|
|
|
+ command = command.substring(1);
|
|
|
|
+ }
|
|
|
|
+ String as[] = command.split(" ");
|
|
|
|
+ as = VanillaCommandWrapper.dropFirstArgument(as);
|
2017-08-06 01:07:24 +02:00
|
|
|
+ if (!sender.getWorld().getServer().getPermissionOverride(sender) && !((VanillaCommandWrapper) commandBlockCommand).testPermission(bSender)) {
|
2014-12-27 15:58:47 +01:00
|
|
|
+ return 0;
|
|
|
|
+ }
|
2015-01-25 16:17:48 +01:00
|
|
|
+ return ((VanillaCommandWrapper) commandBlockCommand).dispatchVanillaCommand(bSender, sender, as);
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ // Make sure this is a valid command
|
|
|
|
+ if (commandMap.getCommand(args[0]) == null) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2015-02-26 23:41:06 +01:00
|
|
|
+ commands.add(args);
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2015-02-26 23:41:06 +01:00
|
|
|
+ // 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;
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+ server.worldServer[pos] = world;
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+ 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();
|
2014-11-25 22:32:16 +01:00
|
|
|
+ }
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+ } finally {
|
|
|
|
+ MinecraftServer.getServer().worldServer = prev;
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ int completed = 0;
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ // 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++;
|
2014-11-25 22:32:16 +01:00
|
|
|
+ }
|
2014-12-06 00:10:47 +01:00
|
|
|
+ } catch (Throwable exception) {
|
2015-02-26 23:41:06 +01:00
|
|
|
+ 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);
|
2014-12-06 00:10:47 +01:00
|
|
|
+ } else {
|
2015-02-26 23:41:06 +01:00
|
|
|
+ MinecraftServer.getServer().server.getLogger().log(Level.WARNING, String.format("Unknown CommandBlock failed to handle command"), exception);
|
2014-12-06 00:10:47 +01:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+
|
2014-12-06 00:10:47 +01:00
|
|
|
+ return completed;
|
2014-11-25 22:32:16 +01:00
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2016-11-17 02:41:03 +01:00
|
|
|
+ private static ArrayList<String[]> buildCommands(ICommandListener sender, String[] args, int pos) throws CommandException {
|
2014-11-25 22:32:16 +01:00
|
|
|
+ ArrayList<String[]> commands = new ArrayList<String[]>();
|
2014-12-06 00:10:47 +01:00
|
|
|
+ java.util.List<EntityPlayer> players = (java.util.List<EntityPlayer>)PlayerSelector.getPlayers(sender, args[pos], EntityPlayer.class);
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ if (players != null) {
|
|
|
|
+ for (EntityPlayer player : players) {
|
2014-12-06 00:10:47 +01:00
|
|
|
+ if (player.world != sender.getWorld()) {
|
2014-11-25 22:32:16 +01:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ String[] command = args.clone();
|
|
|
|
+ command[pos] = player.getName();
|
|
|
|
+ commands.add(command);
|
|
|
|
+ }
|
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ return commands;
|
2015-02-26 23:41:06 +01:00
|
|
|
+ }
|
2017-06-26 00:27:25 +02:00
|
|
|
+
|
|
|
|
+ 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) {
|
2017-12-25 21:28:56 +01:00
|
|
|
+ sender = ((RemoteControlCommandListener) listener).C_().remoteConsole;
|
2017-06-26 00:27:25 +02:00
|
|
|
+ } 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;
|
|
|
|
+ }
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // CraftBukkit end
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
public String getName() {
|
2017-05-14 04:00:00 +02:00
|
|
|
return this.h;
|
2015-02-26 23:41:06 +01:00
|
|
|
}
|