diff --git a/forge/src/main/java/com/aizistral/nochatreports/forge/mixins/client/MixinServerStatusPinger$1.java b/forge/src/main/java/com/aizistral/nochatreports/forge/mixins/client/MixinServerStatusPinger$1.java deleted file mode 100644 index 7c19827a..00000000 --- a/forge/src/main/java/com/aizistral/nochatreports/forge/mixins/client/MixinServerStatusPinger$1.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.aizistral.nochatreports.forge.mixins.client; - -import java.lang.reflect.Field; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.aizistral.nochatreports.common.NCRCore; -import com.aizistral.nochatreports.common.config.NCRConfig; -import com.aizistral.nochatreports.common.core.ServerDataExtension; -import com.aizistral.nochatreports.common.platform.extensions.ServerPingerExtension; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ServerData; -import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket; -import net.minecraft.network.protocol.status.ServerStatus; - -@Mixin(targets = "net/minecraft/client/multiplayer/ServerStatusPinger$1") -public class MixinServerStatusPinger$1 implements ServerPingerExtension { - private static final Field SERVER_DATA_FIELD; - - static { - // We use reflection here because Mixin's AP dies when trying to process @Shadow of this lol - try { - Class pinger = Class.forName("net.minecraft.client.multiplayer.ServerStatusPinger$1"); - - SERVER_DATA_FIELD = pinger.getDeclaredField("val$p_105460_"); - SERVER_DATA_FIELD.setAccessible(true); - } catch (Exception ex) { - Error error = new Error("Reflection failed in MixinServerStatusPinger$1!", ex); - - NCRCore.LOGGER.catching(error); - Minecraft.getInstance().execute(() -> { - throw error; - }); - - throw error; - } - } - - @Override - public ServerData getServerData() { - try { - return (ServerData) SERVER_DATA_FIELD.get(this); - } catch (Exception ex) { - throw new RuntimeException("Failed to get ServerData field via reflection", ex); - } - } - -} diff --git a/forge/src/main/resources/mixins/forge/nochatreports-forge.mixins.json b/forge/src/main/resources/mixins/forge/nochatreports-forge.mixins.json index fc7bc593..9fde6e0f 100644 --- a/forge/src/main/resources/mixins/forge/nochatreports-forge.mixins.json +++ b/forge/src/main/resources/mixins/forge/nochatreports-forge.mixins.json @@ -7,8 +7,7 @@ "client": [ "client.MixinClientConnection", "client.MixinClientHandshakePacketListenerImpl", - "client.MixinClientPacketListener", - "client.MixinServerStatusPinger$1" + "client.MixinClientPacketListener" ], "server": [], "injectors": { diff --git a/neoforge/src/main/java/com/aizistral/nochatreports/neoforge/mixins/client/MixinServerStatusPinger$1.java b/neoforge/src/main/java/com/aizistral/nochatreports/neoforge/mixins/client/MixinServerStatusPinger$1.java deleted file mode 100644 index 80bb635b..00000000 --- a/neoforge/src/main/java/com/aizistral/nochatreports/neoforge/mixins/client/MixinServerStatusPinger$1.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.aizistral.nochatreports.neoforge.mixins.client; - -import com.aizistral.nochatreports.common.NCRCore; -import com.aizistral.nochatreports.common.platform.extensions.ServerPingerExtension; -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ServerData; -import org.spongepowered.asm.mixin.Mixin; - -import java.lang.reflect.Field; - -@Mixin(targets = "net/minecraft/client/multiplayer/ServerStatusPinger$1") -public class MixinServerStatusPinger$1 implements ServerPingerExtension { - private static final Field SERVER_DATA_FIELD; - - static { - // We use reflection here because Mixin's AP dies when trying to process @Shadow of this lol - try { - Class pinger = Class.forName("net.minecraft.client.multiplayer.ServerStatusPinger$1"); - - SERVER_DATA_FIELD = pinger.getDeclaredField("val$p_105460_"); - SERVER_DATA_FIELD.setAccessible(true); - } catch (Exception ex) { - Error error = new Error("Reflection failed in MixinServerStatusPinger$1!", ex); - - NCRCore.LOGGER.catching(error); - Minecraft.getInstance().execute(() -> { - throw error; - }); - - throw error; - } - } - - @Override - public ServerData getServerData() { - try { - return (ServerData) SERVER_DATA_FIELD.get(this); - } catch (Exception ex) { - throw new RuntimeException("Failed to get ServerData field via reflection", ex); - } - } - -} diff --git a/neoforge/src/main/resources/mixins/neoforge/nochatreports-neoforge.mixins.json b/neoforge/src/main/resources/mixins/neoforge/nochatreports-neoforge.mixins.json index 809694f0..d67fdbe9 100644 --- a/neoforge/src/main/resources/mixins/neoforge/nochatreports-neoforge.mixins.json +++ b/neoforge/src/main/resources/mixins/neoforge/nochatreports-neoforge.mixins.json @@ -7,8 +7,7 @@ "client": [ "client.MixinClientConnection", "client.MixinClientHandshakePacketListenerImpl", - "client.MixinClientPacketListener", - "client.MixinServerStatusPinger$1" + "client.MixinClientPacketListener" ], "server": [], "injectors": { diff --git a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerStatusPinger$1.java b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerStatusPinger$1.java index 81c14e07..bb19ae7d 100644 --- a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerStatusPinger$1.java +++ b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerStatusPinger$1.java @@ -1,8 +1,12 @@ package com.aizistral.nochatreports.common.mixins.client; -import java.lang.reflect.Field; +import java.net.InetSocketAddress; +import net.minecraft.client.multiplayer.ServerStatusPinger; +import net.minecraft.client.multiplayer.resolver.ServerAddress; +import net.minecraft.network.Connection; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -12,28 +16,43 @@ import com.aizistral.nochatreports.common.core.ServerDataExtension; import com.aizistral.nochatreports.common.platform.extensions.ServerPingerExtension; -import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ServerData; import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket; import net.minecraft.network.protocol.status.ServerStatus; + +/** + * This one ensures "preventsChatReports" property is transferred from {@link ServerStatus} to + * {@link ServerData} when handling status response. + * @author fxmorin (original implementation) + * @author Aizistral (current version) + * @author pietro-lopes (fixed https://github.com/Aizistral-Studios/No-Chat-Reports/issues/481) + */ + @Mixin(targets = "net/minecraft/client/multiplayer/ServerStatusPinger$1") public abstract class MixinServerStatusPinger$1 implements ServerPingerExtension { - /** - * @reason Ensure "preventsChatReports" property is transferred from {@link ServerStatus} to - * {@link ServerData} when handling status response. - * @author fxmorin (original implementation) - * @author Aizistral (current version) - */ + @Unique + private ServerDataExtension nochatreports$serverData; + + @Inject(method = "", at = @At("RETURN")) + private void captureServerData(ServerStatusPinger serverStatusPinger, Connection connection, ServerData serverData, Runnable runnable, Runnable runnable2, InetSocketAddress inetSocketAddress, ServerAddress serverAddress, CallbackInfo ci){ + this.nochatreports$serverData = (ServerDataExtension) serverData; + } @Inject(method = "handleStatusResponse(Lnet/minecraft/network/protocol/status/ClientboundStatusResponsePacket;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/status/ServerStatus;" - + "description()Lnet/minecraft/network/chat/Component;", - ordinal = 0, shift = At.Shift.BEFORE)) + + "description()Lnet/minecraft/network/chat/Component;")) private void getNoChatReports(ClientboundStatusResponsePacket packet, CallbackInfo info) { boolean preventsReports = ((ServerDataExtension) (Object) packet.status()).preventsChatReports(); - ((ServerDataExtension) this.getServerData()).setPreventsChatReports(preventsReports); + + if (this.nochatreports$serverData == null) { + NCRCore.LOGGER.error("Failed to capture ServerData instance in MixinServerStatusPinger$1!"); + NCRCore.LOGGER.catching(new IllegalStateException()); + return; + } + + this.nochatreports$serverData.setPreventsChatReports(preventsReports); if (NCRConfig.getCommon().enableDebugLog()) { NCRCore.LOGGER.info("Received status response packet from server, preventsChatReports: {}",