Changes

921 bytes added ,  04:36, 9 April 2021
RegistryEvent Kotlin and Scala
Line 62: Line 62:  
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
@SubscribeEvent
+
|java=@SubscribeEvent
 
public void registerBlocks(RegistryEvent.Register<Block> event) {
 
public void registerBlocks(RegistryEvent.Register<Block> event) {
 
     event.getRegistry().registerAll(new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block1")), new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block2")), ...);
 
     event.getRegistry().registerAll(new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block1")), new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block2")), ...);
 
}
 
}
</syntaxhighlight>
+
|kotlin=@JvmStatic
 +
@SubscribeEvent
 +
private fun registerBlocks(event: RegistryEvent.Register<Block>) =
 +
    event.registry.registerAll(Block(...).setRegistryName(new ResourceLocation(MODID, "example_block1")), Block(...).setRegistryName(new ResourceLocation(MODID, "example_block2")), ...)
 +
|scala=@SubscribeEvent
 +
def registerBlocks(event: RegistryEvent.Register[Block]): Unit =
 +
    event.getRegistry.registerAll(new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block1")), new Block(...).setRegistryName(new ResourceLocation(MODID, "example_block2")), ...)
 +
|}}
    
{{Tip/Important|Since all objects registered must be singleton, some classes cannot by themselves be registered. Instead, <code>*Type</code> classes are registered and used in the formers' constructors to wrap the flyweight objects. For example, a [[Basics_of_Tile_Entities|<code>TileEntity</code>]] is wrapped via <code>TileEntityType</code>, and <code>Entity</code> is wrapped via <code>EntityType</code>. These <code>*Type</code> classes hold factories that simply create the containing type on demand.
 
{{Tip/Important|Since all objects registered must be singleton, some classes cannot by themselves be registered. Instead, <code>*Type</code> classes are registered and used in the formers' constructors to wrap the flyweight objects. For example, a [[Basics_of_Tile_Entities|<code>TileEntity</code>]] is wrapped via <code>TileEntityType</code>, and <code>Entity</code> is wrapped via <code>EntityType</code>. These <code>*Type</code> classes hold factories that simply create the containing type on demand.
Line 73: Line 80:  
These factory holders are created through the use of their <code>*Type$Builder</code> classes. An example: (<code>REGISTER</code> here refers to a <code>DeferredRegister<TileEntityType<?>></code>)
 
These factory holders are created through the use of their <code>*Type$Builder</code> classes. An example: (<code>REGISTER</code> here refers to a <code>DeferredRegister<TileEntityType<?>></code>)
   −
<syntaxhighlight lang="java">
+
{{Template:Tabs/Code_Snippets
public static final RegistryObject<TileEntityType<ExampleTile>> EXAMPLE_TILE = REGISTER.register(
+
|java=public static final RegistryObject<TileEntityType<ExampleTile>> EXAMPLE_TILE = REGISTER.register(
 
     "example_tile", () -> TileEntityType.Builder.create(ExampleTile::new, EXAMPLE_BLOCK.get()).build(null)
 
     "example_tile", () -> TileEntityType.Builder.create(ExampleTile::new, EXAMPLE_BLOCK.get()).build(null)
 
);
 
);
</syntaxhighlight>}}
+
|kotlin=val EXAMPLE_TILE: RegistryObject<TileEntityType<ExampleTile>> = REGISTER.register("example_tile") { TileEntityType.Builder.create(::ExampleTile, EXAMPLE_BLOCK.get()).build(null)) }
 +
|scala=final val EXAMPLE_TILE = REGISTER.register("example_tile", () => TileEntityType.Builder.create(() => new ExampleTile(), GeneralRegistrar.EXAMPLE_BLOCK.get).build(null))
 +
|}}
 +
}}
    
=== Non-Forge Registries ===
 
=== Non-Forge Registries ===