diff --git a/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientBusinessHandler.java b/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientBusinessHandler.java index 7beb4974..66a11782 100644 --- a/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientBusinessHandler.java +++ b/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientBusinessHandler.java @@ -1,8 +1,9 @@ package com.xujiayao.discord_mc_chat.client.handlers; -import com.xujiayao.discord_mc_chat.client.NettyClient; +import com.xujiayao.discord_mc_chat.commands.CommandEvents; import com.xujiayao.discord_mc_chat.network.Packet; import com.xujiayao.discord_mc_chat.network.Packets; +import com.xujiayao.discord_mc_chat.utils.events.EventManager; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -15,12 +16,6 @@ import static com.xujiayao.discord_mc_chat.Constants.LOGGER; */ public class ClientBusinessHandler extends SimpleChannelInboundHandler { - private final NettyClient client; - - public ClientBusinessHandler(NettyClient client) { - this.client = client; - } - @Override protected void channelRead0(ChannelHandlerContext ctx, Packet packet) { if (packet instanceof Packets.HandshakeResponse(boolean success, String message)) { @@ -28,8 +23,11 @@ public class ClientBusinessHandler extends SimpleChannelInboundHandler { LOGGER.info("Handshake with server successful with message: \"{}\"", message); } else { LOGGER.error("Handshake with server failed with message: \"{}\"", message); - LOGGER.error("This is a fatal error. DMCC client will shut down."); - client.stop(); // Stop the client and prevent reconnection + LOGGER.error("This is a fatal error. DMCC client will now shut down."); + + // Post a StopEvent to trigger a graceful, application-wide shutdown. + // This reuses the existing shutdown logic in CommandEventHandler. + EventManager.post(new CommandEvents.StopEvent()); } } // TODO: Handle other packet types diff --git a/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientChannelInitializer.java b/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientChannelInitializer.java index 165c73eb..cb518095 100644 --- a/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientChannelInitializer.java +++ b/core/src/main/java/com/xujiayao/discord_mc_chat/client/handlers/ClientChannelInitializer.java @@ -47,6 +47,6 @@ public class ClientChannelInitializer extends ChannelInitializer pipeline.addLast(new HeartbeatHandler(client)); // Business Logic - pipeline.addLast(new ClientBusinessHandler(client)); + pipeline.addLast(new ClientBusinessHandler()); } }