-
Notifications
You must be signed in to change notification settings - Fork 20
How KotlinPlugin Works
The KotlinPlugin is the most important part of the KotlinBukkitAPI, it allows so many things to be possible when writing a GOOD plugin, because it's focus Architecture.
Architecture is the most important thing in any code base, it defines how well your code will be maintained through time. A good architecture and good practices can make your code more boilerplate, KotlinBukkitAPI with KotlinPlugin provide a set of utilities to we build your project the fast as possible and maintain your code as good as possible.
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 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() }
}
With the ability to register to the Lifecycle events from your KotlinPlugin, this allows KotlinBukkitAPI to have a set of extensions built in that uses your Plugin lifecycle to auto dispose prevent problems.
KotlinBukkitAPI also provide a Config extension, using it, when you reload your configuration, the lifecycles receive the event. You can learn more about Configuration, in the Configuration Wiki.