GhostDisplays/main/kotlin/net/hareworks/ghostdisplays/GhostDisplaysPlugin.kt
2025-12-06 04:40:18 +09:00

67 lines
2.7 KiB
Kotlin

package net.hareworks.ghostdisplays
import net.hareworks.ghostdisplays.api.DisplayService
import net.hareworks.ghostdisplays.command.CommandRegistrar
import net.hareworks.ghostdisplays.display.DisplayManager
import net.hareworks.ghostdisplays.display.EditSessionManager
import net.hareworks.ghostdisplays.internal.DefaultDisplayService
import net.hareworks.ghostdisplays.internal.controller.DisplayRegistry
import net.hareworks.kommand_lib.KommandLib
import net.hareworks.permits_lib.PermitsLib
import net.hareworks.permits_lib.bukkit.MutationSession
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.plugin.ServicePriority
import org.bukkit.plugin.java.JavaPlugin
class GhostDisplaysPlugin : JavaPlugin() {
private lateinit var displayRegistry: DisplayRegistry
private lateinit var serviceImpl: DisplayService
private lateinit var displayManager: DisplayManager
private lateinit var editSessions: EditSessionManager
private lateinit var miniMessage: MiniMessage
private var permissionSession: MutationSession? = null
private var kommand: KommandLib? = null
override fun onEnable() {
miniMessage = MiniMessage.miniMessage()
displayRegistry = DisplayRegistry().also {
server.pluginManager.registerEvents(it, this)
}
serviceImpl = DefaultDisplayService(this, displayRegistry)
displayManager = DisplayManager(this, serviceImpl, miniMessage)
editSessions = EditSessionManager(this, displayManager).also {
server.pluginManager.registerEvents(it, this)
}
server.servicesManager.register(DisplayService::class.java, serviceImpl, this, ServicePriority.Normal)
permissionSession = PermitsLib.session(this)
kommand = CommandRegistrar.register(this, displayManager, editSessions, permissionSession!!)
instance = this
logger.info("GhostDisplays ready: ${displayRegistry.controllerCount()} controllers active.")
}
override fun onDisable() {
kommand?.unregister()
kommand = null
runCatching { editSessions.shutdown() }
runCatching { displayManager.destroyAll() }
runCatching { serviceImpl.destroyAll() }
permissionSession = null
server.servicesManager.unregister(DisplayService::class.java, serviceImpl)
displayRegistry.shutdown()
instance = null
}
companion object {
@Volatile
private var instance: GhostDisplaysPlugin? = null
fun service(): DisplayService =
instance?.serviceImpl ?: throw IllegalStateException("GhostDisplays plugin not enabled")
fun manager(): DisplayManager =
instance?.displayManager ?: throw IllegalStateException("GhostDisplays plugin not enabled")
}
}