diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedRegistry.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedRegistry.java index b5b5276b..6b3f4d47 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedRegistry.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedRegistry.java @@ -19,6 +19,7 @@ public class WrappedRegistry { private static final Map, WrappedRegistry> REGISTRY; private static final MethodAccessor GET; + private static final MethodAccessor GET_ID; private static final MethodAccessor GET_KEY; static { @@ -29,7 +30,7 @@ public class WrappedRegistry { for (Field field : regClass.getFields()) { try { // make sure it's actually a registry - if (field.getType().isAssignableFrom(regClass)) { + if (regClass.isAssignableFrom(field.getType())) { Type genType = field.getGenericType(); if (genType instanceof ParameterizedType) { ParameterizedType par = (ParameterizedType) genType; @@ -48,12 +49,19 @@ public class WrappedRegistry { FuzzyReflection fuzzy = FuzzyReflection.fromClass(regClass, false); GET = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract + .newBuilder() + .parameterCount(1) + .returnDerivedOf(Object.class) + .requireModifier(Modifier.ABSTRACT) + .parameterExactType(MinecraftReflection.getMinecraftKeyClass()) + .build())); + GET_ID = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract .newBuilder() - .parameterCount(1) - .returnDerivedOf(Object.class) - .requireModifier(Modifier.ABSTRACT) - .parameterExactType(MinecraftReflection.getMinecraftKeyClass()) - .build())); + .parameterCount(1) + .returnTypeExact(int.class) + .requireModifier(Modifier.ABSTRACT) + .parameterDerivedOf(Object.class) + .build())); GET_KEY = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract .newBuilder() .parameterCount(1) @@ -79,13 +87,27 @@ public class WrappedRegistry { return MinecraftKey.getConverter().getSpecific(GET_KEY.invoke(handle, generic)); } - // TODO add more methods + public int getId(MinecraftKey key) { + return getId(get(key)); + } + + public int getId(String key) { + return getId(new MinecraftKey(key)); + } + + public int getId(Object entry) { + return (int) GET_ID.invoke(this.handle, entry); + } public static WrappedRegistry getAttributeRegistry() { - return REGISTRY.get(MinecraftReflection.getAttributeBase()); + return getRegistry(MinecraftReflection.getAttributeBase()); } public static WrappedRegistry getDimensionRegistry() { - return REGISTRY.get(MinecraftReflection.getDimensionManager()); + return getRegistry(MinecraftReflection.getDimensionManager()); } + + public static WrappedRegistry getRegistry(Class type) { + return REGISTRY.get(type); + } } diff --git a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 275ab89a..9e75218a 100644 --- a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -21,7 +21,7 @@ import static com.comphenix.protocol.utility.TestUtils.equivalentItem; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; @@ -50,6 +50,7 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; import com.comphenix.protocol.wrappers.WrappedEnumEntityUseAction; import com.comphenix.protocol.wrappers.WrappedGameProfile; +import com.comphenix.protocol.wrappers.WrappedRegistry; import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; @@ -492,10 +493,13 @@ public class PacketContainerTest { PacketContainer packet = creator.createPacket(entityId, mobEffect); assertEquals(entityId, packet.getIntegers().read(0)); - // assertEquals(effect.getType().getId(), packet.getIntegers().read(1)); assertEquals(effect.getAmplifier(), (byte) packet.getBytes().read(0)); assertEquals(effect.getDuration(), packet.getIntegers().read(1)); + WrappedRegistry registry = WrappedRegistry.getRegistry(MinecraftReflection.getMobEffectListClass()); + Object effectList = assertInstanceOf(MobEffectList.class, packet.getStructures().read(0).getHandle()); + assertEquals(effect.getType().getId(), registry.getId(effectList)); + int e = 0; if (effect.isAmbient()) { e |= 1; diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedRegistryTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedRegistryTest.java new file mode 100644 index 00000000..247e45a4 --- /dev/null +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedRegistryTest.java @@ -0,0 +1,51 @@ +package com.comphenix.protocol.wrappers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import com.comphenix.protocol.BukkitInitialization; +import com.comphenix.protocol.utility.MinecraftReflection; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Sound; +import org.bukkit.attribute.Attribute; +import org.bukkit.potion.PotionEffectType; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class WrappedRegistryTest { + + @BeforeAll + static void initialize() { + BukkitInitialization.initializeAll(); + } + + @Test + void testRegistries() { + // some randomly selected registries which we can proof to work using the bukkit api + validate(MinecraftReflection.getItemClass(), Material.DIAMOND_AXE.getKey()); + validate(MinecraftReflection.getAttributeBase(), Attribute.GENERIC_MAX_HEALTH.getKey()); + validate(MinecraftReflection.getSoundEffectClass(), Sound.ENTITY_WARDEN_SNIFF.getKey()); + validate(MinecraftReflection.getMobEffectListClass(), PotionEffectType.REGENERATION.getKey()); + } + + void validate(Class registryType, NamespacedKey key) { + WrappedRegistry registry = WrappedRegistry.getRegistry(registryType); + assertNotNull(registry); + + Object registryEntry = registry.get(key.getKey()); + assertNotNull(registryEntry); + assertInstanceOf(registryType, registryEntry); + + MinecraftKey entryKey = registry.getKey(registryEntry); + assertEquals(key.getNamespace(), entryKey.getPrefix()); + assertEquals(key.getKey(), entryKey.getKey()); + + int soundId = registry.getId(registryEntry); + assertNotEquals(-1, soundId); + assertEquals(soundId, registry.getId(entryKey)); + } +} +