Fix race condition with DeferredRegister for custom registries (#6951)

This commit is contained in:
Richard Freimer authored and GitHub committed 2020-07-10 14:08:22 -07:00
1 parent 6b3f2f46fa
commit 065ebde57e
1 file changed
+13 -8
@@ -43,8 +43,8 @@ import com.google.common.reflect.TypeToken;
*
*Example Usage:
*<pre>
* private static final DeferredRegister<Item> ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, MODID);
* private static final DeferredRegister<Block> BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, MODID);
* private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
* private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
*
* public static final RegistryObject<Block> ROCK_BLOCK = BLOCKS.register("rock", () -> new Block(Block.Properties.create(Material.ROCK)));
* public static final RegistryObject<Item> ROCK_ITEM = ITEMS.register("rock", () -> new BlockItem(ROCK_BLOCK.get(), new Item.Properties().group(ItemGroup.MISC)));
@@ -155,11 +155,8 @@ 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);
if (this.type == null && this.registryFactory != null) {
bus.addListener(this::createRegistry);
}
}
@@ -173,6 +170,14 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
private void addEntries(RegistryEvent.Register<?> event)
{
if (this.type == 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();
}
if (this.type != null && event.getGenericType() == this.type.getRegistrySuperType())
{
this.seenRegisterEvent = true;
@@ -191,7 +196,7 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
this.type = this.registryFactory.get().create();
}
private void captureRegistry(RegistryEvent.NewRegistry event)
private void captureRegistry()
{
if (this.superType != null)
{