Skip to content

Commit

Permalink
Fix #479 (#480)
Browse files Browse the repository at this point in the history
This bug only happens on 1.21 due to enchantments being made data-driven.
  • Loading branch information
forkiesassds authored Oct 28, 2024
1 parent b255be3 commit 0400e5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,13 +16,13 @@

public class EncryptionUtil {

public static Optional<Component> tryDecrypt(Component component) {
public static Optional<Component> 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);
Expand Down Expand Up @@ -81,8 +82,8 @@ public static Optional<String> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 0400e5f

Please sign in to comment.