Skip to content

Commit

Permalink
minecraft/packet.go: Put packet type name in error instead of ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Oct 29, 2024
1 parent 8883ef9 commit 737a07a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions minecraft/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,29 @@ func (err unknownPacketError) Error() string {

// decode decodes the packet payload held in the packetData and returns the packet.Packet decoded.
func (p *packetData) decode(conn *Conn) (pks []packet.Packet, err error) {
defer func() {
if recoveredErr := recover(); recoveredErr != nil {
err = fmt.Errorf("decode packet %v: %w", p.h.PacketID, recoveredErr.(error))
}
if err == nil {
return
}
if ok := errors.As(err, &unknownPacketError{}); ok || conn.disconnectOnInvalidPacket {
_ = conn.Close()
}
}()

// Attempt to fetch the packet with the right packet ID from the pool.
pkFunc, ok := conn.pool[p.h.PacketID]
var pk packet.Packet
if !ok {
// No packet with the ID. This may be a custom packet of some sorts.
pk = &packet.Unknown{PacketID: p.h.PacketID}
if conn.disconnectOnUnknownPacket {
_ = conn.Close()
return nil, unknownPacketError{id: p.h.PacketID}
}
} else {
pk = pkFunc()
}

defer func() {
if recoveredErr := recover(); recoveredErr != nil {
err = fmt.Errorf("decode packet %T: %w", pk, recoveredErr.(error))
}
if err != nil && !errors.Is(err, unknownPacketError{}) && conn.disconnectOnInvalidPacket {
_ = conn.Close()
}
}()

r := conn.proto.NewReader(p.payload, conn.shieldID.Load(), conn.readerLimits)
pk.Marshal(r)
if p.payload.Len() != 0 {
Expand Down

0 comments on commit 737a07a

Please sign in to comment.