Skip to content

Commit 0400e5f

Browse files
authored
Fix #479 (#480)
This bug only happens on 1.21 due to enchantments being made data-driven.
1 parent b255be3 commit 0400e5f

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.aizistral.nochatreports.common.config.NCRConfig;
77
import com.aizistral.nochatreports.common.encryption.Encryptor;
88

9+
import net.minecraft.core.HolderLookup;
910
import net.minecraft.core.RegistryAccess;
1011
import net.minecraft.network.chat.Component;
1112
import net.minecraft.network.chat.ComponentContents;
@@ -15,13 +16,13 @@
1516

1617
public class EncryptionUtil {
1718

18-
public static Optional<Component> tryDecrypt(Component component) {
19+
public static Optional<Component> tryDecrypt(HolderLookup.Provider provider, Component component) {
1920
var optional = NCRConfig.getEncryption().getEncryptor();
2021
if (optional.isEmpty())
2122
return Optional.empty();
2223

2324
Encryptor<?> encryption = optional.get();
24-
Component copy = recreate(component);
25+
Component copy = recreate(provider, component);
2526
ComponentContents contents = copy.getContents();
2627

2728
return Optional.ofNullable(tryDecrypt(copy, encryption) ? copy : null);
@@ -81,8 +82,8 @@ public static Optional<String> tryDecrypt(String message, Encryptor<?> encryptor
8182
}
8283
}
8384

84-
public static Component recreate(Component component) {
85-
return Component.Serializer.fromJson(Component.Serializer.toJson(component, RegistryAccess.EMPTY), RegistryAccess.EMPTY);
85+
public static Component recreate(HolderLookup.Provider provider, Component component) {
86+
return Component.Serializer.fromJson(Component.Serializer.toJson(component, provider), provider);
8687
}
8788

8889
}

src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatComponent.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.aizistral.nochatreports.common.mixins.client;
22

3+
import net.minecraft.client.Minecraft;
4+
import org.spongepowered.asm.mixin.Final;
35
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Shadow;
47
import org.spongepowered.asm.mixin.injection.At;
58
import org.spongepowered.asm.mixin.injection.At.Shift;
69

@@ -22,6 +25,7 @@
2225

2326
@Mixin(ChatComponent.class)
2427
public class MixinChatComponent {
28+
@Shadow @Final private Minecraft minecraft;
2529
private static final GuiMessageTag.Icon ENCRYPTED_ICON = GuiMessageTag.Icon.valueOf("CHAT_NCR_ENCRYPTED");
2630
private boolean lastMessageEncrypted;
2731
private Component lastMessageOriginal;
@@ -44,10 +48,11 @@ private GuiMessage modifyGUIMessage(GuiMessage msg) {
4448
Component.Serializer.toJson(msg.content(), RegistryAccess.EMPTY));
4549
}
4650

47-
var decrypted = EncryptionUtil.tryDecrypt(msg.content());
51+
assert minecraft.level != null;
52+
var decrypted = EncryptionUtil.tryDecrypt(minecraft.level.registryAccess(), msg.content());
4853

4954
decrypted.ifPresentOrElse(component -> {
50-
this.lastMessageOriginal = EncryptionUtil.recreate(msg.content());
55+
this.lastMessageOriginal = EncryptionUtil.recreate(minecraft.level.registryAccess(), msg.content());
5156
this.lastMessageEncrypted = true;
5257
}, () -> this.lastMessageEncrypted = false);
5358

src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Base64;
55
import java.util.UUID;
66

7+
import org.spongepowered.asm.mixin.Final;
78
import org.spongepowered.asm.mixin.Mixin;
89
import org.spongepowered.asm.mixin.Shadow;
910
import org.spongepowered.asm.mixin.injection.At;
@@ -40,6 +41,8 @@ private boolean isSenderLocalPlayer(UUID uuid) {
4041
throw new IllegalStateException("@Shadow transformation failed. Should never happen.");
4142
}
4243

44+
@Shadow @Final private Minecraft minecraft;
45+
4346
@Inject(method = "handleSystemMessage", at = @At("HEAD"), cancellable = true)
4447
private void onHandleSystemMessage(Component message, boolean overlay, CallbackInfo info) {
4548
if (message instanceof MutableComponent mutable && message.getContents() instanceof TranslatableContents translatable) {
@@ -115,7 +118,8 @@ private void onEvaluateTrustLevel(PlayerChatMessage playerChatMessage, Component
115118
@ModifyVariable(method = "narrateChatMessage(Lnet/minecraft/network/chat/ChatType$Bound;"
116119
+ "Lnet/minecraft/network/chat/Component;)V", at = @At("HEAD"), argsOnly = true)
117120
private Component decryptNarratedMessage(Component msg) {
118-
return EncryptionUtil.tryDecrypt(msg).orElse(msg);
121+
assert minecraft.level != null;
122+
return EncryptionUtil.tryDecrypt(minecraft.level.registryAccess(), msg).orElse(msg);
119123
}
120124

121125
}

0 commit comments

Comments
 (0)