Skip to content

Commit 9940bca

Browse files
authored
API for creating command sender which forwards feedback (#7432)
1 parent f23c039 commit 9940bca

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
3+
Date: Tue, 1 Feb 2022 15:51:44 -0700
4+
Subject: [PATCH] API for creating command sender which forwards feedback
5+
6+
7+
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
8+
index 53c4e5ca208ee17c7c244e416c537c7b63edf194..3a8326735b62521f8fb95c51a0909d8b7bac83d1 100644
9+
--- a/src/main/java/org/bukkit/Bukkit.java
10+
+++ b/src/main/java/org/bukkit/Bukkit.java
11+
@@ -1297,6 +1297,20 @@ public final class Bukkit {
12+
return server.getConsoleSender();
13+
}
14+
15+
+ // Paper start
16+
+ /**
17+
+ * Creates a special {@link CommandSender} which redirects command feedback (in the form of chat messages) to the
18+
+ * specified listener. The returned sender will have the same effective permissions as {@link #getConsoleSender()}.
19+
+ *
20+
+ * @param feedback feedback listener
21+
+ * @return a command sender
22+
+ */
23+
+ @NotNull
24+
+ public static CommandSender createCommandSender(final @NotNull java.util.function.Consumer<? super net.kyori.adventure.text.Component> feedback) {
25+
+ return server.createCommandSender(feedback);
26+
+ }
27+
+ // Paper end
28+
+
29+
/**
30+
* Gets the folder that contains all of the various {@link World}s.
31+
*
32+
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
33+
index e48af3822e9f118399c3a1c9358c56efae12e0da..73446141e8c50b71a17ff6f0c528a62d5c75751b 100644
34+
--- a/src/main/java/org/bukkit/Server.java
35+
+++ b/src/main/java/org/bukkit/Server.java
36+
@@ -1078,6 +1078,18 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
37+
@NotNull
38+
public ConsoleCommandSender getConsoleSender();
39+
40+
+ // Paper start
41+
+ /**
42+
+ * Creates a special {@link CommandSender} which redirects command feedback (in the form of chat messages) to the
43+
+ * specified listener. The returned sender will have the same effective permissions as {@link #getConsoleSender()}.
44+
+ *
45+
+ * @param feedback feedback listener
46+
+ * @return a command sender
47+
+ */
48+
+ @NotNull
49+
+ public CommandSender createCommandSender(final @NotNull java.util.function.Consumer<? super net.kyori.adventure.text.Component> feedback);
50+
+ // Paper end
51+
+
52+
/**
53+
* Gets the folder that contains all of the various {@link World}s.
54+
*
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
3+
Date: Tue, 1 Feb 2022 15:51:55 -0700
4+
Subject: [PATCH] API for creating command sender which forwards feedback
5+
6+
7+
diff --git a/src/main/java/io/papermc/paper/commands/FeedbackForwardingSender.java b/src/main/java/io/papermc/paper/commands/FeedbackForwardingSender.java
8+
new file mode 100644
9+
index 0000000000000000000000000000000000000000..f7c86155ce0cfd9b4bf8a2b79d77a656d795c95f
10+
--- /dev/null
11+
+++ b/src/main/java/io/papermc/paper/commands/FeedbackForwardingSender.java
12+
@@ -0,0 +1,112 @@
13+
+package io.papermc.paper.commands;
14+
+
15+
+import io.papermc.paper.adventure.PaperAdventure;
16+
+import io.papermc.paper.text.PaperComponents;
17+
+import java.util.UUID;
18+
+import java.util.function.Consumer;
19+
+import net.kyori.adventure.audience.MessageType;
20+
+import net.kyori.adventure.identity.Identity;
21+
+import net.kyori.adventure.text.Component;
22+
+import net.minecraft.commands.CommandSource;
23+
+import net.minecraft.commands.CommandSourceStack;
24+
+import net.minecraft.network.chat.TextComponent;
25+
+import net.minecraft.server.level.ServerLevel;
26+
+import net.minecraft.world.phys.Vec2;
27+
+import net.minecraft.world.phys.Vec3;
28+
+import org.bukkit.command.CommandSender;
29+
+import org.bukkit.craftbukkit.CraftServer;
30+
+import org.bukkit.craftbukkit.command.ServerCommandSender;
31+
+import org.checkerframework.checker.nullness.qual.NonNull;
32+
+import org.checkerframework.checker.nullness.qual.Nullable;
33+
+import org.checkerframework.framework.qual.DefaultQualifier;
34+
+
35+
+@DefaultQualifier(NonNull.class)
36+
+public final class FeedbackForwardingSender extends ServerCommandSender {
37+
+ private final Consumer<? super Component> feedback;
38+
+ private final CraftServer server;
39+
+
40+
+ public FeedbackForwardingSender(final Consumer<? super Component> feedback, final CraftServer server) {
41+
+ super(((ServerCommandSender) server.getConsoleSender()).perm);
42+
+ this.server = server;
43+
+ this.feedback = feedback;
44+
+ }
45+
+
46+
+ @Override
47+
+ public void sendMessage(final String message) {
48+
+ this.sendMessage(PaperComponents.legacySectionSerializer().deserialize(message));
49+
+ }
50+
+
51+
+ @Override
52+
+ public void sendMessage(final String... messages) {
53+
+ for (final String message : messages) {
54+
+ this.sendMessage(message);
55+
+ }
56+
+ }
57+
+
58+
+ @Override
59+
+ public void sendMessage(final Identity identity, final Component message, final MessageType type) {
60+
+ this.feedback.accept(message);
61+
+ }
62+
+
63+
+ @Override
64+
+ public String getName() {
65+
+ return "FeedbackForwardingSender";
66+
+ }
67+
+
68+
+ @Override
69+
+ public Component name() {
70+
+ return Component.text(this.getName());
71+
+ }
72+
+
73+
+ @Override
74+
+ public boolean isOp() {
75+
+ return true;
76+
+ }
77+
+
78+
+ @Override
79+
+ public void setOp(final boolean value) {
80+
+ throw new UnsupportedOperationException("Cannot change operator status of " + this.getClass().getName());
81+
+ }
82+
+
83+
+ public CommandSourceStack asVanilla() {
84+
+ final @Nullable ServerLevel overworld = this.server.getServer().overworld();
85+
+ return new CommandSourceStack(
86+
+ new Source(this),
87+
+ overworld == null ? Vec3.ZERO : Vec3.atLowerCornerOf(overworld.getSharedSpawnPos()),
88+
+ Vec2.ZERO,
89+
+ overworld,
90+
+ 4,
91+
+ this.getName(),
92+
+ new TextComponent(this.getName()),
93+
+ this.server.getServer(),
94+
+ null
95+
+ );
96+
+ }
97+
+
98+
+ private record Source(FeedbackForwardingSender sender) implements CommandSource {
99+
+ @Override
100+
+ public void sendMessage(final net.minecraft.network.chat.Component message, final UUID sender) {
101+
+ this.sender.sendMessage(Identity.identity(sender), PaperAdventure.asAdventure(message));
102+
+ }
103+
+
104+
+ @Override
105+
+ public boolean acceptsSuccess() {
106+
+ return true;
107+
+ }
108+
+
109+
+ @Override
110+
+ public boolean acceptsFailure() {
111+
+ return true;
112+
+ }
113+
+
114+
+ @Override
115+
+ public boolean shouldInformAdmins() {
116+
+ return false;
117+
+ }
118+
+
119+
+ @Override
120+
+ public CommandSender getBukkitSender(final CommandSourceStack stack) {
121+
+ return this.sender;
122+
+ }
123+
+ }
124+
+}
125+
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
126+
index 54e8f0f367645f3aa8af5b1cb69c39c0cec9381f..1a1f5c8f9a049d65043f12374fe694068f7d08cf 100644
127+
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
128+
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
129+
@@ -1988,6 +1988,13 @@ public final class CraftServer implements Server {
130+
return console.console;
131+
}
132+
133+
+ // Paper start
134+
+ @Override
135+
+ public CommandSender createCommandSender(final java.util.function.Consumer<? super net.kyori.adventure.text.Component> feedback) {
136+
+ return new io.papermc.paper.commands.FeedbackForwardingSender(feedback, this);
137+
+ }
138+
+ // Paper end
139+
+
140+
public EntityMetadataStore getEntityMetadata() {
141+
return this.entityMetadata;
142+
}
143+
diff --git a/src/main/java/org/bukkit/craftbukkit/command/ServerCommandSender.java b/src/main/java/org/bukkit/craftbukkit/command/ServerCommandSender.java
144+
index eaff8df6c8c12c64e005a68a02e2e35ed88f757c..1c638a4b1f2c841928d8b2a7ae43e4ebb1f7eac7 100644
145+
--- a/src/main/java/org/bukkit/craftbukkit/command/ServerCommandSender.java
146+
+++ b/src/main/java/org/bukkit/craftbukkit/command/ServerCommandSender.java
147+
@@ -13,9 +13,15 @@ import org.bukkit.plugin.Plugin;
148+
149+
public abstract class ServerCommandSender implements CommandSender {
150+
private static PermissibleBase blockPermInst;
151+
- private final PermissibleBase perm;
152+
+ public final PermissibleBase perm; // Paper
153+
private net.kyori.adventure.pointer.Pointers adventure$pointers; // Paper - implement pointers
154+
155+
+ // Paper start
156+
+ public ServerCommandSender(final PermissibleBase permissibleBase) {
157+
+ this.perm = permissibleBase;
158+
+ }
159+
+ // Paper end
160+
+
161+
public ServerCommandSender() {
162+
if (this instanceof CraftBlockCommandSender) {
163+
if (ServerCommandSender.blockPermInst == null) {
164+
diff --git a/src/main/java/org/bukkit/craftbukkit/command/VanillaCommandWrapper.java b/src/main/java/org/bukkit/craftbukkit/command/VanillaCommandWrapper.java
165+
index 4aa1dc543950b5de64345b3403a6d0bc41c521df..4525fb3bc9b137bce3b59310a8aecca96d6ad5ba 100644
166+
--- a/src/main/java/org/bukkit/craftbukkit/command/VanillaCommandWrapper.java
167+
+++ b/src/main/java/org/bukkit/craftbukkit/command/VanillaCommandWrapper.java
168+
@@ -84,6 +84,11 @@ public final class VanillaCommandWrapper extends BukkitCommand {
169+
if (sender instanceof ProxiedCommandSender) {
170+
return ((ProxiedNativeCommandSender) sender).getHandle();
171+
}
172+
+ // Paper start
173+
+ if (sender instanceof io.papermc.paper.commands.FeedbackForwardingSender feedback) {
174+
+ return feedback.asVanilla();
175+
+ }
176+
+ // Paper end
177+
178+
throw new IllegalArgumentException("Cannot make " + sender + " a vanilla command listener");
179+
}

0 commit comments

Comments
 (0)