From 7e552c16e63222ba2767aef927b5a2b08cf82031 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Thu, 14 May 2026 20:07:00 +0200 Subject: [PATCH] init network channel on client thread --- .../AlchemicalWizardry.java | 2 + .../common/NewPacketHandler.java | 49 +++++++++---------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java index 00b19596..0a16c178 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java +++ b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java @@ -65,6 +65,7 @@ import WayofTime.alchemicalWizardry.common.CommonProxy; import WayofTime.alchemicalWizardry.common.LifeBucketHandler; import WayofTime.alchemicalWizardry.common.LifeEssence; import WayofTime.alchemicalWizardry.common.ModLivingDropsEvent; +import WayofTime.alchemicalWizardry.common.NewPacketHandler; import WayofTime.alchemicalWizardry.common.achievements.ModAchievements; import WayofTime.alchemicalWizardry.common.alchemy.CombinedPotionRegistry; import WayofTime.alchemicalWizardry.common.block.ArmourForge; @@ -689,6 +690,7 @@ public class AlchemicalWizardry { HoldingPacketHandler.init(); ClientToServerPacketHandler.init(); ModAchievements.init(); + NewPacketHandler.init(); } /** diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java b/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java index de84b391..d7b8dd97 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java @@ -60,10 +60,16 @@ public enum NewPacketHandler { } } + public static void init() { + // called on the client thread + // to avoid leaking the server thread + // if initialized on the server thread + } + @SideOnly(Side.CLIENT) private void addClientHandler() { FMLEmbeddedChannel clientChannel = this.channels.get(Side.CLIENT); - + // spotless:off String tileAltarCodec = clientChannel.findChannelHandlerNameForType(TEAltarCodec.class); clientChannel.pipeline().addAfter(tileAltarCodec, "TEAltarHandler", new TEAltarMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEOrientableHandler", new TEOrientableMessageHandler()); @@ -75,15 +81,12 @@ public enum NewPacketHandler { clientChannel.pipeline().addAfter(tileAltarCodec, "ParticleHandler", new ParticleMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "VelocityHandler", new VelocityMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEMasterStoneHandler", new TEMasterStoneMessageHandler()); - clientChannel.pipeline() - .addAfter(tileAltarCodec, "TEReagentConduitHandler", new TEReagentConduitMessageHandler()); + clientChannel.pipeline().addAfter(tileAltarCodec, "TEReagentConduitHandler", new TEReagentConduitMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "CurrentLPMessageHandler", new CurrentLPMessageHandler()); - clientChannel.pipeline() - .addAfter(tileAltarCodec, "CurrentReagentBarMessageHandler", new CurrentReagentBarMessageHandler()); - clientChannel.pipeline() - .addAfter(tileAltarCodec, "CurrentAddedHPMessageHandler", new CurrentAddedHPMessageHandler()); - clientChannel.pipeline() - .addAfter(tileAltarCodec, "GaiaBiomeChangeHandler", new GaiaBiomeChangeMessageHandler()); + clientChannel.pipeline().addAfter(tileAltarCodec, "CurrentReagentBarMessageHandler", new CurrentReagentBarMessageHandler()); + clientChannel.pipeline().addAfter(tileAltarCodec, "CurrentAddedHPMessageHandler", new CurrentAddedHPMessageHandler()); + clientChannel.pipeline().addAfter(tileAltarCodec, "GaiaBiomeChangeHandler", new GaiaBiomeChangeMessageHandler()); + // spotless:on } @SideOnly(Side.SERVER) @@ -997,37 +1000,33 @@ public enum NewPacketHandler { return INSTANCE.channels.get(Side.SERVER).generatePacketFrom(msg); } + // spotless:off public void sendTo(Packet message, EntityPlayerMP player) { - this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET) - .set(FMLOutboundHandler.OutboundTarget.PLAYER); + this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player); - this.channels.get(Side.SERVER).writeAndFlush(message); + this.channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToAll(Packet message) { - this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET) - .set(FMLOutboundHandler.OutboundTarget.ALL); - this.channels.get(Side.SERVER).writeAndFlush(message); + this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL); + this.channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToAllAround(Packet message, NetworkRegistry.TargetPoint point) { - this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET) - .set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT); + this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point); - this.channels.get(Side.SERVER).writeAndFlush(message); + this.channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToDimension(Packet message, Integer dimensionId) { - this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET) - .set(FMLOutboundHandler.OutboundTarget.DIMENSION); + this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId); - this.channels.get(Side.SERVER).writeAndFlush(message); + this.channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } public void sendToServer(Packet message) { - this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET) - .set(FMLOutboundHandler.OutboundTarget.TOSERVER); - this.channels.get(Side.CLIENT).writeAndFlush(message) - .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER); + this.channels.get(Side.CLIENT).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } + // spotless:on }