Add packet class to PacketErrorEvent, add missing EntityEvent
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
Deploy / build (push) Has been cancelled

This commit is contained in:
onebeastchris 2024-12-06 21:04:34 +08:00
parent 9332151a49
commit d069361202
3 changed files with 18 additions and 5 deletions

View file

@ -1,13 +1,15 @@
package org.geysermc.mcprotocollib.network.event.session;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.packet.Packet;
/**
* Called when a session encounters an error while reading or writing packet data.
*/
public class PacketErrorEvent implements SessionEvent {
private final Session session;
private final Throwable cause;
private final @Nullable Class<? extends Packet> packetClass;
private boolean suppress = false;
/**
@ -16,9 +18,10 @@ public class PacketErrorEvent implements SessionEvent {
* @param session Session that the error came from.
* @param cause Cause of the error.
*/
public PacketErrorEvent(Session session, Throwable cause) {
public PacketErrorEvent(Session session, Throwable cause, @Nullable Class<? extends Packet> packetClass) {
this.session = session;
this.cause = cause;
this.packetClass = packetClass;
}
/**
@ -39,6 +42,15 @@ public class PacketErrorEvent implements SessionEvent {
return this.cause;
}
/**
* Gets the packet class where the error occurred in, if it is known.
*
* @return the packet class related to this packet error
*/
public Class<? extends Packet> getPacketClass() {
return this.packetClass;
}
/**
* Gets whether the error should be suppressed. If the error is not suppressed,
* it will be passed on through internal error handling and disconnect the session.

View file

@ -57,7 +57,7 @@ public class TcpPacketCodec extends MessageToMessageCodec<ByteBuf, Packet> {
} catch (Throwable t) {
log.debug(marker, "Error encoding packet", t);
PacketErrorEvent e = new PacketErrorEvent(this.session, t);
PacketErrorEvent e = new PacketErrorEvent(this.session, t, packet.getClass());
this.session.callEvent(e);
if (!e.shouldSuppress()) {
throw new EncoderException(t);
@ -104,7 +104,7 @@ public class TcpPacketCodec extends MessageToMessageCodec<ByteBuf, Packet> {
// Advance buffer to end to make sure remaining data in this packet is skipped.
buf.readerIndex(buf.readerIndex() + buf.readableBytes());
PacketErrorEvent e = new PacketErrorEvent(this.session, t);
PacketErrorEvent e = new PacketErrorEvent(this.session, t, packet.getClass());
this.session.callEvent(e);
if (!e.shouldSuppress()) {
throw new DecoderException(t);

View file

@ -66,7 +66,8 @@ public enum EntityEvent {
WARDEN_SONIC_BOOM,
SNIFFER_MAKE_SOUND,
ARMADILLO_PEEKING,
LIVING_EQUIPMENT_BREAK_BODY;
LIVING_EQUIPMENT_BREAK_BODY,
SHAKE;
private static final EntityEvent[] VALUES = values();