From 8c4d71072591eb7880d40792c904e996c770ad3d Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 30 Jul 2018 21:35:11 -0400 Subject: [PATCH] Gracefully fall back for STATUS/HANDSHAKE/LOGIN states --- .../proxy/protocol/ProtocolConstants.java | 2 -- .../velocitypowered/proxy/protocol/StateRegistry.java | 11 ++++++++--- .../proxy/protocol/PacketRegistryTest.java | 7 +++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java b/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java index a2007c1ff..e8943a68e 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java @@ -3,7 +3,6 @@ package com.velocitypowered.proxy.protocol; import java.util.Arrays; public enum ProtocolConstants { ; - public static final int MINECRAFT_1_7_2 = 4; public static final int MINECRAFT_1_9 = 107; public static final int MINECRAFT_1_9_1 = 108; public static final int MINECRAFT_1_9_2 = 109; @@ -15,7 +14,6 @@ public enum ProtocolConstants { ; public static final int MINECRAFT_1_12_1 = 338; public static final int MINECRAFT_1_12_2 = 340; - public static final int MINIMUM_VERSION_SUPPORTED = MINECRAFT_1_9; public static final int MINIMUM_GENERIC_VERSION = MINECRAFT_1_9; public static final int[] SUPPORTED_VERSIONS = new int[] { diff --git a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index e6fcc74e2..846859016 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -106,8 +106,8 @@ public enum StateRegistry { } }; - public final PacketRegistry CLIENTBOUND = new PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND); - public final PacketRegistry SERVERBOUND = new PacketRegistry(ProtocolConstants.Direction.SERVERBOUND); + public final PacketRegistry CLIENTBOUND = new PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, this); + public final PacketRegistry SERVERBOUND = new PacketRegistry(ProtocolConstants.Direction.SERVERBOUND, this); public static class PacketRegistry { private static final IntObjectMap LINKED_PROTOCOL_VERSIONS = new IntObjectHashMap<>(); @@ -120,10 +120,12 @@ public enum StateRegistry { } private final ProtocolConstants.Direction direction; + private final StateRegistry state; private final IntObjectMap versions = new IntObjectHashMap<>(); - public PacketRegistry(ProtocolConstants.Direction direction) { + public PacketRegistry(Direction direction, StateRegistry state) { this.direction = direction; + this.state = state; for (int version : ProtocolConstants.SUPPORTED_VERSIONS) { versions.put(version, new ProtocolVersion(version)); } @@ -133,6 +135,9 @@ public enum StateRegistry { public ProtocolVersion getVersion(final int version) { ProtocolVersion result = versions.get(version); if (result == null) { + if (state != PLAY) { + return getVersion(MINIMUM_GENERIC_VERSION); + } throw new IllegalArgumentException("Could not find data for protocol version " + version); } return result; diff --git a/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java b/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java index e6abe22d3..b332f8f14 100644 --- a/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -1,7 +1,6 @@ package com.velocitypowered.proxy.protocol; import com.velocitypowered.proxy.protocol.packets.Handshake; -import com.velocitypowered.proxy.protocol.packets.KeepAlive; import org.junit.jupiter.api.Test; import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12; @@ -11,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.*; class PacketRegistryTest { private StateRegistry.PacketRegistry setupRegistry() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND); + StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12)); return registry; } @@ -37,14 +36,14 @@ class PacketRegistryTest { @Test void failOnNoMappings() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND); + StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE); assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new)); assertThrows(IllegalArgumentException.class, () -> registry.getVersion(0).getPacketId(new Handshake())); } @Test void registrySuppliesCorrectPacketsByProtocol() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND); + StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12), new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1)); assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12).createPacket(0x00).getClass());