Skip to content

Commit

Permalink
update astralibs
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Mar 30, 2024
1 parent a014110 commit e97b5b4
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 121 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ minecraft-protocollib = "4.8.0"
minecraft-wg = "7.0.7"
minecraft-vault = "1.7"
minecraft-coreprotect = "21.2"
minecraft-astralibs = "3.0.0-alpha02"
minecraft-astralibs = "3.0.0-alpha06"
minecraft-bstats = "3.0.0"
minecraft-mockbukkit = "v1.19-SNAPSHOT"

Expand Down Expand Up @@ -96,6 +96,8 @@ minecraft-astralibs-orm = { module = "ru.astrainteractive.astralibs:orm", versio
minecraft-astralibs-core = { module = "ru.astrainteractive.astralibs:core", version.ref = "minecraft-astralibs" }
minecraft-astralibs-menu-bukkit = { module = "ru.astrainteractive.astralibs:menu-bukkit", version.ref = "minecraft-astralibs" }
minecraft-astralibs-core-bukkit = { module = "ru.astrainteractive.astralibs:core-bukkit", version.ref = "minecraft-astralibs" }
minecraft-astralibs-command = { module = "ru.astrainteractive.astralibs:command", version.ref = "minecraft-astralibs" }
minecraft-astralibs-command-bukkit = { module = "ru.astrainteractive.astralibs:command-bukkit", version.ref = "minecraft-astralibs" }

# Fabric
minecraft-fabric-kotlin = { module = "net.fabricmc:fabric-language-kotlin", version.ref = "minecraft-fabric-kotlin" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,52 @@ package ru.astrainteractive.astramarket.api.market
import ru.astrainteractive.astramarket.api.market.model.MarketSlot

interface MarketApi {
/**
* Insert new [MarketSlot]
* @return id of inserted slot
* @throws Exception
*/
suspend fun insertSlot(marketSlot: MarketSlot): Int?

/**
* Changes [MarketSlot.expired] to true
* @throws Exception
*/
suspend fun expireSlot(marketSlot: MarketSlot): Unit?

/**
* @return slots with specified [MarketSlot.minecraftUuid]
* @throws Exception
*/
suspend fun getUserSlots(uuid: String, expired: Boolean): List<MarketSlot>?

/**
* @return list of all slots
* @throws Exception
*/
suspend fun getSlots(expired: Boolean): List<MarketSlot>?

/**
* @return slots where [MarketSlot.time] more than [millis]
* @throws Exception
*/
suspend fun getSlotsOlderThan(millis: Long): List<MarketSlot>?

/**
* @return slot by it's [MarketSlot.id]
* @throws Exception
*/
suspend fun getSlot(id: Int): MarketSlot?

/**
* Deletes [MarketSlot]
* @throws Exception
*/
suspend fun deleteSlot(marketSlot: MarketSlot): Unit?

/**
* @return amount of all player slots
* @throws Exception
*/
suspend fun countPlayerSlots(uuid: String): Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ import ru.astrainteractive.astramarket.db.market.entity.AuctionTable
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers
import kotlin.coroutines.CoroutineContext

internal class MarketApiImpl(
internal class SqlMarketApi(
private val database: Database,
private val auctionMapper: AuctionMapper,
dispatchers: KotlinDispatchers
) : MarketApi {
private val limitedIoDispatcher = dispatchers.IO.limitedParallelism(1)

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

override suspend fun insertSlot(marketSlot: MarketSlot): Int? = runCatchingWithContext(
override suspend fun insertSlot(
marketSlot: MarketSlot
): Int? = runCatchingWithContext(
limitedIoDispatcher
) {
AuctionTable.insert(database) {
this[AuctionTable.discordId] = marketSlot.discordId
this[AuctionTable.minecraftUuid] = marketSlot.minecraftUuid
this[AuctionTable.time] = marketSlot.time
this[AuctionTable.item] = marketSlot.item.value
Expand All @@ -37,9 +39,9 @@ internal class MarketApiImpl(
}
}.getOrNull()

override suspend fun expireSlot(marketSlot: MarketSlot) = runCatchingWithContext(
limitedIoDispatcher
) {
override suspend fun expireSlot(
marketSlot: MarketSlot
): Unit? = runCatchingWithContext(limitedIoDispatcher) {
AuctionTable.find(database, constructor = Auction) {
AuctionTable.id.eq(marketSlot.id)
}.firstOrNull()?.let {
Expand Down Expand Up @@ -83,12 +85,12 @@ internal class MarketApiImpl(
): MarketSlot? = runCatchingWithContext(limitedIoDispatcher) {
AuctionTable.find(database, constructor = Auction) {
AuctionTable.id.eq(id)
}.map(auctionMapper::toDTO).firstOrNull()
}.map(auctionMapper::toDTO).first()
}.getOrNull()

override suspend fun deleteSlot(
marketSlot: MarketSlot
) = runCatchingWithContext(limitedIoDispatcher) {
): Unit? = runCatchingWithContext(limitedIoDispatcher) {
AuctionTable.delete<Auction>(database) {
AuctionTable.id.eq(marketSlot.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ internal class AuctionMapperImpl : AuctionMapper {
override fun toDTO(it: Auction): MarketSlot =
MarketSlot(
id = it.id,
discordId = it.discordId,
minecraftUuid = it.minecraftUuid,
time = it.time,
item = EncodedObject.ByteArray(it.item),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ru.astrainteractive.astralibs.encoding.model.EncodedObject

data class MarketSlot(
val id: Int,
val discordId: String?,
val minecraftUuid: String,
val time: Long,
val item: EncodedObject.ByteArray,
Expand Down
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.MarketApiImpl
import ru.astrainteractive.astramarket.api.market.impl.SqlMarketApi
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 {
MarketApiImpl(
SqlMarketApi(
database = database,
auctionMapper = auctionMapper,
dispatchers = dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class MarketApiTest {
private val randomAuction: ru.astrainteractive.astramarket.api.market.model.MarketSlot
get() = ru.astrainteractive.astramarket.api.market.model.MarketSlot(
id = -1,
discordId = UUID.randomUUID().toString(),
minecraftUuid = UUID.randomUUID().toString(),
time = System.currentTimeMillis(),
item = EncodedObject.ByteArray(ByteArray(0)),
Expand Down
2 changes: 2 additions & 0 deletions spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dependencies {
implementation(libs.klibs.kdi)
implementation(libs.minecraft.astralibs.menu.bukkit)
implementation(libs.minecraft.astralibs.core.bukkit)
implementation(libs.minecraft.astralibs.command)
implementation(libs.minecraft.astralibs.command.bukkit)
// 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,9 +2,9 @@ package ru.astrainteractive.astramarket.command.auction

import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import ru.astrainteractive.astralibs.command.api.Command
import ru.astrainteractive.astralibs.command.api.command.BukkitCommand

interface AuctionCommand : Command<AuctionCommand.Result, AuctionCommand.Result> {
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 @@ -2,7 +2,7 @@ package ru.astrainteractive.astramarket.command.auction

import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ru.astrainteractive.astralibs.command.api.CommandExecutor
import ru.astrainteractive.astralibs.command.api.executor.CommandExecutor
import ru.astrainteractive.astramarket.api.market.model.MarketSlot
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies
import ru.astrainteractive.astramarket.domain.usecase.CreateAuctionUseCase
Expand Down Expand Up @@ -34,7 +34,6 @@ class AuctionCommandExecutor(
scope.launch(dispatchers.IO) {
val marketSlot = MarketSlot(
id = -1,
discordId = null,
minecraftUuid = input.player.uniqueId.toString(),
time = System.currentTimeMillis(),
item = encodedItem,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package ru.astrainteractive.astramarket.command.auction

import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.command.api.CommandParser
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser
import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible
import ru.astrainteractive.astramarket.core.PluginPermission

class AuctionCommandParser : CommandParser<AuctionCommand.Result> {
override fun parse(args: Array<out String>, sender: CommandSender): AuctionCommand.Result {
if (!sender.toPermissible().hasPermission(PluginPermission.Amarket)) {
class AuctionCommandParser : BukkitCommandParser<AuctionCommand.Result> {
override fun parse(commandContext: BukkitCommandContext): AuctionCommand.Result {
if (!commandContext.sender.toPermissible().hasPermission(PluginPermission.Amarket)) {
return AuctionCommand.Result.NoPermission
}
return when (args.getOrNull(0)) {
return when (commandContext.args.getOrNull(0)) {
"sell" -> {
val price = args.getOrNull(1)?.toFloatOrNull()
val price = commandContext.args.getOrNull(1)?.toFloatOrNull()
price ?: return AuctionCommand.Result.WrongPrice
val amount = args.getOrNull(2)?.toIntOrNull() ?: 1
val player = sender as? Player
val amount = commandContext.args.getOrNull(2)?.toIntOrNull() ?: 1
val player = commandContext.sender as? Player
player ?: return AuctionCommand.Result.NotPlayer
val itemInstance = player.inventory.itemInMainHand
AuctionCommand.Result.WrongPrice
Expand All @@ -29,14 +29,14 @@ class AuctionCommandParser : CommandParser<AuctionCommand.Result> {
}

"expired" -> {
val player = sender as? Player
val player = commandContext.sender as? Player
player ?: return AuctionCommand.Result.NotPlayer
AuctionCommand.Result.OpenExpired(player)
}

// Open and else
else -> {
val player = sender as? Player
val player = commandContext.sender as? Player
player ?: return AuctionCommand.Result.NotPlayer
AuctionCommand.Result.OpenAuctions(player)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.astrainteractive.astramarket.command.auction

import ru.astrainteractive.astralibs.command.api.command.Command
import ru.astrainteractive.astralibs.command.api.commandfactory.BukkitCommandFactory
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(
private val dependencies: AuctionCommandDependencies
) {
fun register() {
val command = BukkitCommandFactory.create(
alias = "amarket",
commandParser = AuctionCommandParser(),
commandExecutor = AuctionCommandExecutor(dependencies),
commandSideEffect = AuctionCommandSideEffect(dependencies),
mapper = Command.Mapper.NoOp()
)
BukkitCommandRegistry.register(
command = command,
registryContext = dependencies.plugin.toCommandRegistryContext()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package ru.astrainteractive.astramarket.command.auction

import org.bukkit.command.CommandSender
import ru.astrainteractive.astralibs.command.api.Command
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 AuctionCommandResultHandler(
class AuctionCommandSideEffect(
private val dependencies: AuctionCommandDependencies
) : Command.ResultHandler<AuctionCommand.Result>,
) : BukkitCommandSideEffect<AuctionCommand.Result>,
AuctionCommandDependencies by dependencies {
override fun handle(commandSender: CommandSender, result: AuctionCommand.Result) {
override fun handle(commandContext: BukkitCommandContext, result: AuctionCommand.Result) {
when (result) {
AuctionCommand.Result.NoPermission -> with(kyoriComponentSerializer) {
commandSender.sendMessage(translation.general.noPermissions.let(::toComponent))
commandContext.sender.sendMessage(translation.general.noPermissions.let(::toComponent))
}

AuctionCommand.Result.NotPlayer -> with(kyoriComponentSerializer) {
commandSender.sendMessage(translation.general.onlyForPlayers.let(::toComponent))
commandContext.sender.sendMessage(translation.general.onlyForPlayers.let(::toComponent))
}

AuctionCommand.Result.WrongPrice -> with(kyoriComponentSerializer) {
commandSender.sendMessage(translation.auction.wrongPrice.let(::toComponent))
commandContext.sender.sendMessage(translation.auction.wrongPrice.let(::toComponent))
}

AuctionCommand.Result.WrongUsage -> with(kyoriComponentSerializer) {
commandSender.sendMessage(translation.general.wrongArgs.let(::toComponent))
commandContext.sender.sendMessage(translation.general.wrongArgs.let(::toComponent))
}

is AuctionCommand.Result.OpenExpired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ 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
import ru.astrainteractive.klibs.kdi.Factory

class CommonCommandFactory(dependencies: CommonCommandDependencies) :
Factory<Unit>,
CommonCommandDependencies by dependencies {
private fun createReloadCommand() {
plugin.getCommand("amarketreload")?.setExecutor { sender, command, label, args ->
Expand All @@ -32,7 +30,7 @@ class CommonCommandFactory(dependencies: CommonCommandDependencies) :
}
}

override fun create() {
fun register() {
createTabCompleter()
createReloadCommand()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ru.astrainteractive.astramarket.command.di

import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astramarket.command.auction.AuctionCommandFactory
import ru.astrainteractive.astramarket.command.auction.AuctionCommandRegistry
import ru.astrainteractive.astramarket.command.auction.di.AuctionCommandDependencies
import ru.astrainteractive.astramarket.command.common.CommonCommandFactory
import ru.astrainteractive.astramarket.command.common.di.CommonCommandDependencies
Expand All @@ -16,10 +16,10 @@ interface CommandModule {
onEnable = {
CommonCommandFactory(
dependencies = CommonCommandDependencies.Default(rootModule)
).create()
AuctionCommandFactory(
).register()
AuctionCommandRegistry(
dependencies = AuctionCommandDependencies.Default(rootModule)
).create()
).register()
}
)
}
Expand Down
Loading

0 comments on commit e97b5b4

Please sign in to comment.