diff options
author | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2020-05-29 12:46:32 -0700 |
---|---|---|
committer | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2020-05-29 12:46:32 -0700 |
commit | d86d1dab91376ea944de5e9387a20575b50ccf1c (patch) | |
tree | 8d58a04667de724e3f2c2825c01448b12d7f7216 /src/example/kotlin | |
parent | 7f2e66f7dd2efddaf08f329d81c114143bc00cf6 (diff) | |
download | KotlinForForge-d86d1dab91376ea944de5e9387a20575b50ccf1c.tar.gz KotlinForForge-d86d1dab91376ea944de5e9387a20575b50ccf1c.tar.bz2 KotlinForForge-d86d1dab91376ea944de5e9387a20575b50ccf1c.zip |
Kotlin for Forge 1.2.2
Diffstat (limited to 'src/example/kotlin')
4 files changed, 119 insertions, 0 deletions
diff --git a/src/example/kotlin/thedarkcolour/kotlinforforge/ExampleMod.kt b/src/example/kotlin/thedarkcolour/kotlinforforge/ExampleMod.kt new file mode 100644 index 0000000..af01493 --- /dev/null +++ b/src/example/kotlin/thedarkcolour/kotlinforforge/ExampleMod.kt @@ -0,0 +1,80 @@ +package thedarkcolour.kotlinforforge + +import net.minecraft.block.Block +import net.minecraftforge.event.RegistryEvent +import net.minecraftforge.eventbus.api.SubscribeEvent +import net.minecraftforge.fml.common.Mod +import net.minecraftforge.fml.event.server.FMLServerStartingEvent +import thedarkcolour.kotlinforforge.forge.MOD_BUS +import thedarkcolour.kotlinforforge.forge.lazySidedDelegate +import thedarkcolour.kotlinforforge.proxy.ClientProxy +import thedarkcolour.kotlinforforge.proxy.IProxy +import thedarkcolour.kotlinforforge.proxy.ServerProxy + +/** + * Example mod for anyone who'd like to see + * how a mod would be made with Kotlin for Forge. + * + * This mod has a modid of "examplemod", listens + * for the ``RegistryEvent.Register<Block>`` and + * for the ``FMLServerStartingEvent``. + * + * It registers event listeners by adding event listeners + * directly to the event buses KFF provides and + * by using the ``@EventBusSubscriber`` annotation. + */ +@Mod(ExampleMod.ID) +object ExampleMod { + /** + * Your mod's ID + */ + const val ID = "examplemod" + + /** + * The sided proxy. Since we use a lazy sided delegate, + * the supplier parameters are invoked only once. + */ + private val proxy by lazySidedDelegate(::ClientProxy, ::ServerProxy) + + /** + * Example of using the KotlinEventBus + * to register a function reference. + * + * Event classes with a generic type + * should be registered using ``addGenericListener`` + * instead of ``addListener``. + */ + init { + MOD_BUS.addGenericListener(::registerBlocks) + + proxy.modConstruction() + } + + /** + * Handle block registry here. + */ + private fun registerBlocks(event: RegistryEvent.Register<Block>) { + // ... + } + + /** + * Example of an object class using the + * ``@Mod.EventBusSubscriber`` annotation + * to automatically subscribe functions + * to the forge event bus. + * + * Even though the ``Bus.FORGE`` event bus + * is default, I think that it's still + * a good practice to specify the bus explicitly. + */ + @Mod.EventBusSubscriber(modid = ExampleMod.ID, bus = Mod.EventBusSubscriber.Bus.FORGE) + object EventHandler { + /** + * Handles things like registering commands. + */ + @SubscribeEvent + fun onServerStarting(event: FMLServerStartingEvent) { + // ... + } + } +}
\ No newline at end of file diff --git a/src/example/kotlin/thedarkcolour/kotlinforforge/package.md b/src/example/kotlin/thedarkcolour/kotlinforforge/package.md new file mode 100644 index 0000000..c9c1caa --- /dev/null +++ b/src/example/kotlin/thedarkcolour/kotlinforforge/package.md @@ -0,0 +1,7 @@ +# thedarkcolour.kotlinforforge +This package contains an example main mod class +for a mod using Kotlin for Forge. + +## ExampleMod +Your main mod class should be an object declaration. +It must be annotated with the @Mod annotation.
\ No newline at end of file diff --git a/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/Proxies.kt b/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/Proxies.kt new file mode 100644 index 0000000..24c51b7 --- /dev/null +++ b/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/Proxies.kt @@ -0,0 +1,20 @@ +package thedarkcolour.kotlinforforge.proxy + +/** + * Common inheritor of both proxies. + */ +interface IProxy { + fun modConstruction() +} + +class ClientProxy : IProxy { + override fun modConstruction() { + // run client code + } +} + +class ServerProxy : IProxy { + override fun modConstruction() { + // run server code + } +}
\ No newline at end of file diff --git a/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/package.md b/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/package.md new file mode 100644 index 0000000..bb57203 --- /dev/null +++ b/src/example/kotlin/thedarkcolour/kotlinforforge/proxy/package.md @@ -0,0 +1,12 @@ +# thedarkcolour.kotlinforforge.proxy +This package has example proxy classes. +Proxies are used to provide common declarations with sided implementations. + +Forge no longer supports the proxy pattern. +The ``@SidedProxy`` annotation was removed in 1.13+. +This example shows a use case for the ``lazySidedDelegate``. +It is recommended to use the ``runWhenOn`` and ``callWhenOn`` functions +instead of proxies whenever possible. + +In this example, a proxy is instantiated lazily in the ``ExampleMod`` class. +Proxies are not the only use for sided delegates.
\ No newline at end of file |