blob: 6ea5e6470251328f60e7ed653f22d973c6296acd (
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
|
package dev.plutorocks.mixin;
import dev.plutorocks.PlutoBridge;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ChatScreen.class)
public abstract class ChatScreenMixin {
@Inject(method = "sendMessage", at = @At("HEAD"), cancellable = true)
private void bridge$interceptBridgeMessages(String chatText, boolean addToHistory, CallbackInfo ci) {
if (chatText == null) {
return;
}
String trimmed = chatText.trim();
if (trimmed.isEmpty()) {
return;
}
if (trimmed.startsWith("/")) {
return;
}
boolean startsWithBang = trimmed.startsWith("!");
boolean bridgeDefault = PlutoBridge.bridgeChatDefault;
boolean wantsBridge;
if (!bridgeDefault) {
if (!startsWithBang) {
return;
}
wantsBridge = true;
} else {
wantsBridge = !startsWithBang;
}
MinecraftClient client = MinecraftClient.getInstance();
if (client == null || client.player == null) {
return;
}
if (PlutoBridge.BRIDGE_CLIENT == null || !PlutoBridge.BRIDGE_CLIENT.isConnected()) {
if (wantsBridge) {
if (client.inGameHud != null) {
MutableText prefix = Text.literal("[Bridge] ").formatted(Formatting.AQUA);
MutableText body = Text.literal("Not connected. Use /bridge connect first.")
.formatted(Formatting.GRAY);
client.inGameHud.getChatHud().addMessage(prefix.append(body));
}
ci.cancel();
}
return;
}
// at this point we are connected.
if (bridgeDefault && startsWithBang && !wantsBridge) {
// bridge mode ON and user prefixed with "!" => send to normal chat.
String normalMessage = trimmed.substring(1);
if (!normalMessage.isEmpty()) {
client.player.networkHandler.sendChatMessage(normalMessage);
}
} else {
// send to the bridge.
String bridgeMessage;
if (!bridgeDefault) {
// default mode: strip the leading "!" for bridge messages.
bridgeMessage = trimmed.substring(1);
} else {
// bridge mode: already without prefix for bridge messages.
bridgeMessage = trimmed;
}
// enforce max message length client-side to match server.
int len = bridgeMessage.codePointCount(0, bridgeMessage.length());
if (len > PlutoBridge.MAX_MESSAGE_LEN) {
if (client.inGameHud != null) {
MutableText prefix = Text.literal("[Bridge] ").formatted(Formatting.AQUA);
MutableText body = Text.literal(
"Message too long (max " + PlutoBridge.MAX_MESSAGE_LEN + " characters)."
).formatted(Formatting.GRAY);
client.inGameHud.getChatHud().addMessage(prefix.append(body));
}
ci.cancel();
return;
}
if (!bridgeMessage.isEmpty()) {
PlutoBridge.BRIDGE_CLIENT.sendChatMessage(bridgeMessage);
}
}
// prevent this message from going to normal MC chat
ci.cancel();
}
}
|