Skip to content

Commit

Permalink
Remove integration duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed May 3, 2024
1 parent e2ad2fa commit 8511805
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ private class Lifecycle: KoinComponent {
register(get<CheckBanOnConnectListener>())
}

get<DynmapIntegration>().onEnable()
get<EssentialsIntegration>().onEnable()
get<DynmapIntegration>().enable()
get<EssentialsIntegration>().enable()
}

suspend fun shutdown() = trace {
get<DynmapIntegration>().onDisable()
get<EssentialsIntegration>().onDisable()
get<DynmapIntegration>().disable()
get<EssentialsIntegration>().disable()

listenerRegistry.unregisterAll()
commandRegistry.unregisterAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,36 @@ 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

class DynmapIntegration(
private val plugin: JavaPlugin,
private val warpRepository: WarpRepository,
private val config: Config<PluginConfig>,
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 8511805

Please sign in to comment.