Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #479 #480

Open
wants to merge 1 commit into
base: 1.21-Unified
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading