Skip to content

Commit c4a5746

Browse files
committed
Send messages to Minecraft clients on main thread (#532)
1 parent d49a3dd commit c4a5746

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import me.lucko.spark.common.SparkPlatform;
2424
import me.lucko.spark.common.command.sender.CommandSender;
25+
import me.lucko.spark.common.platform.PlatformInfo;
2526
import net.kyori.adventure.text.Component;
2627
import net.kyori.adventure.text.JoinConfiguration;
2728
import net.kyori.adventure.text.TextComponent;
@@ -67,6 +68,21 @@ public CommandSender.Data senderData() {
6768
return this.senderData;
6869
}
6970

71+
private void sendMessage(CommandSender sender, Component message) {
72+
if (sender == null) {
73+
return;
74+
}
75+
76+
if (this.platform.getPlugin().getPlatformInfo().getType() == PlatformInfo.Type.CLIENT) {
77+
// send message on the client render thread
78+
this.platform.getPlugin().executeSync(() -> {
79+
sender.sendMessage(message);
80+
});
81+
} else {
82+
sender.sendMessage(message);
83+
}
84+
}
85+
7086
public void allSenders(Consumer<? super CommandSender> action) {
7187
if (this.commandPrimaryAlias == null) {
7288
throw new IllegalStateException("Command alias has not been set!");
@@ -85,10 +101,7 @@ public void allSenders(Consumer<? super CommandSender> action) {
85101
}
86102

87103
public void reply(Component message) {
88-
CommandSender sender = this.sender.get();
89-
if (sender != null) {
90-
sender.sendMessage(message);
91-
}
104+
sendMessage(this.sender.get(), message);
92105
}
93106

94107
public void reply(Iterable<Component> message) {
@@ -98,7 +111,7 @@ public void reply(Iterable<Component> message) {
98111

99112
public void broadcast(Component message) {
100113
if (this.platform.shouldBroadcastResponse()) {
101-
allSenders(sender -> sender.sendMessage(message));
114+
allSenders(sender -> sendMessage(sender, message));
102115
} else {
103116
reply(message);
104117
}
@@ -107,7 +120,7 @@ public void broadcast(Component message) {
107120
public void broadcast(Iterable<Component> message) {
108121
if (this.platform.shouldBroadcastResponse()) {
109122
Component joinedMsg = Component.join(JoinConfiguration.separator(Component.newline()), message);
110-
allSenders(sender -> sender.sendMessage(joinedMsg));
123+
allSenders(sender -> sendMessage(sender, joinedMsg));
111124
} else {
112125
reply(message);
113126
}

0 commit comments

Comments
 (0)