Changes

955 bytes removed ,  23:36, 2 April 2022
Update registry docs to 8527
Line 126: Line 126:  
* All registries within <code>BuiltinRegistries</code> excluding <code>Biome</code>
 
* All registries within <code>BuiltinRegistries</code> excluding <code>Biome</code>
   −
To register objects to any one of these registries, you will need to call <code>Registry::register(Registry, ResourceLocation, T)</code> where the type parameter <code>T</code> is the object instance being registered. The method can then be called and registered during the highest priority of <code>FMLCommonSetupEvent</code>. We can also utilize a <code>Lazy</code> above to store the result on first access and use within our code.
+
To register objects to any one of these registries, create a <code>DeferredRegister</code> via the <code>#create</code> overload which takes in a resource key of the registry and the mod id to register the entries for. Then simply call <code>#register</code> like any other <code>DeferredRegister</code>.
 
  −
Here is an example: (the event handler is registered on the '''mod event bus''')
      
{{Template:Tabs/Code_Snippets
 
{{Template:Tabs/Code_Snippets
|java=public static final Lazy<Holder<ConfiguredFeature<NoneFeatureConfiguration, ?>>> EXAMPLE_CONFIGURED_FEATURE = Lazy.of(() -> FeatureUtils.register("examplemod:example_configured_feature", Feature.NO_OP));
+
|java=private static final DeferredRegister<RecipeType<?>> RECIPE_TYPES = DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, MODID);
   −
@SubscribeEvent(priority = EventPriority.HIGHEST)
+
public static final RegistryObject<RecipeType<ExampleRecipe>> EXAMPLE_RECIPE = RECIPE_TYPES.register("example_recipe", () -> new RecipeType<>() {});
public void register(FMLCommonSetupEvent event) {
+
|kotlin=private val RECIPE_TYPES = DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, MODID)
    event.enqueueWork(EXAMPLE_CONFIGURED_FEATURE::get);
  −
}
  −
|kotlin=val EXAMPLE_CONFIGURED_FEATURE: ConfiguredFeature<NoneFeatureConfiguration, *> by lazy {
  −
    FeatureUtils.register("examplemod:example_configured_feature", Feature.NO_OP)
  −
}
     −
object Events {
+
val EXAMPLE_RECIPE: RegistryObject<RecipeType<ExampleRecipe>> = RECIPE_TYPES.register("example_recipe") {
 
+
    RecipeType<>() {}
    @SubscribeEvent(priority = EventPriority.HIGHEST)
  −
    fun register(event: FMLCommonSetupEvent) {
  −
        event.enqueueWork(EXAMPLE_CONFIGURED_FEATURE::config)
  −
    }
   
}
 
}
|scala=class Events {
+
|scala=private final val RECIPE_TYPES = DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, MODID)
    @SubscribeEvent(priority = EventPriority.HIGHEST)
  −
    def register(event: FMLCommonSetupEvent): Unit = {
  −
        event.enqueueWork(Events.EXAMPLE_CONFIGURED_FEATURE.config _)
  −
    }
  −
}
  −
object Events {
  −
    final lazy val EXAMPLE_CONFIGURED_FEATURE = FeatureUtils.register("examplemod:example_configured_feature", Feature.NO_OP)
  −
}
  −
|}}
  −
 
  −
{{Tip/Warning|Vanilla registry methods are not thread-safe, so they must be wrapped within the synchronous queue provided within the common setup event via <code>#enqueueWork</code>.}}
     −
Besides the registries within <code>BuiltinRegistries</code>, all other '''non-forge''' wrapped registries can be statically initialized like so:
+
final val EXAMPLE_RECIPE = RECIPE_TYPES.register("example_recipe", () => new RecipeType<>() {})
 +
|groovy=private static final DeferredRegister<RecipeType<?>> RECIPE_TYPES = DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, MODID);
   −
{{Template:Tabs/Code_Snippets
+
public static final RegistryObject<RecipeType<ExampleRecipe>> EXAMPLE_RECIPE = RECIPE_TYPES.register("example_recipe", () -> new RecipeType<>() {});
|java=public static final RecipeType<ExampleRecipe> EXAMPLE_RECIPE = RecipeType.register(MODID + ":example_recipe");
  −
|kotlin=val EXAMPLE_RECIPE: RecipeType<ExampleRecipe> = RecipeType.register("${MODID}:example_recipe")
  −
|scala=final val EXAMPLE_RECIPE = RecipeType.register(s"${MODID}:example_recipe")
  −
|groovy=public static final RecipeType<ExampleRecipe> EXAMPLE_RECIPE = RecipeType.register("$MODID:example_recipe");
   
|}}
 
|}}
   Line 202: 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::of</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 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>.
    
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.of(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 'ExampleRegistry' 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.of(new ResourceLocation("examplemod", "example_object"), () -> ExampleRegistry.class);  
+
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.of(ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
+
|kotlin=val EXAMPLE_ITEM: RegistryObject<Item> = RegistryObject.create(ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
   −
// Assume that 'ExampleRegistry' 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.of(ResourceLocation("examplemod", "example_object")) { ExampleRegistry.class }
+
val EXAMPLE_OBJECT: RegistryObject<ExampleRegistry> = RegistryObject.create(ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod")
|scala=final val EXAMPLE_ITEM = RegistryObject.of(new ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
+
|scala=final val EXAMPLE_ITEM = RegistryObject.create(new ResourceLocation("examplemod:example_item"), ForgeRegistries.ITEMS)
   −
// Assume that 'ExampleRegistry' 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
final val EXAMPLE_OBJECT = RegistryObject.of(new ResourceLocation("examplemod", "example_object"), () => classOf[ExampleRegistry]);  
+
final val EXAMPLE_OBJECT = RegistryObject.create(new ResourceLocation("examplemod", "example_object"), new ResourceLocation("examplemod", "example_registry"), "examplemod");  
 
|}}
 
|}}
   Line 342: Line 317:  
=== With DeferredRegister ===
 
=== With DeferredRegister ===
   −
The first method involves the second static constructor: <code>DeferredRegister::create(Class, String)</code>. The class supplied must extend <code>IForgeRegistryEntry</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>RegistryEvent$NewRegistry</code> event.
    
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(ExampleRegistry.class, MODID);
+
|java=public static final DeferredRegister<ExampleRegistry> EXAMPLE = DeferredRegister.create("example_registry", MODID);
   −
public static final Supplier<IForgeRegistry<ExampleRegistry>> REGISTRY = EXAMPLE.makeRegistry("example_registry", RegistryBuilder::new);
+
public static final Supplier<IForgeRegistry<ExampleRegistry>> REGISTRY = EXAMPLE.makeRegistry(ExampleRegistry.class, RegistryBuilder::new);
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create(ExampleRegistry::class.java, MODID)
+
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create("example_registry", MODID)
    
val REGISTRY: IForgeRegistry<ExampleRegistry> by lazy {
 
val REGISTRY: IForgeRegistry<ExampleRegistry> by lazy {
     EXAMPLE.makeRegistry("example_registry", ::RegistryBuilder).get()
+
     EXAMPLE.makeRegistry(ExampleRegistry::class.java, ::RegistryBuilder).get()
 
}
 
}
|scala=final val EXAMPLE = DeferredRegister.create(classOf[ExampleRegistry], MODID)
+
|scala=final val EXAMPLE = DeferredRegister.create("example_registry", MODID)
   −
final lazy val REGISTRY = EXAMPLE.makeRegistry("example_registry", () => new RegistryBuilder).get
+
final lazy val REGISTRY = EXAMPLE.makeRegistry(classOf[ExampleRegistry], () => new RegistryBuilder).get
 
|}}
 
|}}