mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-27 05:05:24 +01:00
allow easier access to registries (#1613)
This commit is contained in:
parent
84a0b5ffdd
commit
374e6cd5ee
@ -19,6 +19,7 @@ public class WrappedRegistry {
|
||||
private static final Map<Class<?>, 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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user