From 70ea1b2e2bdbe535751f614eaa0847c424e6711c Mon Sep 17 00:00:00 2001 From: Jake Potrebic <15055071+Machine-Maker@users.noreply.github.com> Date: Sun, 11 Jul 2021 11:06:49 -0700 Subject: [PATCH] Fix command signs (#6139) --- build-data/mappings-patch.tiny | 4 + ...Add-PlayerSignCommandPreprocessEvent.patch | 46 ++++++ ...from-signs-not-firing-command-events.patch | 141 ++++++++++++++++++ ...ommand-click-events-through-normal-c.patch | 40 ----- 4 files changed, 191 insertions(+), 40 deletions(-) create mode 100644 patches/api/0323-Add-PlayerSignCommandPreprocessEvent.patch create mode 100644 patches/server/0723-Fix-commands-from-signs-not-firing-command-events.patch delete mode 100644 patches/server/0723-Route-sign-run_command-click-events-through-normal-c.patch diff --git a/build-data/mappings-patch.tiny b/build-data/mappings-patch.tiny index 3b678102f5..ee3c486f14 100644 --- a/build-data/mappings-patch.tiny +++ b/build-data/mappings-patch.tiny @@ -51,3 +51,7 @@ c net/minecraft/world/level/block/entity/IHopper net/minecraft/world/level/block # Teleport method in ServerGamePacketListenerImpl c net/minecraft/server/network/PlayerConnection net/minecraft/server/network/ServerGamePacketListenerImpl m (DDDFFLorg/bukkit/event/player/PlayerTeleportEvent$TeleportCause;)V a teleport + +# Commands performCommand adds a stripSlash boolean +c net/minecraft/commands/CommandDispatcher net/minecraft/commands/Commands + m (Lnet/minecraft/commands/CommandListenerWrapper;Ljava/lang/String;Ljava/lang/String;Z)I a performCommand diff --git a/patches/api/0323-Add-PlayerSignCommandPreprocessEvent.patch b/patches/api/0323-Add-PlayerSignCommandPreprocessEvent.patch new file mode 100644 index 0000000000..fd095653a3 --- /dev/null +++ b/patches/api/0323-Add-PlayerSignCommandPreprocessEvent.patch @@ -0,0 +1,46 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +Date: Fri, 9 Jul 2021 17:44:33 -0700 +Subject: [PATCH] Add PlayerSignCommandPreprocessEvent + + +diff --git a/src/main/java/io/papermc/paper/event/player/PlayerSignCommandPreprocessEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerSignCommandPreprocessEvent.java +new file mode 100644 +index 0000000000000000000000000000000000000000..a51a2288bf812e7d8845a6ec70274d625ff793b6 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/event/player/PlayerSignCommandPreprocessEvent.java +@@ -0,0 +1,34 @@ ++package io.papermc.paper.event.player; ++ ++import org.bukkit.block.Sign; ++import org.bukkit.entity.Player; ++import org.bukkit.event.player.PlayerCommandPreprocessEvent; ++import org.jetbrains.annotations.NotNull; ++ ++import java.util.Set; ++ ++/** ++ * Called when a {@link Player} clicks a sign that causes a command to run. ++ *

++ * This command is run with elevated permissions which allows players to access commands on signs they wouldn't ++ * normally be able to run. ++ */ ++public class PlayerSignCommandPreprocessEvent extends PlayerCommandPreprocessEvent { ++ ++ private final Sign sign; ++ ++ public PlayerSignCommandPreprocessEvent(@NotNull Player player, @NotNull String message, @NotNull Set recipients, @NotNull Sign sign) { ++ super(player, message, recipients); ++ this.sign = sign; ++ } ++ ++ /** ++ * Gets the sign that the command originated from. ++ * ++ * @return the sign ++ */ ++ @NotNull ++ public Sign getSign() { ++ return sign; ++ } ++} diff --git a/patches/server/0723-Fix-commands-from-signs-not-firing-command-events.patch b/patches/server/0723-Fix-commands-from-signs-not-firing-command-events.patch new file mode 100644 index 0000000000..4f424c7936 --- /dev/null +++ b/patches/server/0723-Fix-commands-from-signs-not-firing-command-events.patch @@ -0,0 +1,141 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +Date: Fri, 9 Jul 2021 13:50:48 -0700 +Subject: [PATCH] Fix commands from signs not firing command events + +This patch changes sign command logic so that `run_command` click events: + - are logged to the console + - fire PlayerCommandPreprocessEvent + - work with double-slash commands like `//wand` + - sends failure messages to the player who clicked the sign + +diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java +index 26e18a08a7f0bd704ff3055ce3a7814191450c85..33d7a5da8764c5f6b8e911faecb6b4282443a60b 100644 +--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java ++++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java +@@ -820,5 +820,10 @@ public class PaperWorldConfig { + private void fixInvulnerableEndCrystalExploit() { + fixInvulnerableEndCrystalExploit = getBoolean("unsupported-settings.fix-invulnerable-end-crystal-exploit", fixInvulnerableEndCrystalExploit); + } ++ ++ public boolean showSignClickCommandFailureMessagesToPlayer = false; ++ private void showSignClickCommandFailureMessagesToPlayer() { ++ showSignClickCommandFailureMessagesToPlayer = getBoolean("show-sign-click-command-failure-msgs-to-player", showSignClickCommandFailureMessagesToPlayer); ++ } + } + +diff --git a/src/main/java/io/papermc/paper/commands/DelegatingCommandSource.java b/src/main/java/io/papermc/paper/commands/DelegatingCommandSource.java +new file mode 100644 +index 0000000000000000000000000000000000000000..1f6747d7a4c33f0ee7b0dc2120081bb87a855d35 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/commands/DelegatingCommandSource.java +@@ -0,0 +1,42 @@ ++package io.papermc.paper.commands; ++ ++import net.minecraft.commands.CommandSource; ++import net.minecraft.commands.CommandSourceStack; ++import net.minecraft.network.chat.Component; ++import org.bukkit.command.CommandSender; ++ ++import java.util.UUID; ++ ++public class DelegatingCommandSource implements CommandSource { ++ ++ private final CommandSource delegate; ++ ++ public DelegatingCommandSource(CommandSource delegate) { ++ this.delegate = delegate; ++ } ++ ++ @Override ++ public void sendMessage(Component message, UUID sender) { ++ delegate.sendMessage(message, sender); ++ } ++ ++ @Override ++ public boolean acceptsSuccess() { ++ return delegate.acceptsSuccess(); ++ } ++ ++ @Override ++ public boolean acceptsFailure() { ++ return delegate.acceptsFailure(); ++ } ++ ++ @Override ++ public boolean shouldInformAdmins() { ++ return delegate.shouldInformAdmins(); ++ } ++ ++ @Override ++ public CommandSender getBukkitSender(CommandSourceStack wrapper) { ++ return delegate.getBukkitSender(wrapper); ++ } ++} +diff --git a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java +index 9b5d11ece006d7aa893360a84ba652c666517ac1..344d3a8c1162f1a4ab5fc2b7676680ddace46649 100644 +--- a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java ++++ b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java +@@ -41,6 +41,7 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C + private boolean renderMessagedFiltered; + private DyeColor color; + private boolean hasGlowingText; ++ private static final org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager.getLogger(); + + public SignBlockEntity(BlockPos pos, BlockState state) { + super(BlockEntityType.SIGN, pos, state); +@@ -227,7 +228,17 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C + ClickEvent chatclickable = chatmodifier.getClickEvent(); + + if (chatclickable != null && chatclickable.getAction() == ClickEvent.Action.RUN_COMMAND) { +- player.getServer().getCommands().performCommand(this.createCommandSourceStack(player), chatclickable.getValue()); ++ // Paper start ++ String command = chatclickable.getValue().startsWith("/") ? chatclickable.getValue() : "/" + chatclickable.getValue(); ++ if (org.spigotmc.SpigotConfig.logCommands) { ++ LOGGER.info("{} issued server command: {}", player.getScoreboardName(), command); ++ } ++ io.papermc.paper.event.player.PlayerSignCommandPreprocessEvent event = new io.papermc.paper.event.player.PlayerSignCommandPreprocessEvent(player.getBukkitEntity(), command, new org.bukkit.craftbukkit.util.LazyPlayerSet(player.getServer()), (org.bukkit.block.Sign) net.minecraft.server.MCUtil.toBukkitBlock(this.level, this.worldPosition).getState()); ++ if (!event.callEvent()) { ++ return false; ++ } ++ player.getServer().getCommands().performCommand(this.createCommandSourceStack(((org.bukkit.craftbukkit.entity.CraftPlayer) event.getPlayer()).getHandle()), event.getMessage()); ++ // Paper end + } + } + +@@ -263,8 +274,21 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C + String s = player == null ? "Sign" : player.getName().getString(); + Object object = player == null ? new TextComponent("Sign") : player.getDisplayName(); + ++ // Paper start - send messages back to the player ++ CommandSource commandSource = this.level.paperConfig.showSignClickCommandFailureMessagesToPlayer ? new io.papermc.paper.commands.DelegatingCommandSource(this) { ++ @Override ++ public void sendMessage(net.minecraft.network.chat.Component message, java.util.UUID sender) { ++ player.sendMessage(message, sender); ++ } ++ ++ @Override ++ public boolean acceptsFailure() { ++ return true; ++ } ++ } : this; ++ // Paper end + // CraftBukkit - this +- return new CommandSourceStack(this, Vec3.atCenterOf((Vec3i) this.worldPosition), Vec2.ZERO, (ServerLevel) this.level, 2, s, (Component) object, this.level.getServer(), player); ++ return new CommandSourceStack(commandSource, Vec3.atCenterOf((Vec3i) this.worldPosition), Vec2.ZERO, (ServerLevel) this.level, 2, s, (Component) object, this.level.getServer(), player); // Paper + } + + public DyeColor getColor() { +diff --git a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java +index 0bba36d18d56a4dc2d6c6fb7969e5e6f0e1da404..a246a0bd9e57fb0703ba756e8aa1109f1674c0e8 100644 +--- a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java ++++ b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java +@@ -50,7 +50,7 @@ public class BukkitCommandWrapper implements com.mojang.brigadier.Command context) throws CommandSyntaxException { +- return this.server.dispatchCommand(context.getSource().getBukkitSender(), context.getInput()) ? 1 : 0; ++ return this.server.dispatchCommand(context.getSource().getBukkitSender(), context.getRange().get(context.getInput())) ? 1 : 0; // Paper - actually use the StringRange from context + } + + @Override diff --git a/patches/server/0723-Route-sign-run_command-click-events-through-normal-c.patch b/patches/server/0723-Route-sign-run_command-click-events-through-normal-c.patch deleted file mode 100644 index 3e0e493ef9..0000000000 --- a/patches/server/0723-Route-sign-run_command-click-events-through-normal-c.patch +++ /dev/null @@ -1,40 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: mdcfe <1917406+mdcfe@users.noreply.github.com> -Date: Wed, 7 Jul 2021 12:29:48 +0100 -Subject: [PATCH] Route sign run_command click events through normal chat logic - -This patch changes sign command logic so that `run_command` click events are routed through the standard chat/command -logic used for inbound chat messages. - -This fixes numerous issues related to sign click commands: - - Signs with a `run_command` value of "/" would fail and show the "Unknown command" warning. This - prevents usage of commands like `//wand` from WorldEdit in sign click events entirely and requires users to drop - the leading slash from other plugins' commands. This patch now executes the plugin commands as would be expected, - adding a leading slash if necessary. - - Signs with a `run_command` value that doesn't match an existing command could fail silently. This patch causes - these to *always* show "Unknown command" instead. - - Plugins listening to `PlayerCommandPreprocessEvent` would not be able to intercept any command executions from - sign click events. This patch allows plugins to intercept player commands when fired by a click event, in the same - manner as commands executed by the player typing or clicking on a chat message. - - Commands executed from signs would not be logged to the console. This patch fixes this. - -This patch also prepends a leading slash if the `run_command` value lacks one, which matches vanilla behaviour (old -code would strip this slash away) while also ensuring `PlayerCommandPreprocessEvent#getMessage` remains consistent -with other command executions from chat (which always include the leading slash). - -diff --git a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java -index 9b5d11ece006d7aa893360a84ba652c666517ac1..171fea79d3a7c282e473b0fbb46254e3cd9c7aa9 100644 ---- a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java -+++ b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java -@@ -227,7 +227,10 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C - ClickEvent chatclickable = chatmodifier.getClickEvent(); - - if (chatclickable != null && chatclickable.getAction() == ClickEvent.Action.RUN_COMMAND) { -- player.getServer().getCommands().performCommand(this.createCommandSourceStack(player), chatclickable.getValue()); -+ // Paper start - route through standard chat/command logic -+ final String command = chatclickable.getValue().startsWith("/") ? chatclickable.getValue() : "/" + chatclickable.getValue(); -+ player.connection.chat(command, false); -+ // Paper end - } - } -