diff --git a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 6c62a558b..18f06b17a 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -110,6 +110,9 @@ public enum StateRegistry { for (final PacketMapping mapping : mappings) { ProtocolVersion version = this.versions.get(mapping.protocolVersion); + if (version == null) { + throw new IllegalArgumentException("Unknown protocol version " + mapping.protocolVersion); + } version.packetIdToSupplier.put(mapping.id, packetSupplier); version.packetClassToId.put(clazz, mapping.id); diff --git a/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java b/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java index a6c8b0c0f..d1f33dfb8 100644 --- a/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -1,7 +1,7 @@ package com.velocitypowered.proxy.protocol; import com.velocitypowered.proxy.protocol.packets.Handshake; -import com.velocitypowered.proxy.protocol.packets.Ping; +import com.velocitypowered.proxy.protocol.packets.KeepAlive; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -9,38 +9,27 @@ import static org.junit.jupiter.api.Assertions.*; class PacketRegistryTest { private StateRegistry.PacketRegistry setupRegistry() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND); - registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, 1)); - registry.register(Ping.class, Ping::new, new StateRegistry.PacketMapping(0x01, 1), - new StateRegistry.PacketMapping(0x02, 5)); + registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, ProtocolConstants.MINECRAFT_1_12)); return registry; } @Test void packetRegistryWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - MinecraftPacket packet = registry.getVersion(1).createPacket(0); + MinecraftPacket packet = registry.getVersion(ProtocolConstants.MINECRAFT_1_12).createPacket(0); assertNotNull(packet, "Packet was not found in registry"); assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class"); - assertEquals(0, registry.getVersion(1).getPacketId(packet), "Registry did not return the correct packet ID"); + assertEquals(0, registry.getVersion(ProtocolConstants.MINECRAFT_1_12).getPacketId(packet), "Registry did not return the correct packet ID"); } @Test - void packetRegistryRevertsToBestOldVersion() { + void packetRegistryLinkingWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - MinecraftPacket packet = registry.getVersion(2).createPacket(0); + MinecraftPacket packet = registry.getVersion(ProtocolConstants.MINECRAFT_1_12_1).createPacket(0); assertNotNull(packet, "Packet was not found in registry"); assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class"); - - assertEquals(0, registry.getVersion(2).getPacketId(packet), "Registry did not return the correct packet ID"); - } - - @Test - void packetRegistryDoesntProvideNewPacketsForOld() { - StateRegistry.PacketRegistry registry = setupRegistry(); - assertNull(registry.getVersion(0).createPacket(0), "Packet was found in registry despite being too new"); - - assertThrows(IllegalArgumentException.class, () -> registry.getVersion(0).getPacketId(new Handshake()), "Registry provided new packets for an old protocol version"); + assertEquals(0, registry.getVersion(ProtocolConstants.MINECRAFT_1_12_1).getPacketId(packet), "Registry did not return the correct packet ID"); } @Test @@ -49,16 +38,4 @@ class PacketRegistryTest { assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new)); assertThrows(IllegalArgumentException.class, () -> registry.getVersion(0).getPacketId(new Handshake())); } - - @Test - void packetRegistryProvidesCorrectVersionsForMultipleMappings() { - StateRegistry.PacketRegistry registry = setupRegistry(); - assertNotNull(registry.getVersion(1).createPacket(1), "Packet was not found in registry despite being being registered with ID 1 and version 1"); - assertNotNull(registry.getVersion(2).createPacket(1), "Packet was not found in registry despite being being registered with ID 1 and version 1 (we are looking up version 2)"); - assertNotNull(registry.getVersion(5).createPacket(2), "Packet was not found in registry despite being being registered with ID 2 and version 5"); - assertNotNull(registry.getVersion(6).createPacket(2), "Packet was not found in registry despite being being registered with ID 2 and version 5 (we are looking up version 6)"); - - assertEquals(1, registry.getVersion(1).getPacketId(new Ping()), "Wrong ID provided from registry"); - assertEquals(2, registry.getVersion(5).getPacketId(new Ping()), "Wrong ID provided from registry"); - } } \ No newline at end of file