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

@@ -21,6 +21,7 @@ package net.minecraftforge.registries;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
@@ -33,6 +34,8 @@ import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import com.google.common.reflect.TypeToken;
/**
* Utility class to help with managing registry entries.
* Maintains a list of all suppliers for entries and registers them during the proper Register event.
@@ -56,15 +59,41 @@ import java.util.function.Supplier;
*/
public class DeferredRegister<T extends IForgeRegistryEntry<T>>
{
private final IForgeRegistry<T> type;
/**
* Use for vanilla/forge registries. See example above.
*/
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(IForgeRegistry<B> reg, String modid)
{
return new DeferredRegister<B>(reg, modid);
}
/**
* Use for custom registries that are made during the NewRegistry event.
*/
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(Class<B> base, String modid)
{
return new DeferredRegister<B>(base, modid);
}
private final Class<T> superType;
private final String modid;
private final Map<RegistryObject<T>, Supplier<? extends T>> entries = new LinkedHashMap<>();
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries.keySet());
private IForgeRegistry<T> type;
private Supplier<RegistryBuilder<T>> registryFactory;
private DeferredRegister(Class<T> base, String modid)
{
this.superType = base;
this.modid = modid;
}
@Deprecated // Make private in 1.16, Use create. Constructors are an implementation detail.
public DeferredRegister(IForgeRegistry<T> reg, String modid)
{
this(reg.getRegistrySuperType(), modid);
this.type = reg;
this.modid = modid;
}
/**
@@ -80,13 +109,41 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
Objects.requireNonNull(name);
Objects.requireNonNull(sup);
final ResourceLocation key = new ResourceLocation(modid, name);
RegistryObject<I> ret = RegistryObject.of(key, this.type);
RegistryObject<I> ret;
if (this.type != null)
ret = RegistryObject.of(key, this.type);
else if (this.superType != null)
ret = RegistryObject.of(key, this.superType, this.modid);
else
throw new IllegalStateException("Could not create RegistryObject in DeferredRegister");
if (entries.putIfAbsent((RegistryObject<T>) ret, () -> sup.get().setRegistryName(key)) != null) {
throw new IllegalArgumentException("Duplicate registration " + name);
}
return ret;
}
/**
* For custom registries only, fills the {@link #registryFactory} to be called later see {@link #register(IEventBus)}
*
* Calls {@link RegistryBuilder#setName} and {@link RegistryBuilder#setType} automatically.
*
* @param name Path of the registry's {@link ResourceLocation}
* @param sup Supplier of the RegistryBuilder that is called to fill {@link #type} during the NewRegistry event
* @return A supplier of the {@link IForgeRegistry} created by the builder.
*/
public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplier<RegistryBuilder<T>> sup) {
if (this.superType == null)
throw new IllegalStateException("Cannot create a registry without specifying a base type");
if (this.type != null || this.registryFactory != null)
throw new IllegalStateException("Cannot create a registry for a type that already exists");
this.registryFactory = () -> sup.get().setName(new ResourceLocation(modid, name)).setType(this.superType);
return () -> this.type;
}
/**
* Adds our event handler to the specified event bus, this MUST be called in order for this class to function.
* See the example usage.
@@ -96,6 +153,12 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
public void register(IEventBus bus)
{
bus.addListener(this::addEntries);
if (this.type == null) {
if (this.registryFactory != null)
bus.addListener(this::createRegistry);
else
bus.addListener(EventPriority.LOWEST, this::captureRegistry);
}
}
/**
@@ -108,7 +171,7 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
private void addEntries(RegistryEvent.Register<?> event)
{
if (event.getGenericType() == this.type.getRegistrySuperType())
if (this.type != null && event.getGenericType() == this.type.getRegistrySuperType())
{
@SuppressWarnings("unchecked")
IForgeRegistry<T> reg = (IForgeRegistry<T>)event.getRegistry();
@@ -119,4 +182,21 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
}
}
}
private void createRegistry(RegistryEvent.NewRegistry event)
{
this.type = this.registryFactory.get().create();
}
private void captureRegistry(RegistryEvent.NewRegistry event)
{
if (this.superType != null)
{
this.type = RegistryManager.ACTIVE.getRegistry(this.superType);
if (this.type == null)
throw new IllegalStateException("Unable to find registry for type " + this.superType.getName() + " for modid \"" + modid + "\" after NewRegistry event");
}
else
throw new IllegalStateException("Unable to find registry for mod \"" + modid + "\" No lookup criteria specified.");
}
}