aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0681-Fix-commands-from-signs-not-firing-command-events.patch
blob: 386073e2a7756ecd494cc2bd5a66d82a98d0f3f2 (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
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/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 54a2f43e568b0104571abcb5d84aac35ddde3cd7..ee5479b0b168f230ec4f29db56cf481706bbfeb6 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -858,4 +858,9 @@ 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 615c4f9d9841f7ddc3e5c854e90f41c3905c2e8f..6371176fba41218a209ea59b4cafe5b2d4a685fd 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
@@ -40,6 +40,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);
@@ -224,7 +225,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
             }
         }
 
@@ -260,8 +271,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(Component message, UUID sender) {
+                player.sendMessage(message, sender);
+            }
+
+            @Override
+            public boolean acceptsFailure() {
+                return true;
+            }
+        } : this;
+        // Paper end
         // CraftBukkit - this
-        return new CommandSourceStack(this, Vec3.atCenterOf(this.worldPosition), Vec2.ZERO, (ServerLevel) this.level, 2, s, (Component) object, this.level.getServer(), player);
+        return new CommandSourceStack(commandSource, Vec3.atCenterOf(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<Comman
 
     @Override
     public int run(CommandContext<CommandSourceStack> 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