From de7608559660c92d43e52099325155fa2758561b Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 26 Jun 2017 08:27:25 +1000 Subject: [PATCH] SPIGOT-3376: Fix nested functions --- .../CommandBlockListenerAbstract.patch | 34 ++++++++++++++++--- nms-patches/CommandExecute.patch | 24 ++----------- nms-patches/CustomFunction.patch | 12 +++++-- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/nms-patches/CommandBlockListenerAbstract.patch b/nms-patches/CommandBlockListenerAbstract.patch index da8ee32a40..ba41673ff7 100644 --- a/nms-patches/CommandBlockListenerAbstract.patch +++ b/nms-patches/CommandBlockListenerAbstract.patch @@ -1,6 +1,6 @@ --- a/net/minecraft/server/CommandBlockListenerAbstract.java +++ b/net/minecraft/server/CommandBlockListenerAbstract.java -@@ -4,6 +4,13 @@ +@@ -4,6 +4,14 @@ import java.util.Date; import javax.annotation.Nullable; @@ -9,12 +9,13 @@ +import org.bukkit.craftbukkit.command.VanillaCommandWrapper; +import com.google.common.base.Joiner; +import java.util.logging.Level; ++import org.bukkit.command.CommandSender; +// CraftBukkit end + public abstract class CommandBlockListenerAbstract implements ICommandListener { private static final SimpleDateFormat a = new SimpleDateFormat("HH:mm:ss"); -@@ -15,6 +22,7 @@ +@@ -15,6 +23,7 @@ private String g = ""; private String h = "@"; private final CommandObjectiveExecutor i = new CommandObjectiveExecutor(); @@ -22,7 +23,7 @@ public CommandBlockListenerAbstract() {} -@@ -107,7 +115,9 @@ +@@ -107,7 +116,9 @@ if (minecraftserver != null && minecraftserver.M() && minecraftserver.getEnableCommandBlock()) { try { this.f = null; @@ -33,7 +34,7 @@ } catch (Throwable throwable) { CrashReport crashreport = CrashReport.a(throwable, "Executing command block"); CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Command to be executed"); -@@ -149,6 +159,138 @@ +@@ -149,6 +160,163 @@ } } @@ -167,6 +168,31 @@ + + 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 = ((DedicatedServer) listener).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() { diff --git a/nms-patches/CommandExecute.patch b/nms-patches/CommandExecute.patch index bd31145b08..eef65e5bff 100644 --- a/nms-patches/CommandExecute.patch +++ b/nms-patches/CommandExecute.patch @@ -11,7 +11,7 @@ public class CommandExecute extends CommandAbstract { -@@ -59,26 +63,61 @@ +@@ -59,26 +63,41 @@ } String s = a(astring, b0); @@ -22,27 +22,7 @@ try { - int i = icommandhandler.a(commandlistenerwrapper, s); + // CraftBukkit start -+ org.bukkit.command.CommandSender sender = null; -+ ICommandListener listener = icommandlistener; -+ while (sender == null) { -+ if (listener instanceof DedicatedServer) { -+ sender = minecraftserver.console; -+ } else if (listener instanceof RemoteControlCommandListener) { -+ sender = minecraftserver.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 CommandException("Unhandled executor " + icommandlistener.getClass().getSimpleName(), new Object[0]); -+ } -+ } ++ org.bukkit.command.CommandSender sender = CommandBlockListenerAbstract.unwrapSender(icommandlistener); + int i = CommandBlockListenerAbstract.executeCommand(commandlistenerwrapper, new ProxiedNativeCommandSender(commandlistenerwrapper, sender, entity.getBukkitEntity()), s); + // CraftBukkit end diff --git a/nms-patches/CustomFunction.patch b/nms-patches/CustomFunction.patch index ea37c18565..a01167d403 100644 --- a/nms-patches/CustomFunction.patch +++ b/nms-patches/CustomFunction.patch @@ -1,11 +1,19 @@ --- a/net/minecraft/server/CustomFunction.java +++ b/net/minecraft/server/CustomFunction.java -@@ -122,7 +122,7 @@ +@@ -122,7 +122,15 @@ } public void a(CustomFunctionData customfunctiondata, ICommandListener icommandlistener, ArrayDeque arraydeque, int i) { - customfunctiondata.a().a(icommandlistener, this.a); -+ CommandBlockListenerAbstract.executeSafely(icommandlistener, ((CustomFunctionData.CustomFunctionListener) icommandlistener).sender, this.a); // CraftBukkit ++ // CraftBukkit start ++ org.bukkit.command.CommandSender sender; ++ if (icommandlistener instanceof CustomFunctionData.CustomFunctionListener) { ++ sender = ((CustomFunctionData.CustomFunctionListener) icommandlistener).sender; ++ } else { ++ sender = CommandBlockListenerAbstract.unwrapSender(icommandlistener); ++ } ++ CommandBlockListenerAbstract.executeSafely(icommandlistener, sender, this.a); ++ // CraftBukkit end } public String toString() {