This commit is contained in:
Xujiayao 2025-11-15 22:38:08 +08:00
parent 93492b53c5
commit 291c848754
2 changed files with 8 additions and 10 deletions

View file

@ -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<Packet> {
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<Packet> {
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

View file

@ -47,6 +47,6 @@ public class ClientChannelInitializer extends ChannelInitializer<SocketChannel>
pipeline.addLast(new HeartbeatHandler(client));
// Business Logic
pipeline.addLast(new ClientBusinessHandler(client));
pipeline.addLast(new ClientBusinessHandler());
}
}