Skip to content

Commit 59701fb

Browse files
Fixed mixin. Fixed chat parse issues.
1 parent 18abc18 commit 59701fb

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ mixin {
133133

134134
tasks.named<Jar>("jar") {
135135
manifest.attributes(
136-
mapOf("MixinConfigs" to "${mod_id}.refmap.json")
136+
mapOf("MixinConfigs" to "${mod_id}.mixins.json")
137137
)
138138
}
139139

@@ -153,6 +153,7 @@ dependencies {
153153

154154
// mixin
155155
annotationProcessor("org.spongepowered:mixin:0.8.5:processor")
156+
//jarJar(implementation("io.github.llamalad7:mixinextras-neoforge:0.5.0")!!)
156157

157158
// kff
158159
implementation("thedarkcolour:kotlinforforge:4.10.0")
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,68 @@
11
package NoMathExpectation.chatExchange.neoForged.mixin;
22

33
import NoMathExpectation.chatExchange.neoForged.*;
4+
import net.minecraft.network.chat.Component;
45
import net.minecraft.network.protocol.game.ServerGamePacketListener;
56
import net.minecraft.network.protocol.game.ServerboundChatPacket;
67
import net.minecraft.server.level.ServerPlayer;
78
import net.minecraft.server.network.ServerGamePacketListenerImpl;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
811
import org.spongepowered.asm.mixin.Mixin;
912
import org.spongepowered.asm.mixin.Shadow;
13+
import org.spongepowered.asm.mixin.Unique;
1014
import org.spongepowered.asm.mixin.injection.At;
11-
import org.spongepowered.asm.mixin.injection.ModifyVariable;
15+
import org.spongepowered.asm.mixin.injection.Inject;
16+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1217

1318
@Mixin(ServerGamePacketListenerImpl.class)
1419
public abstract class ServerGamePacketListenerImplMixin implements ServerGamePacketListener {
1520
@Shadow
1621
public ServerPlayer player;
1722

18-
@ModifyVariable(
23+
@Unique
24+
private static final Logger chatExchange$LOGGER = LoggerFactory.getLogger(ChatExchange.ID);
25+
26+
@Inject(
1927
method = "handleChat(Lnet/minecraft/network/protocol/game/ServerboundChatPacket;)V",
2028
at = @At("HEAD"),
21-
ordinal = 0,
22-
argsOnly = true
29+
cancellable = true
2330
)
24-
private ServerboundChatPacket onHandleChat(ServerboundChatPacket packet) {
31+
private void onHandleChat(ServerboundChatPacket packet, CallbackInfo ci) {
2532
if (!ChatExchangeConfig.INSTANCE.getMixinMode().get()) {
26-
return packet;
33+
return;
2734
}
2835

2936
var data = ChatExchangeDataKt.getChatExchangeData(player.server);
3037
var string = packet.message();
3138
if ((!ChatExchangeConfig.INSTANCE.getChat().get() || data.isIgnoredPlayer(player.getUUID())) && !ChatExchangeConfigKt.startsWithBroadcastPrefix(string)) {
32-
return packet;
39+
return;
3340
}
3441

42+
ci.cancel();
43+
3544
var newString = ChatExchangeConfigKt.removeBroadcastPrefix(string);
45+
var playerName = ExchangeServer.Companion.componentToString(player.getName());
3646
ExchangeServer.Companion.sendEvent(
3747
new MessageEvent(
38-
ExchangeServer.Companion.componentToString(player.getName()),
48+
playerName,
3949
newString
4050
)
4151
);
4252

43-
if (!ChatExchangeConfig.INSTANCE.getChat().get() || data.isIgnoredPlayer(player.getUUID())) {
44-
newString = ChatExchangeConfig.INSTANCE.getBroadcastPrefix().get() + newString;
53+
Component message;
54+
try {
55+
var formattedPrefix = ChatExchangeConfig.INSTANCE.getCommandBroadcastFormat().get().formatted(playerName);
56+
message = NeoForgeEventsKt.parseJsonToComponent(formattedPrefix);
57+
} catch (Exception e) {
58+
chatExchange$LOGGER.error(
59+
"Unable to resolve component from command broadcast format. Using default.",
60+
e
61+
);
62+
message = Component.literal("<%s> ".formatted(playerName));
4563
}
46-
return new ServerboundChatPacket(newString, packet.timeStamp(), packet.salt(), packet.signature(), packet.lastSeenMessages());
64+
message = message.copy().append(newString);
65+
66+
player.server.getPlayerList().broadcastSystemMessage(message, false);
4767
}
4868
}

src/main/kotlin/NoMathExpectation/chatExchange/neoForged/ChatExchangeConfig.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,26 @@ object ChatExchangeConfig {
8383
{ true }
8484
)
8585
val broadcastPrefix: ForgeConfigSpec.ConfigValue<String> =
86-
builder.comment("The prefix to prepend when displaying manually broadcast chat message.")
86+
builder.comment("The prefix to prepend when player message is broadcast through player chat.")
8787
.translation("modid.config.broadcastPrefix")
8888
.define("broadcastPrefix", "")
8989
val commandBroadcastFormat: ForgeConfigSpec.ConfigValue<String> = builder.comment(
90-
"The message format when player broadcast message using the command.",
90+
"The message prefix format when player message is broadcast through system chat.",
9191
"Will not prepend broadcast prefix."
9292
)
9393
.translation("modid.config.commandBroadcastFormat")
94-
.define("commandBroadcastFormat", "\"<%s> %s\"") {
95-
(it as? String)?.parseJsonToComponent() != null
94+
.define("commandBroadcastFormat", "\"<%s> \"") {
95+
runCatching {
96+
(it as? String)?.parseJsonToComponent()
97+
}.getOrNull() != null
9698
}
9799
val receiveMessageFormat: ForgeConfigSpec.ConfigValue<String> =
98100
builder.comment("The message format when receiving message from outside.")
99101
.translation("modid.config.receiveMessageFormat")
100-
.define("receiveMessageFormat", "\"<%s> %s\"") {
101-
(it as? String)?.parseJsonToComponent() != null
102+
.define("receiveMessageFormat", "\"<%s> \"") {
103+
runCatching {
104+
(it as? String)?.parseJsonToComponent()
105+
}.getOrNull() != null
102106
}
103107

104108
val spec: ForgeConfigSpec = builder.build()

src/main/kotlin/NoMathExpectation/chatExchange/neoForged/ExchangeServer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ class ExchangeServer(
103103
val formatted = kotlin.runCatching {
104104
ChatExchangeConfig.receiveMessageFormat
105105
.get()
106-
.format(event.from, event.content)
106+
.format(event.from)
107107
.parseJsonToComponent()
108+
.copy()
109+
.append(event.content)
108110
}.getOrElse {
109111
logger.warn("Failed to format message from receive message format. Using default.", it)
110112
Component.literal("<${event.from}> ${event.content}")

src/main/kotlin/NoMathExpectation/chatExchange/neoForged/NeoForgeEvents.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package NoMathExpectation.chatExchange.neoForged
22

33
import com.mojang.brigadier.arguments.BoolArgumentType
44
import com.mojang.brigadier.arguments.StringArgumentType
5-
import net.minecraft.ChatFormatting
65
import net.minecraft.commands.Commands
76
import net.minecraft.network.chat.Component
87
import net.minecraft.network.chat.contents.LiteralContents
@@ -159,13 +158,12 @@ object NeoForgeEvents {
159158
val name = ExchangeServer.componentToString(context.source.displayName)
160159

161160
val component = kotlin.runCatching {
162-
val formatted = ChatExchangeConfig.commandBroadcastFormat
161+
ChatExchangeConfig.commandBroadcastFormat
163162
.get()
164-
.format(
165-
name,
166-
message,
167-
)
168-
formatted.parseJsonToComponent()
163+
.format(name)
164+
.parseJsonToComponent()
165+
.copy()
166+
.append(message)
169167
}.getOrElse {
170168
logger.error(
171169
"Unable to resolve component from command broadcast format. Using default.",
@@ -179,9 +177,7 @@ object NeoForgeEvents {
179177
ExchangeServer.sendEvent(
180178
MessageEvent(name, message)
181179
)
182-
context.source.server.playerList.players.forEach {
183-
it.sendSystemMessage(component)
184-
}
180+
context.source.server.playerList.broadcastSystemMessage(component, false)
185181

186182
1
187183
}
@@ -254,5 +250,5 @@ object NeoForgeEvents {
254250

255251
fun String.parseJsonToComponent(): Component {
256252
return Component.Serializer.fromJson(this)
257-
?: Component.literal("[error]").withStyle(ChatFormatting.RED)
253+
?: error("Unable to parse json to component: $this")
258254
}

0 commit comments

Comments
 (0)