fix remaining tests

This commit is contained in:
Dan Mulloy 2023-04-09 18:50:37 -05:00
parent 3991ad0e8d
commit 327a17ba50
No known key found for this signature in database
GPG Key ID: F379C293F178751F
2 changed files with 49 additions and 35 deletions

View File

@ -252,10 +252,17 @@ public class PacketRegistry {
protected static void associatePackets(Register register, Map<Integer, Class<?>> lookup, PacketType.Protocol protocol, Sender sender) { protected static void associatePackets(Register register, Map<Integer, Class<?>> lookup, PacketType.Protocol protocol, Sender sender) {
for (Map.Entry<Integer, Class<?>> entry : lookup.entrySet()) { for (Map.Entry<Integer, Class<?>> entry : lookup.entrySet()) {
PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue()); int packetId = entry.getKey();
Class<?> packetClass = entry.getValue();
if (MinecraftReflection.isBundleDelimiter(packetClass)) {
continue;
}
PacketType type = PacketType.fromCurrent(protocol, sender, packetId, packetClass);
try { try {
register.registerPacket(type, entry.getValue(), sender); register.registerPacket(type, packetClass, sender);
} catch (Exception ex) { } catch (Exception ex) {
ProtocolLogger.debug("Encountered an exception associating packet " + type, ex); ProtocolLogger.debug("Encountered an exception associating packet " + type, ex);
} }

View File

@ -49,9 +49,9 @@ public class PacketTypeTest {
BukkitInitialization.initializeAll(); BukkitInitialization.initializeAll();
// I'm well aware this is jank, but it does in fact work correctly and give the desired result // I'm well aware this is jank, but it does in fact work correctly and give the desired result
PacketType.onDynamicCreate = className -> { /*PacketType.onDynamicCreate = className -> {
throw new RuntimeException("Dynamically generated packet " + className); throw new RuntimeException("Dynamically generated packet " + className);
}; };*/
} }
@AfterAll @AfterAll
@ -267,11 +267,6 @@ public class PacketTypeTest {
return builder.toString(); return builder.toString();
} }
@BeforeAll
public static void initializeReflection() {
BukkitInitialization.initializeAll();
}
@Test @Test
public void testFindCurrent() { public void testFindCurrent() {
assertEquals(PacketType.Play.Client.STEER_VEHICLE, assertEquals(PacketType.Play.Client.STEER_VEHICLE,
@ -297,42 +292,54 @@ public class PacketTypeTest {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void ensureTypesAreCorrect() throws Exception { public void ensureTypesAreCorrect() throws Exception {
boolean fail = false; PacketType.onDynamicCreate = className -> {
throw new RuntimeException("Dynamically generated packet " + className);
};
EnumProtocol[] protocols = EnumProtocol.values(); try {
for (EnumProtocol protocol : protocols) { boolean fail = false;
Field field = EnumProtocol.class.getDeclaredField("k");
field.setAccessible(true);
Map<EnumProtocolDirection, Object> map = (Map<EnumProtocolDirection, Object>) field.get(protocol); EnumProtocol[] protocols = EnumProtocol.values();
for (Entry<EnumProtocolDirection, Object> entry : map.entrySet()) { for (EnumProtocol protocol : protocols) {
Field mapField = entry.getValue().getClass().getDeclaredField("b"); Field field = EnumProtocol.class.getDeclaredField("k");
mapField.setAccessible(true); field.setAccessible(true);
Map<Class<?>, Integer> reverseMap = (Map<Class<?>, Integer>) mapField.get(entry.getValue()); Map<EnumProtocolDirection, Object> map = (Map<EnumProtocolDirection, Object>) field.get(protocol);
for (Entry<EnumProtocolDirection, Object> entry : map.entrySet()) {
Field mapField = entry.getValue().getClass().getDeclaredField("b");
mapField.setAccessible(true);
Map<Integer, Class<?>> treeMap = new TreeMap<>(); Map<Class<?>, Integer> reverseMap = (Map<Class<?>, Integer>) mapField.get(entry.getValue());
for (Entry<Class<?>, Integer> entry1 : reverseMap.entrySet()) {
treeMap.put(entry1.getValue(), entry1.getKey());
}
for (Entry<Integer, Class<?>> entry1 : treeMap.entrySet()) { Map<Integer, Class<?>> treeMap = new TreeMap<>();
try { for (Entry<Class<?>, Integer> entry1 : reverseMap.entrySet()) {
PacketType type = PacketType.fromClass(entry1.getValue()); treeMap.put(entry1.getValue(), entry1.getKey());
if (type.getCurrentId() != entry1.getKey()) { }
throw new IllegalStateException(
"Packet ID for " + type + " is incorrect. Expected " + entry1.getKey() + ", but got " for (Entry<Integer, Class<?>> entry1 : treeMap.entrySet()) {
+ type.getCurrentId()); try {
PacketType type = PacketType.fromClass(entry1.getValue());
if (type.getCurrentId() != entry1.getKey()) {
throw new IllegalStateException(
"Packet ID for " + type + " is incorrect. Expected " + entry1.getKey() + ", but got "
+ type.getCurrentId());
}
} catch (Throwable ex) {
if (ex.getMessage().contains("BundleDelimiterPacket")) {
continue;
}
ex.printStackTrace();
fail = true;
} }
} catch (Throwable ex) {
ex.printStackTrace();
fail = true;
} }
} }
} }
}
assertFalse(fail, "Packet type(s) were incorrect!"); assertFalse(fail, "Packet type(s) were incorrect!");
} finally {
PacketType.onDynamicCreate = __ -> { };
}
} }
@Test @Test