diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt index b5928392..ea745f15 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt @@ -147,13 +147,13 @@ private class Lifecycle: KoinComponent { register(get()) } - get().onEnable() - get().onEnable() + get().enable() + get().enable() } suspend fun shutdown() = trace { - get().onDisable() - get().onDisable() + get().disable() + get().disable() listenerRegistry.unregisterAll() commandRegistry.unregisterAll() diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/DynmapIntegration.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/DynmapIntegration.kt index 4e39c6fa..be9a5301 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/DynmapIntegration.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/DynmapIntegration.kt @@ -11,6 +11,7 @@ import com.projectcitybuild.pcbridge.core.modules.config.Config import com.projectcitybuild.pcbridge.core.contracts.PlatformLogger import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.plugin.Plugin import org.bukkit.plugin.java.JavaPlugin import org.dynmap.DynmapAPI @@ -18,32 +19,28 @@ class DynmapIntegration( private val plugin: JavaPlugin, private val warpRepository: WarpRepository, private val config: Config, - private val sentry: SentryReporter, private val logger: PlatformLogger, -) : Listener, SpigotIntegration { - + sentry: SentryReporter, +) : Listener, SpigotIntegration( + pluginName = "dynmap", + pluginManager = plugin.server.pluginManager, + logger = logger, + sentry = sentry, +) { class DynmapMarkerIconNotFoundException : Exception() private var dynmap: DynmapAPI? = null - override suspend fun onEnable() = runCatching { - val anyPlugin = plugin.server.pluginManager.getPlugin("dynmap") - if (anyPlugin == null) { - logger.warning("Cannot find dynmap plugin. Disabling marker integration") - return - } - check (anyPlugin is DynmapAPI) { - logger.severe("Found dynmap plugin but cannot access dynmap-api") - "Cannot cast dynmap plugin to DynmapAPI" + override suspend fun onEnable(loadedPlugin: Plugin) { + check (loadedPlugin is DynmapAPI) { + "Found dynmap plugin but cannot cast to DynmapAPI class" } - dynmap = anyPlugin + dynmap = loadedPlugin plugin.server.pluginManager.registerSuspendingEvents(this, plugin) logger.info("Dynmap integration enabled") + updateWarpMarkers() - }.onFailure { - logger.severe("Failed to enable Dynmap integration: ${it.localizedMessage}") - sentry.report(it) - }.let {} + } override suspend fun onDisable() { if (dynmap != null) { diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/EssentialsIntegration.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/EssentialsIntegration.kt index 2765de70..d509afa3 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/EssentialsIntegration.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/integrations/EssentialsIntegration.kt @@ -8,35 +8,29 @@ import com.projectcitybuild.support.spigot.SpigotIntegration import com.projectcitybuild.pcbridge.core.contracts.PlatformLogger import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.plugin.Plugin import org.bukkit.plugin.java.JavaPlugin class EssentialsIntegration( private val plugin: JavaPlugin, private val sentry: SentryReporter, private val logger: PlatformLogger, -) : Listener, SpigotIntegration { - +) : Listener, SpigotIntegration( + pluginName = "Essentials", + pluginManager = plugin.server.pluginManager, + logger = logger, + sentry = sentry, +) { private var essentials: Essentials? = null - override suspend fun onEnable() = runCatching { - val anyPlugin = plugin.server.pluginManager.getPlugin("Essentials") - if (anyPlugin == null) { - logger.severe("Cannot find EssentialsX plugin. Disabling integration") - return - } - check (anyPlugin is Essentials) { - logger.severe("Found Essentials plugin but cannot access API") - "Cannot cast plugin to Essentials" + override suspend fun onEnable(loadedPlugin: Plugin) { + check (loadedPlugin is Essentials) { + "Found Essentials plugin but cannot cast to Essentials class" } - - logger.info("Essentials integration enabled") - essentials = anyPlugin - + essentials = loadedPlugin plugin.server.pluginManager.registerSuspendingEvents(this, plugin) - }.onFailure { - logger.severe("Failed to enable Essentials integration: ${it.localizedMessage}") - sentry.report(it) - }.let {} + logger.info("Essentials integration enabled") + } override suspend fun onDisable() { if (essentials != null) { diff --git a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/support/spigot/SpigotIntegration.kt b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/support/spigot/SpigotIntegration.kt index 5fbd1bc5..8f8a5426 100644 --- a/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/support/spigot/SpigotIntegration.kt +++ b/pcbridge-spigot/src/main/kotlin/com/projectcitybuild/support/spigot/SpigotIntegration.kt @@ -1,6 +1,30 @@ package com.projectcitybuild.support.spigot -interface SpigotIntegration { - suspend fun onEnable() = run { } - suspend fun onDisable() = run { } -} +import com.projectcitybuild.core.errors.SentryReporter +import com.projectcitybuild.pcbridge.core.contracts.PlatformLogger +import org.bukkit.plugin.Plugin +import org.bukkit.plugin.PluginManager + +abstract class SpigotIntegration( + private val pluginName: String, + private val pluginManager: PluginManager, + private val sentry: SentryReporter, + private val logger: PlatformLogger, +) { + protected abstract suspend fun onEnable(loadedPlugin: Plugin) + protected abstract suspend fun onDisable() + + suspend fun enable() = runCatching { + val integratedPlugin = pluginManager.getPlugin(pluginName) + if (integratedPlugin == null) { + logger.warning("Cannot find dynmap plugin. Disabling marker integration") + return@runCatching + } + onEnable(integratedPlugin) + }.onFailure { + logger.severe("Failed to enable Dynmap integration: ${it.localizedMessage}") + sentry.report(it) + } + + suspend fun disable() = onDisable() +} \ No newline at end of file