Major refactoring to use Koin.

This commit is contained in:
Logan Gorence
2021-12-23 22:44:02 +00:00
parent f8178c2307
commit 13479b1ae3
19 changed files with 269 additions and 143 deletions

View File

@ -0,0 +1,31 @@
package cloud.kubelet.foundation.core.abstraction
import cloud.kubelet.foundation.core.FoundationCorePlugin
import org.bukkit.command.CommandExecutor
import org.bukkit.command.TabCompleter
import org.bukkit.event.Listener
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.dsl.module
abstract class Feature : KoinComponent, Listener {
private val plugin by inject<FoundationCorePlugin>()
open fun enable() {}
open fun disable() {}
open fun module() = module {}
protected fun registerCommandExecutor(name: String, executor: CommandExecutor) {
registerCommandExecutor(listOf(name), executor)
}
protected fun registerCommandExecutor(names: List<String>, executor: CommandExecutor) {
for (name in names) {
val command = plugin.getCommand(name) ?: throw Exception("Failed to get $name command")
command.setExecutor(executor)
if (executor is TabCompleter) {
command.tabCompleter = executor
}
}
}
}

View File

@ -0,0 +1,54 @@
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
protected abstract val features: List<Feature>
override fun onEnable() {
pluginModule = module {
single { this@FoundationPlugin }
single { server }
single { config }
single { slF4JLogger }
}
// TODO: If we have another plugin using this class, we may need to use context isolation.
// https://insert-koin.io/docs/reference/koin-core/context-isolation
pluginApplication = startKoin {
modules(pluginModule)
modules(module())
}
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()
}
protected open fun module() = module {}
}