Skip to content

Commit

Permalink
update lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Jan 18, 2024
1 parent fa42469 commit 8f9d6d0
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.File

interface CoreModule {
val lifecycle: Lifecycle
val config: Dependency<EmpireConfig>
val config: Reloadable<EmpireConfig>
val translation: Dependency<PluginTranslation>
val scope: Dependency<AsyncComponent>
val dispatchers: KotlinDispatchers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package ru.astrainteractive.astrarating.db.rating.di

import org.jetbrains.exposed.sql.Database
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astrarating.db.rating.di.factory.RatingDatabaseFactory
import ru.astrainteractive.astrarating.db.rating.model.DBConnection
import ru.astrainteractive.klibs.kdi.Single
import ru.astrainteractive.klibs.kdi.getValue

interface DBRatingModule {
val lifecycle: Lifecycle
val database: Database

class Default(connection: DBConnection) : DBRatingModule {
override val database: Database by Single {
RatingDatabaseFactory(connection).create()
}
override val lifecycle: Lifecycle by lazy {
Lifecycle.Lambda(
onEnable = {
database.connector.invoke().connection
},
onDisable = {
database.connector.invoke().close()
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package ru.astrainteractive.astrarating.integration.papi

import org.bukkit.Bukkit
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astrarating.integration.papi.di.PapiDependencies

internal class RatingPAPILifecycle(dependencies: PapiDependencies) : Lifecycle {
private val expansion = RatingPAPIExpansion(dependencies)
private val isPapiExists: Boolean
get() = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null

override fun onEnable() {
if (!isPapiExists) return
if (expansion.isRegistered) {
expansion.unregister()
}
expansion.register()
}

override fun onReload() {
if (!isPapiExists) return
onDisable()
onEnable()
}

override fun onDisable() {
if (!isPapiExists) return
expansion.unregister()
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package ru.astrainteractive.astrarating

import kotlinx.coroutines.runBlocking
import org.bukkit.event.HandlerList
import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astrarating.di.impl.RootModuleImpl

/**
* Initial class for your plugin
*/
class AstraRating : JavaPlugin() {
private val rootModule = RootModuleImpl()
private val lifecycles: List<Lifecycle>
get() = listOf(
rootModule.coreModule.lifecycle,
rootModule.bukkitModule.lifecycle,
rootModule.dbRatingModule.lifecycle,
rootModule.commandsModule.lifecycle,
rootModule.eventModule.lifecycle,
)

init {
rootModule.bukkitModule.plugin.initialize(this)
Expand All @@ -19,32 +26,20 @@ class AstraRating : JavaPlugin() {
* This method called when server starts or PlugMan load plugin.
*/
override fun onEnable() {
reloadPlugin()
rootModule.dbRatingModule.database
rootModule.bukkitModule.bstats.create()
rootModule.papiModule?.ratingPAPILifecycle?.onEnable()
rootModule.bukkitModule.eventListener.value.onEnable(this)
rootModule.bukkitModule.inventoryClickEvent.value.onEnable(this)
rootModule.eventModule.eventManager
rootModule.commandsModule.commandManager
lifecycles.forEach(Lifecycle::onEnable)
}

/**
* This method called when server is shutting down or when PlugMan disable plugin.
*/
override fun onDisable() {
runBlocking { rootModule.dbRatingModule.database.connector.invoke().close() }
HandlerList.unregisterAll(this)
rootModule.bukkitModule.eventListener.value.onDisable()
rootModule.bukkitModule.inventoryClickEvent.value.onDisable()
rootModule.papiModule?.ratingPAPILifecycle?.onDisable()
lifecycles.forEach(Lifecycle::onDisable)
}

/**
* As it says, function for plugin reload
*/
fun reloadPlugin() {
rootModule.bukkitModule.config.reload()
rootModule.bukkitModule.translation.reload()
lifecycles.forEach(Lifecycle::onReload)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.astrainteractive.astrarating.command.di

import kotlinx.coroutines.CoroutineScope
import ru.astrainteractive.astralibs.async.BukkitDispatchers
import ru.astrainteractive.astralibs.serialization.KyoriComponentSerializer
import ru.astrainteractive.astrarating.AstraRating
import ru.astrainteractive.astrarating.core.EmpireConfig
Expand All @@ -12,11 +11,12 @@ import ru.astrainteractive.astrarating.gui.router.GuiRouter
import ru.astrainteractive.klibs.kdi.Module
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.getValue
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers

interface CommandsDependencies : Module {
val plugin: AstraRating
val addRatingUseCase: AddRatingUseCase
val dispatchers: BukkitDispatchers
val dispatchers: KotlinDispatchers
val scope: CoroutineScope
val translation: PluginTranslation
val config: EmpireConfig
Expand All @@ -29,10 +29,10 @@ interface CommandsDependencies : Module {
override val addRatingUseCase: AddRatingUseCase by Provider {
rootModule.sharedModule.changeRatingModule.addRatingUseCase
}
override val dispatchers by rootModule.bukkitModule.dispatchers
override val scope by rootModule.bukkitModule.scope
override val translation by rootModule.bukkitModule.translation
override val config by rootModule.bukkitModule.config
override val dispatchers = rootModule.coreModule.dispatchers
override val scope by rootModule.coreModule.scope
override val translation by rootModule.coreModule.translation
override val config by rootModule.coreModule.config

override val kyoriComponentSerializer by rootModule.bukkitModule.kyoriComponentSerializer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package ru.astrainteractive.astrarating.command.di

import CommandManager
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astrarating.di.RootModule

interface CommandsModule {
val lifecycle: Lifecycle
val commandManager: CommandManager

class Default(rootModule: RootModule) : CommandsModule {
override val commandManager: CommandManager by lazy {
val dependencies = CommandsDependencies.Default(rootModule)
CommandManager(dependencies)
}
override val lifecycle: Lifecycle by lazy {
Lifecycle.Lambda(
onEnable = {
commandManager
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.ConsoleCommandSender
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.async.BukkitDispatchers
import ru.astrainteractive.astralibs.command.api.CommandExecutor
import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible
import ru.astrainteractive.astralibs.serialization.KyoriComponentSerializer
Expand All @@ -15,12 +14,13 @@ import ru.astrainteractive.astrarating.dto.RatingType
import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase
import ru.astrainteractive.astrarating.gui.router.GuiRouter
import ru.astrainteractive.astrarating.model.PlayerModel
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers

class RatingCommandExecutor(
private val addRatingUseCase: AddRatingUseCase,
private val translation: PluginTranslation,
private val coroutineScope: CoroutineScope,
private val dispatchers: BukkitDispatchers,
private val dispatchers: KotlinDispatchers,
kyoriComponentSerializer: KyoriComponentSerializer,
private val router: GuiRouter
) : CommandExecutor<RatingCommand.Result>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package ru.astrainteractive.astrarating.command.rating

import kotlinx.coroutines.CoroutineScope
import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.async.BukkitDispatchers
import ru.astrainteractive.astralibs.command.api.Command
import ru.astrainteractive.astralibs.command.api.DefaultCommandFactory
import ru.astrainteractive.astralibs.serialization.KyoriComponentSerializer
import ru.astrainteractive.astrarating.core.PluginTranslation
import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase
import ru.astrainteractive.astrarating.gui.router.GuiRouter
import ru.astrainteractive.klibs.kdi.Factory
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers

@Suppress("LongParameterList")
class RatingCommandFactory(
private val plugin: JavaPlugin,
private val addRatingUseCase: AddRatingUseCase,
private val translation: PluginTranslation,
private val coroutineScope: CoroutineScope,
private val dispatchers: BukkitDispatchers,
private val dispatchers: KotlinDispatchers,
private val kyoriComponentSerializer: KyoriComponentSerializer,
private val guiRouter: GuiRouter
) : Factory<RatingCommand> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package ru.astrainteractive.astrarating.di

import org.bstats.bukkit.Metrics
import ru.astrainteractive.astralibs.async.AsyncComponent
import ru.astrainteractive.astralibs.async.BukkitDispatchers
import ru.astrainteractive.astralibs.event.EventListener
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astralibs.menu.event.DefaultInventoryClickEvent
import ru.astrainteractive.astralibs.serialization.KyoriComponentSerializer
import ru.astrainteractive.astrarating.AstraRating
import ru.astrainteractive.astrarating.core.EmpireConfig
import ru.astrainteractive.astrarating.core.PluginTranslation
import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge
import ru.astrainteractive.klibs.kdi.Factory
import ru.astrainteractive.klibs.kdi.Lateinit
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.Reloadable
import ru.astrainteractive.klibs.kdi.Single
import ru.astrainteractive.klibs.kdi.getValue

interface BukkitModule {
val lifecycle: Lifecycle

// Core
val plugin: Lateinit<AstraRating>
val inventoryClickEvent: Single<DefaultInventoryClickEvent>
Expand All @@ -25,10 +23,39 @@ interface BukkitModule {
// Services
val bstats: Factory<Metrics>
val eventListener: Single<EventListener>
val dispatchers: Single<BukkitDispatchers>
val scope: Single<AsyncComponent>
val config: Reloadable<EmpireConfig>
val translation: Reloadable<PluginTranslation>

val platformBridge: Provider<PlatformBridge>
class Default : BukkitModule {
override val plugin = Lateinit<AstraRating>()

override val inventoryClickEvent: Single<DefaultInventoryClickEvent> = Single {
DefaultInventoryClickEvent()
}

override val kyoriComponentSerializer: Reloadable<KyoriComponentSerializer> = Reloadable {
KyoriComponentSerializer.Legacy
}

override val bstats = Factory {
val plugin by plugin
Metrics(plugin, 15801)
}

override val eventListener: Single<EventListener> = Single {
object : EventListener {}
}

override val lifecycle: Lifecycle by lazy {
Lifecycle.Lambda(
onEnable = {
eventListener.value.onEnable(plugin.value)
inventoryClickEvent.value.onEnable(plugin.value)
bstats.create()
},
onDisable = {
eventListener.value.onDisable()
inventoryClickEvent.value.onDisable()
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.astrainteractive.astrarating.di

import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule
import ru.astrainteractive.astrarating.command.di.CommandsModule
import ru.astrainteractive.astrarating.core.di.CoreModule
import ru.astrainteractive.astrarating.db.rating.di.DBRatingModule
import ru.astrainteractive.astrarating.event.di.EventModule
import ru.astrainteractive.astrarating.feature.di.SharedModule
Expand All @@ -11,9 +12,10 @@ import ru.astrainteractive.klibs.kdi.Module

interface RootModule : Module {

// Modules
val bukkitModule: BukkitModule

val coreModule: CoreModule

val dbRatingModule: DBRatingModule

val apiRatingModule: ApiRatingModule
Expand Down
Loading

0 comments on commit 8f9d6d0

Please sign in to comment.