Skip to content

Commit

Permalink
fix custom currency (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg authored Sep 13, 2024
1 parent b552634 commit d1738fd
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 25 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.16.12
makeevrserg.project.version.string=1.16.13
makeevrserg.project.description=Market plugin for EmpireSMP
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ minecraft-spigot = "1.21-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper
minecraft-papi = "2.11.6" # https://github.com/PlaceholderAPI/PlaceholderAPI
minecraft-protocollib = "5.1.0" # https://github.com/dmulloy2/ProtocolLib
minecraft-vault = "1.7.1" # https://github.com/MilkBowl/VaultAPI
minecraft-astralibs = "3.12.2" # https://github.com/Astra-Interactive/AstraLibs
minecraft-astralibs = "3.14.1" # https://github.com/Astra-Interactive/AstraLibs
minecraft-bstats = "3.0.3" # https://github.com/Bastian/bStats
minecraft-mockbukkit = "3.113.0" #https://github.com/MockBukkit/MockBukkit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers
import java.util.UUID
import kotlin.coroutines.CoroutineContext

internal class SqlMarketApi(
internal class ExposedMarketApi(
private val database: Database,
private val auctionMapper: AuctionMapper,
private val dispatchers: KotlinDispatchers
Expand All @@ -30,7 +30,7 @@ internal class SqlMarketApi(
block: suspend CoroutineScope.() -> T
): Result<T> = runCatching {
mutex.withLock { withContext(context, block) }
}.onFailure { throwable -> debug { throwable.stackTraceToString() } }
}.onFailure { throwable -> error(throwable) { "Error during execution" } }

override suspend fun insertSlot(
marketSlot: MarketSlot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package ru.astrainteractive.astramarket.api.market.mapping
import ru.astrainteractive.astralibs.encoding.model.EncodedObject
import ru.astrainteractive.astramarket.api.market.model.MarketSlot
import ru.astrainteractive.astramarket.db.market.entity.Auction
import ru.astrainteractive.klibs.mikro.core.domain.Mapper

internal interface AuctionMapper : Mapper<Auction, MarketSlot>
internal interface AuctionMapper {
fun toDTO(it: Auction): MarketSlot
}

internal class AuctionMapperImpl : AuctionMapper {

override fun toDTO(it: Auction): MarketSlot =
MarketSlot(
id = it.id,
Expand All @@ -18,8 +18,4 @@ internal class AuctionMapperImpl : AuctionMapper {
price = it.price,
expired = it.expired == 1
)

override fun fromDTO(it: MarketSlot): Auction {
error("Not implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ru.astrainteractive.astralibs.orm.DBConnection
import ru.astrainteractive.astralibs.orm.DBSyntax
import ru.astrainteractive.astralibs.orm.Database
import ru.astrainteractive.astramarket.api.market.MarketApi
import ru.astrainteractive.astramarket.api.market.impl.SqlMarketApi
import ru.astrainteractive.astramarket.api.market.impl.ExposedMarketApi
import ru.astrainteractive.astramarket.api.market.mapping.AuctionMapper
import ru.astrainteractive.astramarket.api.market.mapping.AuctionMapperImpl
import ru.astrainteractive.astramarket.di.factory.DatabaseFactory
Expand Down Expand Up @@ -36,7 +36,7 @@ interface ApiMarketModule {
}

override val marketApi: MarketApi by Provider {
SqlMarketApi(
ExposedMarketApi(
database = database,
auctionMapper = auctionMapper,
dispatchers = dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ru.astrainteractive.astramarket.command.auction

import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
import ru.astrainteractive.astralibs.command.api.error.ErrorHandler
import ru.astrainteractive.astralibs.command.api.exception.NoPermissionException
import ru.astrainteractive.astralibs.command.api.exception.DefaultCommandException
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies

internal class AuctionCommandErrorHandler(
Expand All @@ -11,8 +11,14 @@ internal class AuctionCommandErrorHandler(
AuctionCommandDependencies by dependencies {
override fun handle(commandContext: BukkitCommandContext, throwable: Throwable) {
when (throwable) {
is NoPermissionException -> with(kyoriComponentSerializer) {
commandContext.sender.sendMessage(translation.general.noPermissions.component)
is DefaultCommandException -> with(kyoriComponentSerializer) {
when (throwable) {
is DefaultCommandException.NoPermissionException -> {
commandContext.sender.sendMessage(translation.general.noPermissions.component)
}

else -> error("BadArgumentException not handled")
}
}

AuctionCommand.Error.NotPlayer -> with(kyoriComponentSerializer) {
Expand All @@ -26,6 +32,8 @@ internal class AuctionCommandErrorHandler(
AuctionCommand.Error.WrongUsage -> with(kyoriComponentSerializer) {
commandContext.sender.sendMessage(translation.general.wrongArgs.component)
}

else -> error("${throwable::class} not handled")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.astrainteractive.astramarket.core

import com.charleskorn.kaml.YamlComment
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.time.Duration.Companion.days

Expand Down Expand Up @@ -34,7 +36,10 @@ data class PluginConfig(
val maxPrice: Int = 1000000,
val taxPercent: Int = 0,
val announce: Boolean = true,
val maxTimeSeconds: Long = 7.days.inWholeMilliseconds // 1*24*60*60*1000
val maxTimeSeconds: Long = 7.days.inWholeMilliseconds, // 1*24*60*60*1000
@SerialName("currency_id")
@YamlComment("The id of currency you want to use")
val currencyId: String? = null
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ interface CoreModule {
class Default(
dataFolder: File,
override val dispatchers: KotlinDispatchers,
override val economyProvider: EconomyProvider
private val getEconomyProvider: (id: String?) -> EconomyProvider
) : CoreModule {

override val translation: Reloadable<Translation> = Reloadable {
val file = dataFolder.resolve("translations.yml")
if (!file.parentFile.exists()) file.parentFile.mkdirs()
if (!file.exists()) file.createNewFile()
val serializer = YamlStringFormat()
serializer.parse<Translation>(file)
.onFailure(Throwable::printStackTrace)
Expand All @@ -39,12 +41,19 @@ interface CoreModule {

override val config: Reloadable<PluginConfig> = Reloadable {
val file = dataFolder.resolve("config.yml")
if (!file.parentFile.exists()) file.parentFile.mkdirs()
if (!file.exists()) file.createNewFile()
val serializer = YamlStringFormat()
serializer.parse<PluginConfig>(file)
.onFailure(Throwable::printStackTrace)
.getOrElse { PluginConfig() }
.also { serializer.writeIntoFile(it, file) }
}

override val economyProvider: EconomyProvider by lazy {
getEconomyProvider.invoke(config.value.auction.currencyId)
}

override val scope: Dependency<AsyncComponent> = Single {
AsyncComponent.Default()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.astrainteractive.astramarket.market.domain.usecase

import ru.astrainteractive.astralibs.economy.EconomyProvider
import ru.astrainteractive.astralibs.logging.JUtiltLogger
import ru.astrainteractive.astralibs.logging.Logger
import ru.astrainteractive.astramarket.api.market.MarketApi
import ru.astrainteractive.astramarket.api.market.model.MarketSlot
import ru.astrainteractive.astramarket.core.PluginConfig
Expand Down Expand Up @@ -29,7 +31,7 @@ internal class AuctionBuyUseCaseImpl(
private val translation: Translation,
private val config: PluginConfig,
private val economyProvider: EconomyProvider,
) : AuctionBuyUseCase {
) : AuctionBuyUseCase, Logger by JUtiltLogger("AuctionBuyUseCase") {

@Suppress("LongMethod")
override suspend operator fun invoke(input: AuctionBuyUseCase.Params): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
implementation(projects.modules.commandBukkit)
}

val destination = File("/Userss/romanmakeev/Desktop/Server/SMP/smp/plugins")
val destination = File("/home/makeevrserg/Desktop/server/data/plugins")
.takeIf(File::exists)
?: File(rootDir, "jars")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.HandlerList
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astralibs.logging.JUtiltLogger
import ru.astrainteractive.astralibs.logging.Logger
import ru.astrainteractive.astramarket.core.AstraMarketPlugin
import ru.astrainteractive.astramarket.di.RootModule

/**
* Initial class for your plugin
*/
class AstraMarket : AstraMarketPlugin() {
class AstraMarket : AstraMarketPlugin(), Logger by JUtiltLogger("AstraMarket") {

private val rootModule = RootModule.Default()
private val lifecycle: List<Lifecycle>
Expand All @@ -25,6 +27,8 @@ class AstraMarket : AstraMarketPlugin() {
override fun onEnable() {
rootModule.bukkitCoreModule.plugin.initialize(this)
lifecycle.forEach(Lifecycle::onEnable)
// Init economy provider
rootModule.coreModule.economyProvider
}

override fun onDisable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ru.astrainteractive.astramarket.di

import net.milkbowl.vault.economy.Economy
import org.bukkit.Bukkit
import ru.astrainteractive.astralibs.async.DefaultBukkitDispatchers
import ru.astrainteractive.astralibs.economy.EconomyProviderFactory
import ru.astrainteractive.astralibs.economy.VaultEconomyProvider
import ru.astrainteractive.astralibs.logging.JUtiltLogger
import ru.astrainteractive.astralibs.logging.Logger
import ru.astrainteractive.astramarket.command.di.CommandModule
import ru.astrainteractive.astramarket.core.di.BukkitCoreModule
import ru.astrainteractive.astramarket.core.di.CoreModule
Expand All @@ -25,7 +29,7 @@ internal interface RootModule {
val playersMarketModule: PlayersMarketModule
val workerModule: WorkerModule

class Default : RootModule {
class Default : RootModule, Logger by JUtiltLogger("RootModule") {
override val bukkitCoreModule: BukkitCoreModule by lazy {
BukkitCoreModule.Default()
}
Expand All @@ -34,7 +38,24 @@ internal interface RootModule {
CoreModule.Default(
dataFolder = bukkitCoreModule.plugin.value.dataFolder,
dispatchers = DefaultBukkitDispatchers(bukkitCoreModule.plugin.value),
economyProvider = EconomyProviderFactory(bukkitCoreModule.plugin.value).create()
getEconomyProvider = getEconomyProviderById@{ currencyId ->
val registrations = Bukkit.getServer().servicesManager.getRegistrations(Economy::class.java)
if (currencyId == null) {
return@getEconomyProviderById VaultEconomyProvider(
bukkitCoreModule.plugin.value
)
}
val specificEconomyProvider = registrations
.firstOrNull { it.provider.currencyNameSingular() == currencyId }
?.provider
?.let(::VaultEconomyProvider)
if (specificEconomyProvider == null) {
error { "#economyProvider could not find economy with currency: $currencyId" }
} else {
return@getEconomyProviderById specificEconomyProvider
}
error("EconomyProvider could not find economy with currency: $currencyId")
}
)
}

Expand Down
2 changes: 1 addition & 1 deletion spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors: [ "${authors}" ]
author: "${author}"
website: "${url}"
api-version: 1.18
softdepend: [ Vault, EssentialsX ]
softdepend: [ Vault, EssentialsX, AspeKt ]
#libraries: [ "${libraries}" ]
libraries:
- org.bstats:bstats-bukkit:3.0.0
Expand Down

0 comments on commit d1738fd

Please sign in to comment.