From f884e049c006f5fae14abaab9332ccaad9f001e0 Mon Sep 17 00:00:00 2001 From: Adrian <68704415+4drian3d@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:29:04 -0500 Subject: [PATCH] Log the protocol phase in case of trying to obtain a packet id not existing in the phase (#1107) --- .../proxy/protocol/StateRegistry.java | 13 ++++++++----- .../proxy/protocol/PacketRegistryTest.java | 12 ++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 40d6d015..8ebb7ead 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -550,8 +550,8 @@ public enum StateRegistry { public static final int STATUS_ID = 1; public static final int LOGIN_ID = 2; - protected final PacketRegistry clientbound = new PacketRegistry(CLIENTBOUND); - protected final PacketRegistry serverbound = new PacketRegistry(SERVERBOUND); + protected final PacketRegistry clientbound = new PacketRegistry(CLIENTBOUND, this); + protected final PacketRegistry serverbound = new PacketRegistry(SERVERBOUND, this); public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(Direction direction, ProtocolVersion version) { @@ -562,11 +562,13 @@ public enum StateRegistry { public static class PacketRegistry { private final Direction direction; + private final StateRegistry registry; private final Map versions; private boolean fallback = true; - PacketRegistry(Direction direction) { + PacketRegistry(Direction direction, StateRegistry registry) { this.direction = direction; + this.registry = registry; Map mutableVersions = new EnumMap<>(ProtocolVersion.class); for (ProtocolVersion version : ProtocolVersion.values()) { @@ -693,8 +695,9 @@ public enum StateRegistry { final int id = this.packetClassToId.getInt(packet.getClass()); if (id == Integer.MIN_VALUE) { throw new IllegalArgumentException(String.format( - "Unable to find id for packet of type %s in %s protocol %s", - packet.getClass().getName(), PacketRegistry.this.direction, this.version + "Unable to find id for packet of type %s in %s protocol %s phase %s", + packet.getClass().getName(), PacketRegistry.this.direction, + this.version, PacketRegistry.this.registry )); } return id; diff --git a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java index 81a18e72..05a83eea 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -44,7 +44,7 @@ class PacketRegistryTest { private StateRegistry.PacketRegistry setupRegistry() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, null, false), new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false), @@ -84,7 +84,7 @@ class PacketRegistryTest { @Test void failOnNoMappings() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new)); assertThrows(IllegalArgumentException.class, @@ -94,7 +94,7 @@ class PacketRegistryTest { @Test void failOnWrongOrder() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false), @@ -115,7 +115,7 @@ class PacketRegistryTest { @Test void failOnDuplicate() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false)); assertThrows(IllegalArgumentException.class, @@ -129,7 +129,7 @@ class PacketRegistryTest { @Test void shouldNotFailWhenRegisterLatestProtocolVersion() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); assertDoesNotThrow(() -> registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false), new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), @@ -139,7 +139,7 @@ class PacketRegistryTest { @Test void registrySuppliesCorrectPacketsByProtocol() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolUtils.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false), new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, null, false),