2021-12-23 22:44:02 +00:00
|
|
|
package cloud.kubelet.foundation.core.abstraction
|
|
|
|
|
|
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin
|
|
|
|
|
import org.koin.core.KoinApplication
|
|
|
|
|
import org.koin.core.context.startKoin
|
|
|
|
|
import org.koin.core.context.stopKoin
|
|
|
|
|
import org.koin.core.module.Module
|
|
|
|
|
import org.koin.dsl.module
|
|
|
|
|
|
|
|
|
|
abstract class FoundationPlugin : JavaPlugin() {
|
|
|
|
|
private lateinit var pluginModule: Module
|
|
|
|
|
private lateinit var pluginApplication: KoinApplication
|
2021-12-23 23:05:29 +00:00
|
|
|
private lateinit var features: List<Feature>
|
|
|
|
|
private lateinit var module: Module
|
2021-12-23 22:44:02 +00:00
|
|
|
|
|
|
|
|
override fun onEnable() {
|
|
|
|
|
pluginModule = module {
|
|
|
|
|
single { this@FoundationPlugin }
|
|
|
|
|
single { server }
|
|
|
|
|
single { config }
|
|
|
|
|
single { slF4JLogger }
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 23:05:29 +00:00
|
|
|
features = createFeatures()
|
|
|
|
|
module = createModule()
|
|
|
|
|
|
2021-12-24 00:43:44 +00:00
|
|
|
// TODO: If we have another plugin using Koin, we may need to use context isolation and ensure
|
|
|
|
|
// it uses the same context so they can fetch stuff from us.
|
2021-12-23 22:44:02 +00:00
|
|
|
// https://insert-koin.io/docs/reference/koin-core/context-isolation
|
|
|
|
|
pluginApplication = startKoin {
|
|
|
|
|
modules(pluginModule)
|
2021-12-23 23:05:29 +00:00
|
|
|
modules(module)
|
2021-12-23 22:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-24 00:43:44 +00:00
|
|
|
// This is probably a bit of a hack.
|
|
|
|
|
pluginApplication.modules(module {
|
|
|
|
|
single { pluginApplication }
|
|
|
|
|
})
|
|
|
|
|
|
2021-12-23 22:44:02 +00:00
|
|
|
features.forEach {
|
|
|
|
|
pluginApplication.modules(it.module())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
features.forEach {
|
|
|
|
|
try {
|
|
|
|
|
slF4JLogger.info("Enabling feature: ${it.javaClass.simpleName}")
|
|
|
|
|
it.enable()
|
|
|
|
|
server.pluginManager.registerEvents(it, this)
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
slF4JLogger.error("Failed to enable feature: ${it.javaClass.simpleName}", e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDisable() {
|
|
|
|
|
features.forEach {
|
|
|
|
|
it.disable()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopKoin()
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 23:05:29 +00:00
|
|
|
protected open fun createModule() = module {}
|
|
|
|
|
protected abstract fun createFeatures(): List<Feature>
|
2021-12-23 22:44:02 +00:00
|
|
|
}
|