Kotlin for Forge

Kotlin for Forge

73M Downloads

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

filloax opened this issue ยท 1 comments

commented

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?

commented

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.