Skip to content

Commit

Permalink
Merge pull request #21 from Astra-Interactive/architecture
Browse files Browse the repository at this point in the history
Update modules
  • Loading branch information
makeevrserg authored Jun 11, 2024
2 parents 1d19e57 + 061dc45 commit 59b8cc6
Show file tree
Hide file tree
Showing 89 changed files with 521 additions and 439 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AstraMarket
makeevrserg.project.group=ru.astrainteractive.astramarket
makeevrserg.project.version.string=1.14.2
makeevrserg.project.version.string=1.15.0
makeevrserg.project.description=Market plugin for EmpireSMP
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
3 changes: 0 additions & 3 deletions modules/api-market/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ dependencies {
implementation(libs.bundles.kotlin)
// AstraLibs
implementation(libs.minecraft.astralibs.core)
implementation(libs.minecraft.astralibs.core.bukkit)
implementation(libs.minecraft.astralibs.menu.bukkit)
implementation(libs.minecraft.astralibs.orm)
implementation(libs.klibs.kdi)
implementation(libs.klibs.mikro.core)
implementation(libs.minecraft.bstats)
// Test
testImplementation(libs.bundles.testing.kotlin)
testImplementation(libs.tests.kotlin.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ru.astrainteractive.astramarket.api.market.impl

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import ru.astrainteractive.astralibs.logging.JUtiltLogger
import ru.astrainteractive.astralibs.logging.Logger
import ru.astrainteractive.astralibs.orm.Database
import ru.astrainteractive.astramarket.api.market.MarketApi
import ru.astrainteractive.astramarket.api.market.mapping.AuctionMapper
Expand All @@ -17,21 +19,21 @@ internal class SqlMarketApi(
private val database: Database,
private val auctionMapper: AuctionMapper,
dispatchers: KotlinDispatchers
) : MarketApi {
) : MarketApi,
Logger by JUtiltLogger("SqlMarketApi") {

private val limitedIoDispatcher = dispatchers.IO.limitedParallelism(1)

private suspend fun <T> runCatchingWithContext(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): Result<T> = runCatching {
withContext(context, block)
}
}.onFailure { throwable -> debug { throwable.stackTraceToString() } }

override suspend fun insertSlot(
marketSlot: MarketSlot
): Int? = runCatchingWithContext(
limitedIoDispatcher
) {
): Int? = runCatchingWithContext(limitedIoDispatcher) {
AuctionTable.insert(database) {
this[AuctionTable.minecraftUuid] = marketSlot.minecraftUuid
this[AuctionTable.time] = marketSlot.time
Expand Down Expand Up @@ -106,8 +108,10 @@ internal class SqlMarketApi(
}
}.getOrNull()

override suspend fun findPlayersWithSlots(isExpired: Boolean): List<PlayerAndSlots> {
return AuctionTable.all(database, Auction)
override suspend fun findPlayersWithSlots(
isExpired: Boolean
): List<PlayerAndSlots> = runCatchingWithContext(limitedIoDispatcher) {
AuctionTable.all(database, Auction)
.map(auctionMapper::toDTO)
.groupBy(MarketSlot::minecraftUuid)
.map { (uuid, slots) ->
Expand All @@ -116,5 +120,5 @@ internal class SqlMarketApi(
slots = slots
)
}
}
}.getOrNull().orEmpty()
}
24 changes: 24 additions & 0 deletions modules/command-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
// Kotlin
implementation(libs.bundles.kotlin)
// AstraLibs
implementation(libs.minecraft.astralibs.core)
implementation(libs.minecraft.astralibs.core.bukkit)
implementation(libs.klibs.kdi)
implementation(libs.klibs.mikro.core)
implementation(libs.minecraft.astralibs.command)
implementation(libs.minecraft.astralibs.command.bukkit)
// Spigot dependencies
compileOnly(libs.minecraft.paper.api)
// Local
implementation(projects.modules.apiMarket)
implementation(projects.modules.core)
implementation(projects.modules.coreBukkit)
implementation(projects.modules.marketView)
implementation(projects.modules.guiBukkit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.bukkit.inventory.ItemStack
import ru.astrainteractive.astralibs.command.api.command.BukkitCommand
import java.util.UUID

interface AuctionCommand : BukkitCommand {
internal interface AuctionCommand : BukkitCommand {
sealed interface Result {
data object NoPermission : Result
data object WrongUsage : Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ru.astrainteractive.astramarket.market.domain.usecase.CreateAuctionUseCas
import kotlin.math.max
import kotlin.math.min

class AuctionCommandExecutor(
internal class AuctionCommandExecutor(
private val dependencies: AuctionCommandDependencies
) : CommandExecutor<AuctionCommand.Result>, AuctionCommandDependencies by dependencies {
override fun execute(input: AuctionCommand.Result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser
import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible
import ru.astrainteractive.astramarket.core.PluginPermission

class AuctionCommandParser : BukkitCommandParser<AuctionCommand.Result> {
internal class AuctionCommandParser : BukkitCommandParser<AuctionCommand.Result> {
override fun parse(commandContext: BukkitCommandContext): AuctionCommand.Result {
if (!commandContext.sender.toPermissible().hasPermission(PluginPermission.Amarket)) {
return AuctionCommand.Result.NoPermission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistry
import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistryContext.Companion.toCommandRegistryContext
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies

class AuctionCommandRegistry(
internal class AuctionCommandRegistry(
private val dependencies: AuctionCommandDependencies
) {
fun register() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
import ru.astrainteractive.astralibs.command.api.sideeffect.BukkitCommandSideEffect
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies

class AuctionCommandSideEffect(
internal class AuctionCommandSideEffect(
private val dependencies: AuctionCommandDependencies
) : BukkitCommandSideEffect<AuctionCommand.Result>,
AuctionCommandDependencies by dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.encoding.encoder.ObjectEncoder
import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astramarket.core.Translation
import ru.astrainteractive.astramarket.di.RootModule
import ru.astrainteractive.astramarket.core.di.BukkitCoreModule
import ru.astrainteractive.astramarket.core.di.CoreModule
import ru.astrainteractive.astramarket.gui.router.GuiRouter
import ru.astrainteractive.astramarket.gui.router.di.RouterModule
import ru.astrainteractive.astramarket.market.di.MarketModule
import ru.astrainteractive.astramarket.market.domain.usecase.CreateAuctionUseCase
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.getValue
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers

interface AuctionCommandDependencies {
internal interface AuctionCommandDependencies {
val plugin: JavaPlugin
val kyoriComponentSerializer: KyoriComponentSerializer
val translation: Translation
Expand All @@ -22,20 +25,25 @@ interface AuctionCommandDependencies {
val dispatchers: KotlinDispatchers
val createAuctionUseCase: CreateAuctionUseCase

class Default(rootModule: RootModule) : AuctionCommandDependencies {
override val plugin: JavaPlugin by rootModule.bukkitCoreModule.plugin
override val kyoriComponentSerializer by rootModule.bukkitCoreModule.kyoriComponentSerializer
override val translation: Translation by rootModule.coreModule.translation
class Default(
coreModule: CoreModule,
bukkitCoreModule: BukkitCoreModule,
routerModule: RouterModule,
marketModule: MarketModule
) : AuctionCommandDependencies {
override val plugin: JavaPlugin by bukkitCoreModule.plugin
override val kyoriComponentSerializer by bukkitCoreModule.kyoriComponentSerializer
override val translation: Translation by coreModule.translation
override val router: GuiRouter by Provider {
rootModule.routerModule.router
routerModule.router
}
override val encoder: ObjectEncoder by Provider {
rootModule.bukkitCoreModule.encoder.value
bukkitCoreModule.encoder.value
}
override val scope: CoroutineScope by rootModule.coreModule.scope
override val dispatchers: KotlinDispatchers = rootModule.coreModule.dispatchers
override val scope: CoroutineScope by coreModule.scope
override val dispatchers: KotlinDispatchers = coreModule.dispatchers
override val createAuctionUseCase: CreateAuctionUseCase by Provider {
rootModule.marketModule.marketDomainModule.createAuctionUseCase
marketModule.marketDomainModule.createAuctionUseCase
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import org.bukkit.Bukkit
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible
import ru.astrainteractive.astralibs.util.StringListExt.withEntry
import ru.astrainteractive.astramarket.AstraMarket
import ru.astrainteractive.astramarket.command.common.di.CommonCommandDependencies
import ru.astrainteractive.astramarket.core.PluginPermission

class CommonCommandRegistry(dependencies: CommonCommandDependencies) :
internal class CommonCommandRegistry(dependencies: CommonCommandDependencies) :
CommonCommandDependencies by dependencies {
private fun createReloadCommand() {
plugin.getCommand("amarketreload")?.setExecutor { sender, command, label, args ->
if (!sender.toPermissible().hasPermission(PluginPermission.Reload)) return@setExecutor true
with(kyoriComponentSerializer) {
sender.sendMessage(translation.general.reloadStarted.let(::toComponent))
(plugin as AstraMarket).onReload()
plugin.onReload()
sender.sendMessage(translation.general.reloadSuccess.let(::toComponent))
}
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.astrainteractive.astramarket.command.common.di

import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astramarket.core.AstraMarketPlugin
import ru.astrainteractive.astramarket.core.Translation
import ru.astrainteractive.astramarket.core.di.BukkitCoreModule
import ru.astrainteractive.astramarket.core.di.CoreModule
import ru.astrainteractive.klibs.kdi.getValue

internal interface CommonCommandDependencies {
val plugin: AstraMarketPlugin
val translation: Translation
val kyoriComponentSerializer: KyoriComponentSerializer

class Default(
bukkitCoreModule: BukkitCoreModule,
coreModule: CoreModule
) : CommonCommandDependencies {
override val plugin: AstraMarketPlugin by bukkitCoreModule.plugin
override val translation: Translation by coreModule.translation
override val kyoriComponentSerializer by bukkitCoreModule.kyoriComponentSerializer
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@ import ru.astrainteractive.astramarket.command.auction.AuctionCommandRegistry
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies
import ru.astrainteractive.astramarket.command.common.CommonCommandRegistry
import ru.astrainteractive.astramarket.command.common.di.CommonCommandDependencies
import ru.astrainteractive.astramarket.di.RootModule
import ru.astrainteractive.astramarket.core.di.BukkitCoreModule
import ru.astrainteractive.astramarket.core.di.CoreModule
import ru.astrainteractive.astramarket.gui.router.di.RouterModule
import ru.astrainteractive.astramarket.market.di.MarketModule

interface CommandModule {
val lifecycle: Lifecycle

class Default(rootModule: RootModule) : CommandModule {
class Default(
coreModule: CoreModule,
bukkitCoreModule: BukkitCoreModule,
routerModule: RouterModule,
marketModule: MarketModule
) : CommandModule {
override val lifecycle: Lifecycle by lazy {
Lifecycle.Lambda(
onEnable = {
CommonCommandRegistry(
dependencies = CommonCommandDependencies.Default(rootModule)
dependencies = CommonCommandDependencies.Default(
coreModule = coreModule,
bukkitCoreModule = bukkitCoreModule
)
).register()
AuctionCommandRegistry(
dependencies = AuctionCommandDependencies.Default(rootModule)
dependencies = AuctionCommandDependencies.Default(
coreModule = coreModule,
bukkitCoreModule = bukkitCoreModule,
marketModule = marketModule,
routerModule = routerModule
)
).register()
}
)
Expand Down
19 changes: 19 additions & 0 deletions modules/core-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
// Kotlin
implementation(libs.bundles.kotlin)
// AstraLibs
implementation(libs.minecraft.astralibs.core)
implementation(libs.minecraft.astralibs.core.bukkit)
implementation(libs.klibs.kdi)
implementation(libs.minecraft.astralibs.menu.bukkit)
// Spigot dependencies
compileOnly(libs.minecraft.paper.api)
implementation(libs.minecraft.bstats)
// Local
implementation(projects.modules.core)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.astrainteractive.astramarket.core

import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.lifecycle.Lifecycle

abstract class AstraMarketPlugin : JavaPlugin(), Lifecycle {
override fun onEnable() {
super<Lifecycle>.onEnable()
super<JavaPlugin>.onEnable()
}

override fun onDisable() {
super<Lifecycle>.onDisable()
super<JavaPlugin>.onDisable()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ru.astrainteractive.astramarket.core.di

import org.bstats.bukkit.Metrics
import ru.astrainteractive.astralibs.encoding.encoder.BukkitObjectEncoder
import ru.astrainteractive.astralibs.encoding.encoder.ObjectEncoder
import ru.astrainteractive.astralibs.event.EventListener
import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astralibs.menu.event.DefaultInventoryClickEvent
import ru.astrainteractive.astramarket.core.AstraMarketPlugin
import ru.astrainteractive.klibs.kdi.Factory
import ru.astrainteractive.klibs.kdi.Lateinit
import ru.astrainteractive.klibs.kdi.Reloadable
import ru.astrainteractive.klibs.kdi.Single
import ru.astrainteractive.klibs.kdi.getValue

interface BukkitCoreModule {
val lifecycle: Lifecycle

val plugin: Lateinit<AstraMarketPlugin>
val encoder: Single<ObjectEncoder>

val inventoryClickEventListener: Single<EventListener>
val kyoriComponentSerializer: Reloadable<KyoriComponentSerializer>

class Default : BukkitCoreModule {

override val plugin: Lateinit<AstraMarketPlugin> = Lateinit()

override val encoder: Single<ObjectEncoder> = Single {
BukkitObjectEncoder()
}

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

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

private val bStatsFactory = Factory {
val plugin by plugin
Metrics(plugin, 15771)
}

override val lifecycle: Lifecycle by lazy {
Lifecycle.Lambda(
onEnable = {
bStatsFactory.create()
inventoryClickEventListener.value.onEnable(plugin.value)
},
onDisable = {
inventoryClickEventListener.value.onEnable(plugin.value)
}
)
}
}
}
Loading

0 comments on commit 59b8cc6

Please sign in to comment.