Life Cycle
KotlinPlugin adds a possibility to listen to the Lifecycle of your Plugin, this means act to these events outside your Plugin Main.
typealias PluginLifecycleListener = (LifecycleEvent) -> Unit
enum class LifecycleEvent { LOAD, ENABLE, DISABLE, CONFIG_RELOAD }
fun registerKotlinPluginLifecycle(priority: Int = 1, listener: PluginLifecycleListener)
Priority Order: High priority loads first and disable lastly
yourPlugin.registerKotlinPlugin(100) {
when(it) {
LifecycleEvent.DISABLE -> dispose()
LifecycleEvent.CONFIG_RELOAD -> reapplyConfigurations()
}
}LifecycleListener
LifecycleListener is a utility interface to be used in your managers classes, it uses WithPlugin interface, this means that all extesions provided to WithPlugin we will have here.
interface LifecycleListener<T : KotlinPlugin> : PluginLifecycleListener, WithPlugin<T> {
/**
* Called when the Plugin loads (before the World)
*/
fun onPluginLoad() {}
/**
* Called when the Plugin enables and is ready to register events, commands and etc...
*/
fun onPluginEnable() {}
/**
* Called when the Plugin disable like: Server Stop,
* Reload Server or Plugins such Plugman disable the plugin.
*/
fun onPluginDisable() {}
/**
* Called when some part of your code calls [KotlinPlugin.reloadConfig]
*/
fun onConfigReload() {}
}Usage
class YourManager(override val plugin: YourPlugin) : LifecycleListener<YourPlugin> {
override fun onPluginEnable() {
retrieveDataFromDatabase()
}
override fun onPluginDisable() {
saveDataIntoTheDatabase()
}
}Registration
class YourPlugin : KotlinPlugin() {
val yourManager = lifecycle(100) { YourManager() }
}Also you can use annotations for bind lifecycle methods:
// on enable:
@Plugin
fun KotlinPlugin.start() { ... }
// on disable
@OnDisable
fun KotlinPlugin.stop() { ... }
// on config reload
@OnConfigReload
fun KotlinPlugin.configReload() { ... }
// on plugin load
@OnLoad
fun KotlinPlugin.load() { ... }Last updated