Changes

89 bytes added ,  18:26, 3 April 2022
Some cleanup based on #8527 vanilla Deferred Registers
Line 177: Line 177:  
<code>RegistryObject</code>s can be used to retrieve references to registered objects once they become available. Their references are updated along with all <code>@ObjectHolder</code> annotations after the associated <code>RegistryEvent$Register</code> has been dispatched and frozen.
 
<code>RegistryObject</code>s can be used to retrieve references to registered objects once they become available. Their references are updated along with all <code>@ObjectHolder</code> annotations after the associated <code>RegistryEvent$Register</code> has been dispatched and frozen.
   −
A <code>RegistryObject</code> can be retrieved as a result of using <code>DeferredRegister</code> or calling the static constructor <code>RegistryObject::create</code>. Each static constructor takes in the "registry name" of the object being referenced and either a <code>IForgeRegistry</code> or, if custom registries are used, a supplier of the object class implementing <code>IForgeRegistryEntry</code>. The <code>RegistryObject</code> can be stored within some field and retrieve the registered object using <code>#get</code>.
+
A <code>RegistryObject</code> can be retrieved as a result of using <code>DeferredRegister</code> or calling the static factory <code>RegistryObject#create</code>. Each static factory takes in the "registry name" of the object being referenced and one of the following: a <code>IForgeRegistry</code>, a registry name of the type <code>ResourceLocation</code>, or a registry key of the type <code>ResourceKey<? extends Registry<?>></code>. The <code>RegistryObject</code> can be stored within some field and retrieve the registered object using <code>#get</code>.
    
An example using <code>RegistryObject</code>:
 
An example using <code>RegistryObject</code>:
 
{{Template:Tabs/Code_Snippets
 
{{Template:Tabs/Code_Snippets
|java=public static final RegistryObject<Item> EXAMPLE_ITEM = RegistryObject.create(new ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS);
+
|java=public static final RegistryObject<Item> EXAMPLE_ITEM = RegistryObject.create(new ResourceLocation("examplemod", "example_item"), ForgeRegistries.ITEMS);
    
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
 
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
 
public static final RegistryObject<ExampleRegistry> EXAMPLE_OBJECT = RegistryObject.create(new ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod");  
 
public static final RegistryObject<ExampleRegistry> EXAMPLE_OBJECT = RegistryObject.create(new ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod");  
|kotlin=val EXAMPLE_ITEM: RegistryObject<Item> = RegistryObject.create(ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
+
|kotlin=val EXAMPLE_ITEM: RegistryObject<Item> = RegistryObject.create(ResourceLocation("examplemod", "example_item"), ForgeRegistries.ITEMS)
    
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
 
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
 
val EXAMPLE_OBJECT: RegistryObject<ExampleRegistry> = RegistryObject.create(ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod")
 
val EXAMPLE_OBJECT: RegistryObject<ExampleRegistry> = RegistryObject.create(ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod")
|scala=final val EXAMPLE_ITEM = RegistryObject.create(new ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
+
|scala=final val EXAMPLE_ITEM = RegistryObject.create(new ResourceLocation("examplemod", "example_item"), ForgeRegistries.ITEMS)
    
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
 
// Assume that 'examplemod:example_registry' is a valid registry, and 'examplemod:example_object' is a valid object within that registry
Line 195: Line 195:  
|}}
 
|}}
   −
{{Tip/Important|All vanilla objects are bootstrapped and registered before mods are loaded. As such, they can be referenced as is without any issues, assuming the entries are not replaced.}}
+
{{Tip/Important|All vanilla objects are bootstrapped and registered before mods are loaded. As such, they can be referenced as is without any issues.}}
    
=== Using @ObjectHolder ===
 
=== Using @ObjectHolder ===
Line 317: Line 317:  
=== With DeferredRegister ===
 
=== With DeferredRegister ===
   −
The first method involves the second static constructor: <code>DeferredRegister::create(ResourceLocation, String)</code>. From there, we can construct the registry using <code>#makeRegistry</code>. This will already populate <code>#setName</code> and <code>#setType</code> for us. This method also returns a supplier of the registry which we can use after the <code>RegistryEvent$NewRegistry</code> event.
+
The first method involves the second static constructor: <code>DeferredRegister#create(ResourceLocation, String)</code>. From there, we can construct the registry using <code>#makeRegistry</code>. This will already populate <code>#setName</code> and <code>#setType</code> for us. This method also returns a supplier of the registry which we can use after the <code>NewRegistryEvent</code> is called.
    
Here is an example:
 
Here is an example:
    
{{Template:Tabs/Code_Snippets
 
{{Template:Tabs/Code_Snippets
|java=public static final DeferredRegister<ExampleRegistry> EXAMPLE = DeferredRegister.create("example_registry", MODID);
+
|java=public static final DeferredRegister<ExampleRegistry> EXAMPLE = DeferredRegister.create(new ResourceLocation(MODID, "example_registry"), MODID);
    
public static final Supplier<IForgeRegistry<ExampleRegistry>> REGISTRY = EXAMPLE.makeRegistry(ExampleRegistry.class, RegistryBuilder::new);
 
public static final Supplier<IForgeRegistry<ExampleRegistry>> REGISTRY = EXAMPLE.makeRegistry(ExampleRegistry.class, RegistryBuilder::new);
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create("example_registry", MODID)
+
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create(ResourceLocation(MODID, "example_registry"), MODID)
    
val REGISTRY: IForgeRegistry<ExampleRegistry> by lazy {
 
val REGISTRY: IForgeRegistry<ExampleRegistry> by lazy {
 
     EXAMPLE.makeRegistry(ExampleRegistry::class.java, ::RegistryBuilder).get()
 
     EXAMPLE.makeRegistry(ExampleRegistry::class.java, ::RegistryBuilder).get()
 
}
 
}
|scala=final val EXAMPLE = DeferredRegister.create("example_registry", MODID)
+
|scala=final val EXAMPLE = DeferredRegister.create(new ResourceLocation(MODID, "example_registry"), MODID)
    
final lazy val REGISTRY = EXAMPLE.makeRegistry(classOf[ExampleRegistry], () => new RegistryBuilder).get
 
final lazy val REGISTRY = EXAMPLE.makeRegistry(classOf[ExampleRegistry], () => new RegistryBuilder).get
 
|}}
 
|}}
   −
=== Using RegistryEvent$NewRegistry ===
+
=== Using `NewRegistryEvent` ===
   −
The second method can be done during the <code>RegistryEvent$NewRegistry</code> event. This will call a new instance of the builder directly. From there, the registry can be built and stored via <code>RegistryBuilder#create</code>. This will cause the registry to be registered to the <code>RegistryManager</code> and returned to the caller for additional processing.
+
The second method can be done during the <code>NewRegistryEvent</code> event. Using <code>NewRegistryEvent#create</code>, you can pass in a <code>RegistryBuilder</code> directly. This method will return a <code>Supplier<IForgeRegistry<V>></code> that can be stored and queried after the event is fired to gain access to your <code>IForgeRegistry</code> instance.
    
Here is an example: (the event handler is registered on the '''mod event bus''')
 
Here is an example: (the event handler is registered on the '''mod event bus''')
    
<syntaxhighlight lang="Java">
 
<syntaxhighlight lang="Java">
public static IForgeRegistry<ExampleRegistry> registry = null;
+
public static Supplier<IForgeRegistry<ExampleRegistry>> registrySupplier = null;
    
@SubscribeEvent
 
@SubscribeEvent
public void onNewRegistry(RegistryEvent.NewRegistry event){
+
public void onNewRegistry(NewRegistryEvent event){
 
     RegistryBuilder<ExampleRegistry> registryBuilder = new RegistryBuilder<>();
 
     RegistryBuilder<ExampleRegistry> registryBuilder = new RegistryBuilder<>();
 
     registryBuilder.setName(new ResourceLocation(MODID, "example_registry");
 
     registryBuilder.setName(new ResourceLocation(MODID, "example_registry");
 
     registryBuilder.setType(ExampleRegistry.class);
 
     registryBuilder.setType(ExampleRegistry.class);
     registry = registryBuilder.create();
+
     registrySupplier = event.create(registryBuilder);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>