Class DeferredRegister<T>
- Type Parameters:
T
- the base registry type
- Direct Known Subclasses:
DeferredRegister.Blocks
,DeferredRegister.Items
This class maintains a list of all suppliers for entries and registers them during the proper RegisterEvent
event, after being registered to an event bus.
Suppliers should return new instances every time they are invoked.
To create an instance of this helper class, use any of the three factory methods: create(Registry, String)
,
create(ResourceKey, String)
, or create(ResourceLocation, String)
. There are also specialized
subclasses of this helper for Block
s and Item
s, created through createBlocks(String)
and
createItems(String)
respectively. (Be sure to store the concrete type of those subclasses, rather than
storing them generically as DeferredRegister<Block>
or DeferredRegister<Item>
.)
Here are some common examples for using this class:
private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(MODID);
private static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(MODID);
private static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(BuiltInRegistries.BLOCK_ENTITY_TYPE, MODID);
// If you don't care about the actual Block class, use the simple variants
public static final DeferredBlock<Block> ROCK_BLOCK = BLOCKS.registerSimpleBlock("rock", Block.Properties.create(Material.ROCK));
public static final DeferredItem<BlockItem> ROCK_ITEM = ITEMS.registerSimpleBlockItem(ROCK_BLOCK, new Item.Properties());
// Otherwise, use the regular (non-'simple') variants
public static final DeferredBlock<SpecialRockBlock> SPECIAL_ROCK_BLOCK = BLOCKS.registerBlock("special_rock",
SpecialRockBlock::new, Block.Properties.create(Material.ROCK));
// (#registerSimpleBlockItem does not have a non-'simple' variant -- register an item in the usual way)
public static final DeferredItem<SpecialRockItem> SPECIAL_ROCK_ITEM = ITEMS.register("special_rock",
() -> new SpecialRockItem(SPECIAL_ROCK_BLOCK.get(), new Item.Properties()))
// (Can be DeferredHolder<BlockEntityType<?>, BlockEntityType<RockBlockEntity>> if you prefer)
public static final Supplier<BlockEntityType<RockBlockEntity>> ROCK_BLOCK_ENTITY = BLOCK_ENTITIES.register("rock",
() -> BlockEntityType.Builder.of(RockBlockEntity::new, ROCK_BLOCK.get()).build(null));
public ExampleMod(IEventBus modBus) {
ITEMS.register(modBus);
BLOCKS.register(modBus);
BLOCK_ENTITIES.register(modBus);
}
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Specialized DeferredRegister forBlocks
that uses the specializedDeferredBlock
as the return type forDeferredRegister.Blocks.register(java.lang.String, java.util.function.Function<net.minecraft.resources.ResourceLocation, ? extends B>)
.static class
Specialized DeferredRegister forItems
that uses the specializedDeferredItem
as the return type forDeferredRegister.Items.register(java.lang.String, java.util.function.Function<net.minecraft.resources.ResourceLocation, ? extends I>)
.private static class
-
Field Summary
Modifier and TypeFieldDescriptionprivate final Map<ResourceLocation,
ResourceLocation> private final Map<DeferredHolder<T,
?>, Supplier<? extends T>> private final Set<DeferredHolder<T,
?>> private final String
private boolean
private @Nullable DeferredRegister.RegistryHolder<T>
private final ResourceKey<? extends Registry<T>>
private boolean
private boolean
-
Constructor Summary
ModifierConstructorDescriptionprotected
DeferredRegister
(ResourceKey<? extends Registry<T>> registryKey, String namespace) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAlias
(ResourceLocation from, ResourceLocation to) Adds an alias that maps from the name specified byfrom
to the name specified byto
.private void
addEntries
(RegisterEvent event) private void
addRegistry
(NewRegistryEvent event) static <T> DeferredRegister<T>
DeferredRegister factory for modded registries or vanilla registries.static <T> DeferredRegister<T>
create
(ResourceKey<? extends Registry<T>> key, String namespace) DeferredRegister factory for modded registries or vanilla registries to lookup based on the provided registry key.static <B> DeferredRegister<B>
create
(ResourceLocation registryName, String modid) DeferredRegister factory for custom forge registries orvanilla registries
to lookup based on the provided registry name.static DeferredRegister.Blocks
createBlocks
(String modid) Factory for a specialized DeferredRegister forBlocks
.protected <I extends T>
DeferredHolder<T,I> createHolder
(ResourceKey<? extends Registry<T>> registryKey, ResourceLocation key) Create aDeferredHolder
or an inheriting type to be stored.static DeferredRegister.Items
createItems
(String modid) Factory for a specializedDeferredRegister
forItems
.createTagKey
(String path) Creates a tag key based on the current namespace and provided path as the location and the registry name linked to this DeferredRegister.createTagKey
(ResourceLocation location) Creates a tag key based on the provided resource location and the registry name linked to this DeferredRegister.Collection<DeferredHolder<T,
? extends T>> Returns the modid/namespace associated with this deferred register.Returns a supplier for theRegistry
linked to this deferred register.ResourceKey<? extends Registry<T>>
makeRegistry
(Consumer<RegistryBuilder<T>> consumer) This method is used to configure a custom modded registry.makeRegistry
(ResourceLocation registryName, Consumer<RegistryBuilder<T>> consumer) <I extends T>
DeferredHolder<T,I> register
(String name, Function<ResourceLocation, ? extends I> func) Adds a new entry to the list of entries to be registered and returns aDeferredHolder
that will be populated with the created entry automatically.<I extends T>
DeferredHolder<T,I> Adds a new entry to the list of entries to be registered and returns aDeferredHolder
that will be populated with the created entry automatically.void
register
(net.neoforged.bus.api.IEventBus bus) Adds our event handler to the specified event bus, this MUST be called in order for this class to function.
-
Field Details
-
registryKey
-
namespace
-
entries
-
entriesView
-
aliases
-
customRegistry
-
registryHolder
-
seenRegisterEvent
private boolean seenRegisterEvent -
seenNewRegistryEvent
private boolean seenNewRegistryEvent -
registeredEventBus
private boolean registeredEventBus
-
-
Constructor Details
-
DeferredRegister
-
-
Method Details
-
create
DeferredRegister factory for modded registries or vanilla registries.If the registry is never created, any
DeferredHolder
s made from this DeferredRegister will throw an exception.- Parameters:
registry
- the registry to register tonamespace
- the namespace for all objects registered to this DeferredRegister- See Also:
-
create
public static <T> DeferredRegister<T> create(ResourceKey<? extends Registry<T>> key, String namespace) DeferredRegister factory for modded registries or vanilla registries to lookup based on the provided registry key. Supports both registries that already exist or do not exist yet.If the registry is never created, any
DeferredHolder
s made from this DeferredRegister will throw an exception.- Parameters:
key
- the key of the registry to reference. May come from another DeferredRegister throughgetRegistryKey()
.namespace
- the namespace for all objects registered to this DeferredRegister- See Also:
-
create
DeferredRegister factory for custom forge registries orvanilla registries
to lookup based on the provided registry name. Supports both registries that already exist or do not exist yet.If the registry is never created, any
DeferredHolder
s made from this DeferredRegister will throw an exception.- Parameters:
registryName
- The name of the registry, should include namespace. May come from another DeferredRegister throughgetRegistryName()
.modid
- The namespace for all objects registered to this DeferredRegister- See Also:
-
createItems
Factory for a specializedDeferredRegister
forItems
.- Parameters:
modid
- The namespace for all objects registered to thisDeferredRegister
- See Also:
-
createBlocks
Factory for a specialized DeferredRegister forBlocks
.- Parameters:
modid
- The namespace for all objects registered to this DeferredRegister- See Also:
-
register
Adds a new entry to the list of entries to be registered and returns aDeferredHolder
that will be populated with the created entry automatically.- Parameters:
name
- The new entry's name. It will automatically have the namespace prefixed.sup
- A factory for the new entry. The factory should not cache the created entry.- Returns:
- A
DeferredHolder
that will track updates from the registry for this entry.
-
register
public <I extends T> DeferredHolder<T,I> register(String name, Function<ResourceLocation, ? extends I> func) Adds a new entry to the list of entries to be registered and returns aDeferredHolder
that will be populated with the created entry automatically.- Parameters:
name
- The new entry's name. It will automatically have the namespace prefixed.func
- A factory for the new entry. The factory should not cache the created entry.- Returns:
- A
DeferredHolder
that will track updates from the registry for this entry.
-
createHolder
protected <I extends T> DeferredHolder<T,I> createHolder(ResourceKey<? extends Registry<T>> registryKey, ResourceLocation key) Create aDeferredHolder
or an inheriting type to be stored.- Type Parameters:
I
- The specific type of the entry.- Parameters:
registryKey
- The key of the registry.key
- The resource location of the entry.- Returns:
- The new instance of
DeferredHolder
or an inheriting type.
-
makeRegistry
This method is used to configure a custom modded registry. It can only be invoked by a single DeferredRegister instance for a given registry key.- Parameters:
consumer
- A consumer that configures the provided RegistryBuilder duringNewRegistryEvent
- Returns:
- The
Registry
linked togetRegistryKey()
.
-
getRegistry
Returns a supplier for theRegistry
linked to this deferred register. For vanilla registries, this will always return a non-null registry. For modded registries, a non-null registry will only be returned afterNewRegistryEvent
fires, or ifmakeRegistry(Consumer)
is called on this same DeferredRegister instance.To register additional DeferredRegisters for custom modded registries, use
create(ResourceKey, String)
which can take a registry key fromgetRegistryKey()
. -
createTagKey
Creates a tag key based on the current namespace and provided path as the location and the registry name linked to this DeferredRegister. To control the namespace, usecreateTagKey(ResourceLocation)
.- See Also:
-
createTagKey
Creates a tag key based on the provided resource location and the registry name linked to this DeferredRegister. To use the current namespace as the tag key namespace automatically, usecreateTagKey(String)
.- See Also:
-
addAlias
Adds an alias that maps from the name specified byfrom
to the name specified byto
.Any registry lookups that target the first name will resolve as the second name, if the first name is not present.
- Parameters:
from
- The source registry name to alias from.to
- The target registry name to alias to.
-
register
public void register(net.neoforged.bus.api.IEventBus bus) Adds our event handler to the specified event bus, this MUST be called in order for this class to function. Seethe example usage
.- Parameters:
bus
- The Mod Specific event bus.
-
getEntries
- Returns:
- The unmodifiable view of registered entries. Useful for bulk operations on all values.
-
getRegistryKey
- Returns:
- The registry key stored in this deferred register. Useful for creating new deferred registers based on an existing one.
-
getRegistryName
- Returns:
- The registry name stored in this deferred register. Useful for creating new deferred registers based on an existing one.
-
getNamespace
Returns the modid/namespace associated with this deferred register.- Returns:
- the modid/namespace associated with this deferred register
-
makeRegistry
private Registry<T> makeRegistry(ResourceLocation registryName, Consumer<RegistryBuilder<T>> consumer) -
addEntries
-
addRegistry
-