Skip to content

Commit

Permalink
fix: WrappedRegistrable returning incorrect key (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia authored Jun 4, 2024
1 parent fe51dea commit 11f4810
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,9 @@ private static final class Factory {
@NotNull
private final WrappedRegistry registry;

@NotNull
private final FieldAccessor fieldAccessor;

private Factory(@NotNull final Class<?> registrableClass) {
this.registrableClass = registrableClass;
this.registry = WrappedRegistry.getRegistry(registrableClass);
this.fieldAccessor = Accessors.getFieldAccessor(
FuzzyReflection.fromClass(registrableClass, true)
.getField(
FuzzyFieldContract.newBuilder()
.typeExact(registrableClass)
.build()
)
);
}

@NotNull
Expand All @@ -174,7 +163,7 @@ public static Factory getOrCreate(

@NotNull
public MinecraftKey getKey(@NotNull final Object handle) {
return registry.getKey(fieldAccessor.get(handle));
return registry.getKey(handle);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.comphenix.protocol.wrappers;

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.entity.EntityType;
import org.bukkit.potion.PotionEffectType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class WrappedRegistrableTest {

@BeforeAll
static void initialize() {
BukkitInitialization.initializeAll();
}

@Test
void testRegistrables() {
// some randomly selected registrables which we can prove that work using the bukkit api
validate(MinecraftReflection.getEntityTypes(), EntityType.WARDEN.getKey());
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) {
MinecraftKey minecraftKey = new MinecraftKey(key.getNamespace(), key.getKey());
WrappedRegistrable registrable = WrappedRegistrable.fromClassAndKey(registryType, minecraftKey);
assertNotNull(registrable);

Object registrableHandle = registrable.getHandle();
assertNotNull(registrableHandle);
assertInstanceOf(registryType, registrableHandle);

MinecraftKey registrableKey = registrable.getKey();
assertEquals(key.getNamespace(), registrableKey.getPrefix());
assertEquals(key.getKey(), registrableKey.getKey());
}
}

0 comments on commit 11f4810

Please sign in to comment.