2021-07-11 20:06:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
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/io/papermc/paper/commands/DelegatingCommandSource.java b/src/main/java/io/papermc/paper/commands/DelegatingCommandSource.java
new file mode 100644
2022-06-08 14:19:54 +02:00
index 0000000000000000000000000000000000000000..01a2bc1feec808790bb93618ce46adb9bea5a9c8
2021-07-11 20:06:49 +02:00
--- /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
2022-06-08 14:19:54 +02:00
+ public void sendSystemMessage(Component message) {
+ delegate.sendSystemMessage(message);
2021-07-11 20:06:49 +02:00
+ }
+
+ @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
2024-01-19 13:22:30 +01:00
index 1a54db9f8b2fb3071e79ec26c49242b528b8cd9e..393e6e5fb2c901cdf369cfe5bcde454dee2b1b0d 100644
2021-07-11 20:06:49 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java
2023-12-06 04:57:46 +01:00
@@ -273,7 +273,17 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C
2022-06-14 10:51:53 +02:00
ClickEvent chatclickable = chatmodifier.getClickEvent();
2021-07-11 20:06:49 +02:00
if (chatclickable != null && chatclickable.getAction() == ClickEvent.Action.RUN_COMMAND) {
2023-06-08 04:04:01 +02:00
- player.getServer().getCommands().performPrefixedCommand(this.createCommandSourceStack(player, world, pos), chatclickable.getValue());
2024-01-19 13:22:30 +01:00
+ // Paper start - Fix commands from signs not firing command events
2021-07-11 20:06:49 +02:00
+ String command = chatclickable.getValue().startsWith("/") ? chatclickable.getValue() : "/" + chatclickable.getValue();
+ if (org.spigotmc.SpigotConfig.logCommands) {
+ LOGGER.info("{} issued server command: {}", player.getScoreboardName(), command);
+ }
2023-06-09 07:57:24 +02:00
+ io.papermc.paper.event.player.PlayerSignCommandPreprocessEvent event = new io.papermc.paper.event.player.PlayerSignCommandPreprocessEvent((org.bukkit.entity.Player) player.getBukkitEntity(), command, new org.bukkit.craftbukkit.util.LazyPlayerSet(player.getServer()), (org.bukkit.block.Sign) io.papermc.paper.util.MCUtil.toBukkitBlock(this.level, this.worldPosition).getState(), front ? Side.FRONT : Side.BACK);
2021-07-11 20:06:49 +02:00
+ if (!event.callEvent()) {
+ return false;
+ }
2023-06-08 04:04:01 +02:00
+ player.getServer().getCommands().performPrefixedCommand(this.createCommandSourceStack(((org.bukkit.craftbukkit.entity.CraftPlayer) event.getPlayer()).getHandle(), world, pos), event.getMessage());
2024-01-19 13:22:30 +01:00
+ // Paper end - Fix commands from signs not firing command events
2023-06-08 04:04:01 +02:00
flag1 = true;
2021-07-11 20:06:49 +02:00
}
}
2023-12-06 04:57:46 +01:00
@@ -310,8 +320,23 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C
2021-07-11 20:06:49 +02:00
String s = player == null ? "Sign" : player.getName().getString();
2022-06-08 11:31:06 +02:00
Object object = player == null ? Component.literal("Sign") : player.getDisplayName();
2021-07-11 20:06:49 +02:00
2024-01-19 13:22:30 +01:00
+ // Paper start - Fix commands from signs not firing command events
2022-06-09 10:51:45 +02:00
+ CommandSource commandSource = this.level.paperConfig().misc.showSignClickCommandFailureMsgsToPlayer ? new io.papermc.paper.commands.DelegatingCommandSource(this) {
2021-07-11 20:06:49 +02:00
+ @Override
2022-06-08 14:19:54 +02:00
+ public void sendSystemMessage(Component message) {
2023-06-08 04:04:01 +02:00
+ if (player != null) {
+ player.sendSystemMessage(message);
+ }
2021-07-11 20:06:49 +02:00
+ }
+
+ @Override
+ public boolean acceptsFailure() {
+ return true;
+ }
+ } : this;
2024-01-19 13:22:30 +01:00
+ // Paper end - Fix commands from signs not firing command events
2021-07-11 20:06:49 +02:00
// CraftBukkit - this
2023-06-08 04:04:01 +02:00
- return new CommandSourceStack(this, Vec3.atCenterOf(pos), Vec2.ZERO, (ServerLevel) world, 2, s, (Component) object, world.getServer(), player);
2024-01-19 13:22:30 +01:00
+ return new CommandSourceStack(commandSource, Vec3.atCenterOf(pos), Vec2.ZERO, (ServerLevel) world, 2, s, (Component) object, world.getServer(), player); // Paper - Fix commands from signs not firing command events
2021-07-11 20:06:49 +02:00
}
2023-06-08 04:04:01 +02:00
@Override
2021-07-11 20:06:49 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
2024-01-19 13:22:30 +01:00
index d113e54a30db16e2ad955170df6030d15de530d6..21b6f90cf5bd7087d1a0f512289d971f2c3e1afa 100644
2021-07-11 20:06:49 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
+++ b/src/main/java/org/bukkit/craftbukkit/command/BukkitCommandWrapper.java
2022-08-07 01:22:51 +02:00
@@ -61,7 +61,7 @@ public class BukkitCommandWrapper implements com.mojang.brigadier.Command<Comman
2022-07-29 09:11:11 +02:00
CommandSender sender = context.getSource().getBukkitSender();
2021-07-11 20:06:49 +02:00
2022-07-29 09:11:11 +02:00
try {
- return this.server.dispatchCommand(sender, context.getInput()) ? 1 : 0;
2024-01-19 13:22:30 +01:00
+ return this.server.dispatchCommand(sender, context.getRange().get(context.getInput())) ? 1 : 0; // Paper - Fix commands from signs not firing command events; actually use the StringRange from context
2022-07-29 09:11:11 +02:00
} catch (CommandException ex) {
sender.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
this.server.getLogger().log(Level.SEVERE, null, ex);