mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-28 00:41:31 +00:00
Major refactoring to use Koin.
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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 {}
|
||||
}
|
Reference in New Issue
Block a user