diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/PacketCancelException.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/PacketCancelException.java new file mode 100644 index 00000000..184c7169 --- /dev/null +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/PacketCancelException.java @@ -0,0 +1,7 @@ +package org.geysermc.mcprotocollib.network.packet; + +public class PacketCancelException extends RuntimeException { + public PacketCancelException() { + super("This packet should not be sent out!"); + } +} diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketCodec.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketCodec.java index 91c8b4fb..141b1b98 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketCodec.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketCodec.java @@ -11,6 +11,7 @@ import org.geysermc.mcprotocollib.network.codec.PacketDefinition; import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent; import org.geysermc.mcprotocollib.network.packet.FakeFlushPacket; import org.geysermc.mcprotocollib.network.packet.Packet; +import org.geysermc.mcprotocollib.network.packet.PacketCancelException; import org.geysermc.mcprotocollib.network.packet.PacketProtocol; import org.geysermc.mcprotocollib.network.packet.PacketRegistry; import org.slf4j.Logger; @@ -34,7 +35,7 @@ public class TcpPacketCodec extends ByteToMessageCodec { public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) { if (packet == FakeFlushPacket.INSTANCE) { log.debug("Fake flush packet reached"); - return; + throw new PacketCancelException(); } if (log.isTraceEnabled()) { diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java index af370e58..b20c537f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java @@ -22,6 +22,7 @@ import org.geysermc.mcprotocollib.network.event.session.PacketSendingEvent; import org.geysermc.mcprotocollib.network.event.session.SessionEvent; import org.geysermc.mcprotocollib.network.event.session.SessionListener; import org.geysermc.mcprotocollib.network.packet.Packet; +import org.geysermc.mcprotocollib.network.packet.PacketCancelException; import org.geysermc.mcprotocollib.network.packet.PacketProtocol; import java.net.SocketAddress; @@ -315,6 +316,10 @@ public abstract class TcpSession extends SimpleChannelInboundHandler imp @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + if (cause instanceof PacketCancelException) { + return; + } + Component message; if (cause instanceof TimeoutException) { message = Component.translatable("disconnect.timeout");