Add custom registry type support to DeferredRegister. (#6742)

It's still a hard dependency because it requires a class reference.
But there is not a sane way to test registry types without a class reference.
This commit is contained in:
LexManos authored and GitHub committed 2020-06-12 15:28:43 -07:00
1 parent 5dbea9ea8d
commit fd9bf64d53
8 files changed
+244 -25

No files matched your search

@@ -50,6 +50,10 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
return new RegistryObject<>(name, registry);
}
public static <T extends IForgeRegistryEntry<T>, U extends T> RegistryObject<U> of(final ResourceLocation name, final Class<T> baseType, String modid) {
return new RegistryObject<>(name, baseType, modid);
}
private static RegistryObject<?> EMPTY = new RegistryObject<>();
private static <T extends IForgeRegistryEntry<? super T>> RegistryObject<T> empty() {
@@ -80,6 +84,30 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
});
}
@SuppressWarnings("unchecked")
private <V extends IForgeRegistryEntry<V>> RegistryObject(final ResourceLocation name, final Class<V> baseType, final String modid)
{
this.name = name;
final Throwable callerStack = new Throwable("Calling Site from mod: " + modid);
ObjectHolderRegistry.addHandler(new Consumer<Predicate<ResourceLocation>>()
{
private IForgeRegistry<V> registry;
@Override
public void accept(Predicate<ResourceLocation> pred)
{
if (registry == null)
{
this.registry = RegistryManager.ACTIVE.getRegistry(baseType);
if (registry == null)
throw new IllegalStateException("Unable to find registry for type " + baseType.getName() + " for mod \"" + modid + "\". Check the 'caused by' to see futher stack.", callerStack);
}
if (pred.test(registry.getRegistryName()))
RegistryObject.this.value = registry.containsKey(RegistryObject.this.name) ? (T)registry.getValue(RegistryObject.this.name) : null;
}
});
}
/**
* Directly retrieves the wrapped Registry Object. This value will automatically be updated when the backing registry is updated.
* Will throw NPE if the value is null, use isPresent to check first. Or use any of the other guarded functions.