Changes

1,480 bytes added ,  05:20, 19 April 2021
Non-Forge Registries translations
Line 115: Line 115:  
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">
+
{{Template:Tabs/Code_Snippets
public static final Lazy<ConfiguredFeature<?, ?>> EXAMPLE_CONFIGURED_FEATURE = Lazy.of(() ->
+
|java=public static final Lazy<ConfiguredFeature<?, ?>> EXAMPLE_CONFIGURED_FEATURE = Lazy.of(() ->
 
     register("example_configured_feature",
 
     register("example_configured_feature",
 
         Feature.NO_OP.withConfiguration(NoFeatureConfig.field_236559_b_)
 
         Feature.NO_OP.withConfiguration(NoFeatureConfig.field_236559_b_)
Line 131: Line 131:  
     return Registry.register(WorldGenRegistries.CONFIGURED_FEATURE, new ResourceLocation(MODID, name), value);
 
     return Registry.register(WorldGenRegistries.CONFIGURED_FEATURE, new ResourceLocation(MODID, name), value);
 
}
 
}
</syntaxhighlight>
+
|kotlin=val EXAMPLE_CONFIGURED_FEATURE: ConfiguredFeature<*, *> by lazy {
 +
    register("example_configured_feature",
 +
        Feature.NO_OP.withConfiguration(NoFeatureConfig.field_236559_b_)
 +
            .withPlacement(Placement.NOPE.configure(NoPlacementConfig.INSTANCE)))
 +
}
 +
 
 +
object Events {
 +
 
 +
    @SubscribeEvent(priority = EventPriority.HIGHEST)
 +
    fun register(event: FMLCommonSetupEvent) {
 +
        event.enqueueWork(EXAMPLE_CONFIGURED_FEATURE::getConfig)
 +
    }
 +
}
 +
 
 +
private fun <T: ConfiguredFeature<*, *>> register(name: String, value: T): T =
 +
    Registry.register(WorldGenRegistries.CONFIGURED_FEATURE, ResourceLocation(MODID, name), value)
 +
|scala=class Events {
 +
    @SubscribeEvent(priority = EventPriority.HIGHEST)
 +
    def register(event: FMLCommonSetupEvent): Unit = {
 +
        event.enqueueWork(Events.EXAMPLE_CONFIGURED_FEATURE.getConfig _)
 +
    }
 +
}
 +
object Events {
 +
    final lazy val EXAMPLE_CONFIGURED_FEATURE = register("example_configured_feature",
 +
        Feature.NO_OP.withConfiguration(NoFeatureConfig.field_236559_b_)
 +
            .withPlacement(Placement.NOPE.configure(NoPlacementConfig.INSTANCE)))
 +
 
 +
    private def register[T <: ConfiguredFeature[_, _]](name: String, value: T): T =
 +
        Registry.register(WorldGenRegistries.CONFIGURED_FEATURE, new ResourceLocation(MODID, name), value)
 +
}
 +
|}}
    
{{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>.}}
 
{{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>.}}
Line 137: Line 167:  
Besides the registries within <code>WorldGenRegistries</code>, all other '''non-forge''' wrapped registries can be statically initialized like so:
 
Besides the registries within <code>WorldGenRegistries</code>, all other '''non-forge''' wrapped registries can be statically initialized like so:
   −
<syntaxhighlight lang="java">
+
{{Template:Tabs/Code_Snippets
public static final IRecipeType<ExampleRecipe> EXAMPLE_RECIPE = IRecipeType.register(MODID + ":example_recipe");
+
|java=public static final IRecipeType<ExampleRecipe> EXAMPLE_RECIPE = IRecipeType.register(MODID + ":example_recipe");
</syntaxhighlight>
+
|kotlin=val EXAMPLE_RECIPE: IRecipeType<ExampleRecipe> = IRecipeType.register("${MODID}:example_recipe")
 +
|scala=final val EXAMPLE_RECIPE = IRecipeType.register(s"${MODID}:example_recipe")
 +
|}}
    
If you attempt to make one of these instances require an instance of another registry object, you must use the lazy initialization method mentioned above to register the object in the correct order.
 
If you attempt to make one of these instances require an instance of another registry object, you must use the lazy initialization method mentioned above to register the object in the correct order.