mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-23 13:39:43 +00:00
Allow using DeferredRegisters for vanilla registries (#8527)
Catch and aggregate exceptions when applying object holders
This commit is contained in:
1 parent
8f0d3da1e4
commit
1f031ad2c3
13 files changed
+550
-142
No files matched your search
@@ -8,6 +8,7 @@ package net.minecraftforge.registries;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.common.collect.SetMultimap;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.data.BuiltinRegistries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
@@ -48,12 +49,20 @@ import java.util.function.Supplier;
|
||||
* }
|
||||
*}</pre>
|
||||
*
|
||||
* @param <T> The base registry type, must be a concrete base class, do not use subclasses or wild cards.
|
||||
* @param <T> The base registry type
|
||||
*/
|
||||
public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
public class DeferredRegister<T>
|
||||
{
|
||||
/**
|
||||
* Use for vanilla/forge registries. See example above.
|
||||
* DeferredRegister factory for forge registries that exist <i>before</i> this DeferredRegister is created.
|
||||
* <p>
|
||||
* If you have a supplier, <u>do not use this method.</u>
|
||||
* Instead, use one of the other factories that takes in a registry key or registry name.
|
||||
*
|
||||
* @param reg the forge registry to wrap
|
||||
* @param modid the namespace for all objects registered to this DeferredRegister
|
||||
* @see #create(ResourceKey, String)
|
||||
* @see #create(ResourceLocation, String)
|
||||
*/
|
||||
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(IForgeRegistry<B> reg, String modid)
|
||||
{
|
||||
@@ -68,56 +77,111 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
@Deprecated(forRemoval = true, since = "1.18.2")
|
||||
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(Class<B> base, String modid)
|
||||
{
|
||||
return new DeferredRegister<>(null, base, modid);
|
||||
return new DeferredRegister<>(null, base, modid, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* DeferredRegister factory for custom registries that are made during the {@link NewRegistryEvent} event
|
||||
* or with {@link #makeRegistry(Class, Supplier)}.
|
||||
* DeferredRegister factory for custom forge registries, {@link Registry vanilla registries},
|
||||
* or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry key.
|
||||
* Supports both registries that already exist or do not exist yet.
|
||||
* <p>
|
||||
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will throw an exception.
|
||||
* To allow the optional existence of a registry without error, use {@link #createOptional(ResourceKey, String)}.
|
||||
*
|
||||
* @param key The key of the registry to reference
|
||||
* @param modid The namespace for all objects registered to this DeferredRegister
|
||||
* @param key the key of the registry to reference
|
||||
* @param modid the namespace for all objects registered to this DeferredRegister
|
||||
* @see #createOptional(ResourceKey, String)
|
||||
* @see #create(IForgeRegistry, String)
|
||||
* @see #create(ResourceLocation, String)
|
||||
*/
|
||||
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(ResourceKey<? extends Registry<B>> key, String modid)
|
||||
public static <B> DeferredRegister<B> create(ResourceKey<? extends Registry<B>> key, String modid)
|
||||
{
|
||||
return new DeferredRegister<>(key.location(), null, modid);
|
||||
return new DeferredRegister<>(key, null, modid, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* DeferredRegister factory for custom registries that are made during the {@link NewRegistryEvent} event
|
||||
* or with {@link #makeRegistry(Class, Supplier)}.
|
||||
* DeferredRegister factory for the optional existence of custom forge registries, {@link Registry vanilla registries},
|
||||
* or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry key.
|
||||
* Supports both registries that already exist or do not exist yet.
|
||||
* <p>
|
||||
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will never be filled but will not throw an exception.
|
||||
*
|
||||
* @param key the key of the registry to reference
|
||||
* @param modid the namespace for all objects registered to this DeferredRegister
|
||||
* @see #create(ResourceKey, String)
|
||||
* @see #create(IForgeRegistry, String)
|
||||
* @see #create(ResourceLocation, String)
|
||||
*/
|
||||
public static <B> DeferredRegister<B> createOptional(ResourceKey<? extends Registry<B>> key, String modid)
|
||||
{
|
||||
return new DeferredRegister<>(key, null, modid, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* DeferredRegister factory for custom forge registries, {@link Registry vanilla registries},
|
||||
* or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry name.
|
||||
* Supports both registries that already exist or do not exist yet.
|
||||
* <p>
|
||||
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will throw an exception.
|
||||
* To allow the optional existence of a registry without error, use {@link #createOptional(ResourceLocation, String)}.
|
||||
*
|
||||
* @param registryName The name of the registry, should include namespace. May come from another DeferredRegister through {@link #getRegistryName()}.
|
||||
* @param modid The namespace for all objects registered to this DeferredRegister
|
||||
* @see #createOptional(ResourceLocation, String)
|
||||
* @see #create(IForgeRegistry, String)
|
||||
* @see #create(ResourceKey, String)
|
||||
*/
|
||||
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(ResourceLocation registryName, String modid)
|
||||
public static <B> DeferredRegister<B> create(ResourceLocation registryName, String modid)
|
||||
{
|
||||
return new DeferredRegister<>(registryName, null, modid);
|
||||
return new DeferredRegister<>(ResourceKey.createRegistryKey(registryName), null, modid, false);
|
||||
}
|
||||
|
||||
private final ResourceLocation registryName;
|
||||
private final Class<T> superType;
|
||||
/**
|
||||
* DeferredRegister factory for the optional existence of custom forge registries, {@link Registry vanilla registries},
|
||||
* or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry name.
|
||||
* <p>
|
||||
* The registry may not exist at the time this DeferredRegister is created.
|
||||
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will never be filled but will not throw an exception.
|
||||
*
|
||||
* @param registryName The name of the registry, should include namespace. May come from another DeferredRegister through {@link #getRegistryName()}.
|
||||
* @param modid The namespace for all objects registered to this DeferredRegister
|
||||
* @see #create(ResourceLocation, String)
|
||||
* @see #create(IForgeRegistry, String)
|
||||
* @see #create(ResourceKey, String)
|
||||
*/
|
||||
public static <B> DeferredRegister<B> createOptional(ResourceLocation registryName, String modid)
|
||||
{
|
||||
return new DeferredRegister<>(ResourceKey.createRegistryKey(registryName), null, modid, true);
|
||||
}
|
||||
|
||||
@Nullable // Nullable when superType is not null
|
||||
private final ResourceKey<? extends Registry<T>> registryKey;
|
||||
@Nullable // Nullable when registryKey is not null
|
||||
private final Class<? extends IForgeRegistryEntry<?>> superType;
|
||||
private final String modid;
|
||||
private final boolean optionalRegistry;
|
||||
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<IForgeRegistry<T>> typeSupplier;
|
||||
private Supplier<RegistryBuilder<T>> registryFactory;
|
||||
@Nullable
|
||||
private Supplier<RegistryBuilder<?>> registryFactory;
|
||||
@Nullable
|
||||
private SetMultimap<TagKey<T>, Supplier<T>> optionalTags;
|
||||
private boolean seenRegisterEvent = false;
|
||||
|
||||
private DeferredRegister(@Nullable ResourceLocation registryName, @Nullable Class<T> base, String modid)
|
||||
private <E extends IForgeRegistryEntry<E>> DeferredRegister(@Nullable ResourceKey<? extends Registry<T>> registryKey, @Nullable Class<E> base,
|
||||
String modid, boolean optionalRegistry)
|
||||
{
|
||||
this.registryName = registryName;
|
||||
this.registryKey = registryKey;
|
||||
this.superType = base;
|
||||
this.modid = modid;
|
||||
this.optionalRegistry = optionalRegistry;
|
||||
}
|
||||
|
||||
private DeferredRegister(IForgeRegistry<T> reg, String modid)
|
||||
@SuppressWarnings("unchecked")
|
||||
private <E extends IForgeRegistryEntry<E>> DeferredRegister(IForgeRegistry<E> reg, String modid)
|
||||
{
|
||||
this(reg.getRegistryName(), reg.getRegistrySuperType(), modid);
|
||||
this.type = reg;
|
||||
this((ResourceKey<? extends Registry<T>>) (ResourceKey) reg.getRegistryKey(), reg.getRegistrySuperType(), modid, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +191,7 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
* @param sup A factory for the new entry, it should return a new instance every time it is called.
|
||||
* @return A RegistryObject that will be updated with when the entries in the registry change.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public <I extends T> RegistryObject<I> register(final String name, final Supplier<? extends I> sup)
|
||||
{
|
||||
if (seenRegisterEvent)
|
||||
@@ -137,19 +201,23 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
final ResourceLocation key = new ResourceLocation(modid, name);
|
||||
|
||||
RegistryObject<I> ret;
|
||||
IForgeRegistry<T> reg = getForgeRegistry();
|
||||
if (reg != null)
|
||||
ret = RegistryObject.of(key, reg);
|
||||
else if (this.registryName != null)
|
||||
ret = RegistryObject.of(key, this.registryName, this.modid);
|
||||
if (this.registryKey != null)
|
||||
ret = this.optionalRegistry
|
||||
? RegistryObject.createOptional(key, this.registryKey, this.modid)
|
||||
: RegistryObject.create(key, this.registryKey, this.modid);
|
||||
else if (this.superType != null)
|
||||
ret = RegistryObject.of(key, this.superType, this.modid);
|
||||
ret = RegistryObject.of(key, (Class) 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) {
|
||||
Supplier<? extends T> prevValue = entries.putIfAbsent((RegistryObject<T>) ret, () -> {
|
||||
I value = sup.get();
|
||||
if (value instanceof IForgeRegistryEntry<?> regEntry)
|
||||
regEntry.setRegistryName(key);
|
||||
return value;
|
||||
});
|
||||
if (prevValue != null)
|
||||
throw new IllegalArgumentException("Duplicate registration " + name);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -165,10 +233,11 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
*
|
||||
* @deprecated Use {@link #create(ResourceLocation, String)} and {@link #makeRegistry(Class, Supplier)} instead
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Deprecated(forRemoval = true, since = "1.18.2")
|
||||
public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplier<RegistryBuilder<T>> sup)
|
||||
public <E extends IForgeRegistryEntry<E>> Supplier<IForgeRegistry<E>> makeRegistry(final String name, final Supplier<RegistryBuilder<E>> sup)
|
||||
{
|
||||
return makeRegistry(new ResourceLocation(modid, name), this.superType, sup);
|
||||
return makeRegistry(new ResourceLocation(modid, name), (Class<E>) this.superType, sup);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,9 +250,9 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
* @return A supplier of the {@link IForgeRegistry} created by the builder.
|
||||
* Will always return null until after the {@link NewRegistryEvent} event fires.
|
||||
*/
|
||||
public Supplier<IForgeRegistry<T>> makeRegistry(final Class<T> base, final Supplier<RegistryBuilder<T>> sup)
|
||||
public <E extends IForgeRegistryEntry<E>> Supplier<IForgeRegistry<E>> makeRegistry(final Class<E> base, final Supplier<RegistryBuilder<E>> sup)
|
||||
{
|
||||
return makeRegistry(this.registryName, base, sup);
|
||||
return makeRegistry(this.registryKey.location(), base, sup);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,10 +283,10 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
@NotNull
|
||||
public TagKey<T> createTagKey(@NotNull ResourceLocation location)
|
||||
{
|
||||
if (this.registryName == null)
|
||||
if (this.registryKey == null)
|
||||
throw new IllegalStateException("The registry name was not set, cannot create a tag key");
|
||||
Objects.requireNonNull(location);
|
||||
return TagKey.create(ResourceKey.createRegistryKey(this.registryName), location);
|
||||
return TagKey.create(this.registryKey, location);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,7 +358,9 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
public void register(IEventBus bus)
|
||||
{
|
||||
bus.register(new EventDispatcher(this));
|
||||
if (this.type == null && this.registryFactory != null) {
|
||||
if (this.registryKey != null && this.findForgeRegistry() == null)
|
||||
bus.addListener(this::vanillaRegister);
|
||||
if (this.registryFactory != null) {
|
||||
bus.addListener(this::createRegistry);
|
||||
}
|
||||
}
|
||||
@@ -319,61 +390,60 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
@Nullable
|
||||
public ResourceLocation getRegistryName()
|
||||
{
|
||||
return this.registryName;
|
||||
return this.registryKey == null ? null : this.registryKey.location();
|
||||
}
|
||||
|
||||
private Supplier<IForgeRegistry<T>> makeRegistry(final ResourceLocation registryName, final Class<T> superType, final Supplier<RegistryBuilder<T>> sup) {
|
||||
// TODO 1.19: Remove this E method generic in favor of the class generic T when IForgeRegistry and RegistryBuilder no longer require IForgeRegistryEntry
|
||||
private <E extends IForgeRegistryEntry<E>> Supplier<IForgeRegistry<E>> makeRegistry(final ResourceLocation registryName, final Class<E> superType, final Supplier<RegistryBuilder<E>> sup) {
|
||||
if (registryName == null)
|
||||
throw new IllegalStateException("Cannot create a registry without specifying a registry name");
|
||||
if (superType == null)
|
||||
throw new IllegalStateException("Cannot create a registry without specifying a base type");
|
||||
if (this.type != null || this.registryFactory != null)
|
||||
if (RegistryManager.ACTIVE.getRegistry(registryName) != null || this.registryFactory != null)
|
||||
throw new IllegalStateException("Cannot create a registry for a type that already exists");
|
||||
|
||||
this.registryFactory = () -> sup.get().setName(registryName).setType(superType);
|
||||
return this::getForgeRegistry;
|
||||
return new Supplier<>()
|
||||
{
|
||||
private IForgeRegistry<E> registry;
|
||||
|
||||
@Override
|
||||
public IForgeRegistry<E> get()
|
||||
{
|
||||
// Keep trying to capture until it's not null
|
||||
if (registry == null)
|
||||
registry = RegistryManager.ACTIVE.getRegistry(registryName);
|
||||
|
||||
return registry;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private IForgeRegistry<T> getForgeRegistry()
|
||||
{
|
||||
if (this.type == null && this.typeSupplier != null)
|
||||
this.type = this.typeSupplier.get();
|
||||
|
||||
return this.type;
|
||||
}
|
||||
|
||||
private void onFill(IForgeRegistry<T> registry)
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private void onFill(IForgeRegistry<?> registry)
|
||||
{
|
||||
if (this.optionalTags == null)
|
||||
return;
|
||||
|
||||
ITagManager<T> tagManager = registry.tags();
|
||||
// TODO 1.19: Put back the <T> here and fix the unsafe generic operations
|
||||
ITagManager tagManager = registry.tags();
|
||||
if (tagManager == null)
|
||||
throw new IllegalStateException("The forge registry " + registry.getRegistryName() + " does not support tags, but optional tags were registered!");
|
||||
|
||||
Multimaps.asMap(this.optionalTags).forEach(tagManager::addOptionalTagDefaults);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private void addEntries(RegistryEvent.Register<?> event)
|
||||
{
|
||||
IForgeRegistry<T> storedType = this.getForgeRegistry();
|
||||
if (storedType == null && this.registryFactory == null)
|
||||
{
|
||||
//If there is no type yet and we don't have a registry factory, attempt to capture the registry
|
||||
//Note: This will only ever get run on the first registry event, as after that time,
|
||||
// the type will no longer be null. This is needed here rather than during the NewRegistry event
|
||||
// to ensure that mods can properly use deferred registers for custom registries added by other mods
|
||||
captureRegistry();
|
||||
storedType = this.type;
|
||||
}
|
||||
IForgeRegistry<?> storedType = findForgeRegistry();
|
||||
if (storedType != null && event.getGenericType() == storedType.getRegistrySuperType())
|
||||
{
|
||||
this.seenRegisterEvent = true;
|
||||
@SuppressWarnings("unchecked")
|
||||
IForgeRegistry<T> reg = (IForgeRegistry<T>)event.getRegistry();
|
||||
IForgeRegistry<? extends T> reg = (IForgeRegistry<? extends T>) event.getRegistry();
|
||||
for (Entry<RegistryObject<T>, Supplier<? extends T>> e : entries.entrySet())
|
||||
{
|
||||
reg.register(e.getValue().get());
|
||||
((IForgeRegistry) reg).register((IForgeRegistryEntry) e.getValue().get());
|
||||
e.getKey().updateReference(reg);
|
||||
}
|
||||
}
|
||||
@@ -381,24 +451,36 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||
|
||||
private void createRegistry(NewRegistryEvent event)
|
||||
{
|
||||
this.typeSupplier = event.create(this.registryFactory.get(), this::onFill);
|
||||
event.create(this.registryFactory.get(), this::onFill);
|
||||
}
|
||||
|
||||
private void captureRegistry()
|
||||
@SuppressWarnings({ "unchecked", "removal" })
|
||||
private void vanillaRegister(VanillaRegisterEvent event)
|
||||
{
|
||||
if (this.registryName != null)
|
||||
if (this.registryKey != null && event.vanillaRegistry.key() == this.registryKey)
|
||||
{
|
||||
this.type = RegistryManager.ACTIVE.getRegistry(this.registryName);
|
||||
if (this.type == null)
|
||||
throw new IllegalStateException("Unable to find registry with key " + this.registryName + " for modid \"" + modid + "\" after NewRegistry event");
|
||||
this.seenRegisterEvent = true;
|
||||
for (Entry<RegistryObject<T>, Supplier<? extends T>> e : entries.entrySet())
|
||||
{
|
||||
Registry.register((Registry<T>) event.vanillaRegistry, e.getKey().getId(), e.getValue().get());
|
||||
e.getKey().updateReference((Registry<T>) event.vanillaRegistry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Nullable
|
||||
private IForgeRegistry<?> findForgeRegistry()
|
||||
{
|
||||
if (this.registryKey != null)
|
||||
{
|
||||
return RegistryManager.ACTIVE.getRegistry((ResourceKey) this.registryKey);
|
||||
}
|
||||
else 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");
|
||||
return RegistryManager.ACTIVE.getRegistry((Class) this.superType);
|
||||
}
|
||||
else
|
||||
throw new IllegalStateException("Unable to find registry for mod \"" + modid + "\" No lookup criteria specified.");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user