Cancel packet using exception rather than return

This commit is contained in:
AlexProgrammerDE 2024-09-12 11:18:16 +02:00
parent d4eddfa4c0
commit 6507e77bbe
3 changed files with 14 additions and 1 deletions

View file

@ -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!");
}
}

View file

@ -11,6 +11,7 @@ import org.geysermc.mcprotocollib.network.codec.PacketDefinition;
import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent; import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent;
import org.geysermc.mcprotocollib.network.packet.FakeFlushPacket; import org.geysermc.mcprotocollib.network.packet.FakeFlushPacket;
import org.geysermc.mcprotocollib.network.packet.Packet; 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.PacketProtocol;
import org.geysermc.mcprotocollib.network.packet.PacketRegistry; import org.geysermc.mcprotocollib.network.packet.PacketRegistry;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -34,7 +35,7 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) { public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) {
if (packet == FakeFlushPacket.INSTANCE) { if (packet == FakeFlushPacket.INSTANCE) {
log.debug("Fake flush packet reached"); log.debug("Fake flush packet reached");
return; throw new PacketCancelException();
} }
if (log.isTraceEnabled()) { if (log.isTraceEnabled()) {

View file

@ -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.SessionEvent;
import org.geysermc.mcprotocollib.network.event.session.SessionListener; import org.geysermc.mcprotocollib.network.event.session.SessionListener;
import org.geysermc.mcprotocollib.network.packet.Packet; 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.PacketProtocol;
import java.net.SocketAddress; import java.net.SocketAddress;
@ -315,6 +316,10 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof PacketCancelException) {
return;
}
Component message; Component message;
if (cause instanceof TimeoutException) { if (cause instanceof TimeoutException) {
message = Component.translatable("disconnect.timeout"); message = Component.translatable("disconnect.timeout");