Skip to content

Commit

Permalink
Cancel packet using exception rather than return
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Sep 12, 2024
1 parent d4eddfa commit 6507e77
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand All @@ -34,7 +35,7 @@ public TcpPacketCodec(Session session, boolean client) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -315,6 +316,10 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof PacketCancelException) {
return;
}

Component message;
if (cause instanceof TimeoutException) {
message = Component.translatable("disconnect.timeout");
Expand Down

0 comments on commit 6507e77

Please sign in to comment.