From 1c8a5702e79993a629d54a2aa164f5b24ebc9c99 Mon Sep 17 00:00:00 2001 From: Aizistral Date: Tue, 18 Aug 2026 10:49:25 +0200 Subject: Fix server ping crash Fixes #518 --- .../common/mixins/client/MixinServerData.java | 5 +- .../mixins/client/MixinServerStatusPinger$1.java | 66 ++++++++++++++++------ 2 files changed, 53 insertions(+), 18 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerData.java b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerData.java index 095cf7f..cdce6a6 100644 --- a/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerData.java +++ b/src/main/java/com/aizistral/nochatreports/common/mixins/client/MixinServerData.java @@ -1,6 +1,7 @@ package com.aizistral.nochatreports.common.mixins.client; 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.CallbackInfoReturnable; @@ -18,9 +19,9 @@ import net.minecraft.nbt.CompoundTag; * @author fxmorin (original implementation) * @author Aizistral (current version) */ - @Mixin(ServerData.class) public class MixinServerData implements ServerDataExtension { + @Unique private boolean preventsChatReports; @Override @@ -40,7 +41,7 @@ public class MixinServerData implements ServerDataExtension { @Inject(method = "read", locals = LocalCapture.CAPTURE_FAILSOFT, at = @At("RETURN")) private static void onRead(CompoundTag tag, CallbackInfoReturnable info, ServerData data) { - ((ServerDataExtension)data).setPreventsChatReports(tag.getBoolean("preventsChatReports")); + ((ServerDataExtension) data).setPreventsChatReports(tag.getBoolean("preventsChatReports")); } } 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 976e9c6..2b1a0fa 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,37 +16,67 @@ 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; +/** + * 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 pinger, + Connection connection, + ServerData serverData, + Runnable runnable, + InetSocketAddress socketAddress, + CallbackInfo info + ) { + try { + this.nochatreports$serverData = (ServerDataExtension) serverData; + } catch (Throwable ex) { + NCRCore.LOGGER.error("Failed to capture server data in MixinServerStatusPinger$1!", ex); + } + } @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); + try { + boolean preventsReports = ((ServerDataExtension) (Object) packet.status()).preventsChatReports(); - if (NCRConfig.getCommon().enableDebugLog()) { - NCRCore.LOGGER.info("Received status response packet from server, preventsChatReports: {}", - 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: {}", + preventsReports); + } + } catch (Throwable ex) { + NCRCore.LOGGER.error("Failed to handle status response in MixinServerStatusPinger$1!", ex); } } static { - NCRCore.LOGGER.info("Common mixin into ServerStatusPinger$1 succeeded."); + if (NCRConfig.getCommon().enableDebugLog()) { + NCRCore.LOGGER.info("Common mixin into ServerStatusPinger$1 succeeded."); + } } } -- cgit v1.2.3