thedarkcolour / KotlinForForge

Makes Kotlin forge-friendly.
GNU Lesser General Public License v2.1
194 stars 26 forks source link

[Question] How are you supposed to use SubscribeEvent in non-mod classes? #107

Closed filloax closed 4 weeks ago

filloax commented 1 month ago

For instance, let's say I have a ModNetworking class where I want to subscribe to the `RegisterPayloadHandlersEvent' statically, as the class is instanced elsewhere (example: ServiceLoader) so I cannot make it an object. Do I place @EventBusSubscriber on the class, and @SubscribeEvent on a @JvmStatic method in the companion object?

thedarkcolour commented 4 weeks ago

Here's the correct usage:

class Test {
    // other methods...

    @EventBusSubscriber(bus=EventBusSubscriber.Bus.MOD)
    companion object {
        @SubscribeEvent
        fun onGameStart(event: FMLCommonSetupEvent) {
            println("Test print please ignore")
        }
    }
}

KFF treats the companion object the same as any other object declaration, so all you need to do is annotate the companion object. No need to use @JvmStatic, KFF will use the object instance.