From 6d0ff541f17e3b8358b2c09382c5fbbf52982e78 Mon Sep 17 00:00:00 2001 From: forkiesassds Date: Sun, 30 Jun 2024 14:54:04 +0300 Subject: [PATCH] Fix #479 This bug only happens on 1.21 due to enchantments being made data-driven. --- .../nochatreports/common/core/EncryptionUtil.java | 9 +++++---- .../common/mixins/client/MixinChatComponent.java | 9 +++++++-- .../common/mixins/client/MixinChatListener.java | 6 +++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java b/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java index e379f9c6..173de427 100644 --- a/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java +++ b/src/main/java/com/aizistral/nochatreports/common/core/EncryptionUtil.java @@ -6,6 +6,7 @@ import com.aizistral.nochatreports.common.config.NCRConfig; import com.aizistral.nochatreports.common.encryption.Encryptor; +import net.minecraft.core.HolderLookup; import net.minecraft.core.RegistryAccess; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; @@ -15,13 +16,13 @@ public class EncryptionUtil { - public static Optional tryDecrypt(Component component) { + public static Optional tryDecrypt(HolderLookup.Provider provider, Component component) { var optional = NCRConfig.getEncryption().getEncryptor(); if (optional.isEmpty()) return Optional.empty(); Encryptor encryption = optional.get(); - Component copy = recreate(component); + Component copy = recreate(provider, component); ComponentContents contents = copy.getContents(); return Optional.ofNullable(tryDecrypt(copy, encryption) ? copy : null); @@ -81,8 +82,8 @@ public static Optional tryDecrypt(String message, Encryptor encryptor } } - public static Component recreate(Component component) { - return Component.Serializer.fromJson(Component.Serializer.toJson(component, RegistryAccess.EMPTY), RegistryAccess.EMPTY); + public static Component recreate(HolderLookup.Provider provider, Component component) { + return Component.Serializer.fromJson(Component.Serializer.toJson(component, provider), provider); } } diff --git a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatComponent.java b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatComponent.java index 1d8e15a5..42ed761c 100644 --- a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatComponent.java +++ b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatComponent.java @@ -1,6 +1,9 @@ package com.aizistral.nochatreports.common.mixins.client; +import net.minecraft.client.Minecraft; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At.Shift; @@ -22,6 +25,7 @@ @Mixin(ChatComponent.class) public class MixinChatComponent { + @Shadow @Final private Minecraft minecraft; private static final GuiMessageTag.Icon ENCRYPTED_ICON = GuiMessageTag.Icon.valueOf("CHAT_NCR_ENCRYPTED"); private boolean lastMessageEncrypted; private Component lastMessageOriginal; @@ -44,10 +48,11 @@ private GuiMessage modifyGUIMessage(GuiMessage msg) { Component.Serializer.toJson(msg.content(), RegistryAccess.EMPTY)); } - var decrypted = EncryptionUtil.tryDecrypt(msg.content()); + assert minecraft.level != null; + var decrypted = EncryptionUtil.tryDecrypt(minecraft.level.registryAccess(), msg.content()); decrypted.ifPresentOrElse(component -> { - this.lastMessageOriginal = EncryptionUtil.recreate(msg.content()); + this.lastMessageOriginal = EncryptionUtil.recreate(minecraft.level.registryAccess(), msg.content()); this.lastMessageEncrypted = true; }, () -> this.lastMessageEncrypted = false); diff --git a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatListener.java b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatListener.java index 4a04d2fa..996cb14f 100644 --- a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatListener.java +++ b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinChatListener.java @@ -4,6 +4,7 @@ import java.util.Base64; import java.util.UUID; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -40,6 +41,8 @@ private boolean isSenderLocalPlayer(UUID uuid) { throw new IllegalStateException("@Shadow transformation failed. Should never happen."); } + @Shadow @Final private Minecraft minecraft; + @Inject(method = "handleSystemMessage", at = @At("HEAD"), cancellable = true) private void onHandleSystemMessage(Component message, boolean overlay, CallbackInfo info) { if (message instanceof MutableComponent mutable && message.getContents() instanceof TranslatableContents translatable) { @@ -115,7 +118,8 @@ private void onEvaluateTrustLevel(PlayerChatMessage playerChatMessage, Component @ModifyVariable(method = "narrateChatMessage(Lnet/minecraft/network/chat/ChatType$Bound;" + "Lnet/minecraft/network/chat/Component;)V", at = @At("HEAD"), argsOnly = true) private Component decryptNarratedMessage(Component msg) { - return EncryptionUtil.tryDecrypt(msg).orElse(msg); + assert minecraft.level != null; + return EncryptionUtil.tryDecrypt(minecraft.level.registryAccess(), msg).orElse(msg); } }