Skip to content

Commit

Permalink
Replace kdi with kotlin-existing code (#61)
Browse files Browse the repository at this point in the history
* update astralibs, kstorage

* remove some kdi dependencies

* remove single

* remove factory kdi
  • Loading branch information
makeevrserg authored Aug 4, 2024
1 parent 7208753 commit 705cb67
Show file tree
Hide file tree
Showing 43 changed files with 196 additions and 272 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AspeKt
makeevrserg.project.group=ru.astrainteractive.aspekt
makeevrserg.project.version.string=2.20.11
makeevrserg.project.version.string=2.21.0-RC1
makeevrserg.project.description=Essentials plugin for EmpireProjekt
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ driver-mysql = "8.0.33" # https://github.com/mysql/mysql-connector-j
klibs-gradleplugin = "1.3.4" # https://github.com/makeevrserg/gradle-plugin
klibs-mikro = "1.8.4" # https://github.com/makeevrserg/klibs.mikro
klibs-kdi = "1.4.5" # https://github.com/makeevrserg/klibs.kdi
klibs-kstorage = "2.4.1" # https://github.com/makeevrserg/klibs.kstorage
klibs-kstorage = "3.1.0" # https://github.com/makeevrserg/klibs.kstorage

# Minecraft
minecraft-velocity = "4.0.0-SNAPSHOT" # https://github.com/PaperMC/Velocity
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.10.0" # https://github.com/Astra-Interactive/AstraLibs
minecraft-astralibs = "3.12.0" # https://github.com/Astra-Interactive/AstraLibs
minecraft-bstats = "3.0.2" # https://github.com/Bastian/bStats
minecraft-mockbukkit = "3.96.1" #https://github.com/MockBukkit/MockBukkit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@ package ru.astrainteractive.aspekt.module.adminprivate.command.adminprivate

import org.bukkit.entity.Player
import ru.astrainteractive.aspekt.module.adminprivate.model.ChunkFlag
import ru.astrainteractive.astralibs.command.api.command.BukkitCommand
import ru.astrainteractive.astralibs.command.api.exception.CommandException

internal interface AdminPrivateCommand : BukkitCommand {
sealed interface Input {
class ShowMap(val player: Player) : Input
class Claim(val player: Player) : Input
class UnClaim(val player: Player) : Input
internal interface AdminPrivateCommand {
sealed interface Model {
class ShowMap(val player: Player) : Model
class Claim(val player: Player) : Model
class UnClaim(val player: Player) : Model
class SetFlag(
val player: Player,
val flag: ChunkFlag,
val value: Boolean
) : Input
) : Model
}

sealed interface Output {
data object WrongUsage : Output
data object NotPlayer : Output
data object NoPermission : Output
class ShowMap(val player: Player) : Output
class Claim(val player: Player) : Output
class UnClaim(val player: Player) : Output
class SetFlag(
val player: Player,
val flag: ChunkFlag,
val value: Boolean
) : Output
sealed class Error(message: String) : CommandException(message) {
data object NotPlayer : Error("The sender is not a player")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class AdminPrivateCommandExecutor(
private val translation: PluginTranslation,
private val dispatchers: BukkitDispatchers,
private val kyoriComponentSerializer: KyoriComponentSerializer
) : CommandExecutor<AdminPrivateCommand.Input> {
) : CommandExecutor<AdminPrivateCommand.Model> {

private suspend fun showMap(player: Player) {
val result = runCatching {
Expand All @@ -40,7 +40,7 @@ internal class AdminPrivateCommandExecutor(
}
}

private suspend fun setFlag(input: AdminPrivateCommand.Input.SetFlag) {
private suspend fun setFlag(input: AdminPrivateCommand.Model.SetFlag) {
val result = runCatching {
adminPrivateController.setFlag(
flag = input.flag,
Expand All @@ -61,7 +61,7 @@ internal class AdminPrivateCommandExecutor(
}
}

private suspend fun claim(input: AdminPrivateCommand.Input.Claim) {
private suspend fun claim(input: AdminPrivateCommand.Model.Claim) {
val result = runCatching {
adminPrivateController.claim(input.player.chunk.adminChunk)
}
Expand All @@ -78,7 +78,7 @@ internal class AdminPrivateCommandExecutor(
}
}

private suspend fun unclaim(input: AdminPrivateCommand.Input.UnClaim) {
private suspend fun unclaim(input: AdminPrivateCommand.Model.UnClaim) {
val result = runCatching {
adminPrivateController.unclaim(input.player.chunk.adminChunk)
}
Expand All @@ -95,21 +95,21 @@ internal class AdminPrivateCommandExecutor(
}
}

override fun execute(input: AdminPrivateCommand.Input) {
override fun execute(input: AdminPrivateCommand.Model) {
when (input) {
is AdminPrivateCommand.Input.Claim -> scope.launch(dispatchers.IO) {
is AdminPrivateCommand.Model.Claim -> scope.launch(dispatchers.IO) {
claim(input)
}

is AdminPrivateCommand.Input.SetFlag -> scope.launch(dispatchers.IO) {
is AdminPrivateCommand.Model.SetFlag -> scope.launch(dispatchers.IO) {
setFlag(input)
}

is AdminPrivateCommand.Input.ShowMap -> scope.launch(dispatchers.IO) {
is AdminPrivateCommand.Model.ShowMap -> scope.launch(dispatchers.IO) {
showMap(input.player)
}

is AdminPrivateCommand.Input.UnClaim -> scope.launch(dispatchers.IO) {
is AdminPrivateCommand.Model.UnClaim -> scope.launch(dispatchers.IO) {
unclaim(input)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,51 @@ package ru.astrainteractive.aspekt.module.adminprivate.command.adminprivate
import org.bukkit.entity.Player
import ru.astrainteractive.aspekt.module.adminprivate.model.ChunkFlag
import ru.astrainteractive.aspekt.plugin.PluginPermission
import ru.astrainteractive.astralibs.command.api.argumenttype.ArgumentType
import ru.astrainteractive.astralibs.command.api.argumenttype.PrimitiveArgumentType
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.astralibs.command.api.context.BukkitCommandContextExt.requireArgument
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContextExt.requirePermission
import ru.astrainteractive.astralibs.command.api.exception.BadArgumentException
import ru.astrainteractive.astralibs.command.api.parser.CommandParser

internal class AdminPrivateCommandParser : BukkitCommandParser<AdminPrivateCommand.Output> {
internal class AdminPrivateCommandParser : CommandParser<AdminPrivateCommand.Model, BukkitCommandContext> {

override fun parse(commandContext: BukkitCommandContext): AdminPrivateCommand.Output {
override fun parse(commandContext: BukkitCommandContext): AdminPrivateCommand.Model {
val sender = commandContext.sender
val args = commandContext.args
if (!sender.toPermissible().hasPermission(PluginPermission.AdminClaim)) {
return AdminPrivateCommand.Output.NoPermission
}
commandContext.requirePermission(PluginPermission.AdminClaim)
return when (args.getOrNull(0)) {
"map" -> {
(sender as? Player)?.let(AdminPrivateCommand.Output::ShowMap) ?: AdminPrivateCommand.Output.NotPlayer
val player = sender as? Player ?: throw AdminPrivateCommand.Error.NotPlayer
player.let(AdminPrivateCommand.Model::ShowMap)
}

"claim" -> {
(sender as? Player)?.let(AdminPrivateCommand.Output::Claim) ?: AdminPrivateCommand.Output.NotPlayer
val player = sender as? Player ?: throw AdminPrivateCommand.Error.NotPlayer
player.let(AdminPrivateCommand.Model::Claim)
}

"unclaim" -> {
(sender as? Player)?.let(AdminPrivateCommand.Output::UnClaim) ?: AdminPrivateCommand.Output.NotPlayer
val player = sender as? Player ?: throw AdminPrivateCommand.Error.NotPlayer
player.let(AdminPrivateCommand.Model::UnClaim)
}

"flag" -> {
val flag = args.getOrNull(1)?.let(ChunkFlag::valueOf)
flag ?: return AdminPrivateCommand.Output.WrongUsage
val value = args.getOrNull(2)?.toBoolean()
value ?: return AdminPrivateCommand.Output.WrongUsage
val player = (sender as? Player)
player ?: return AdminPrivateCommand.Output.NotPlayer
AdminPrivateCommand.Output.SetFlag(
val player = sender as? Player ?: throw AdminPrivateCommand.Error.NotPlayer
val flag = commandContext.requireArgument(
index = 1,
type = ArgumentType.Lambda("ChunkFlag", ChunkFlag::valueOf)
)
val value = commandContext.requireArgument(2, PrimitiveArgumentType.Boolean)
AdminPrivateCommand.Model.SetFlag(
player = player,
flag = flag,
value = value
)
}

else -> AdminPrivateCommand.Output.WrongUsage
else -> throw BadArgumentException(args.getOrNull(0), PrimitiveArgumentType.String)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import ru.astrainteractive.aspekt.module.adminprivate.controller.AdminPrivateCon
import ru.astrainteractive.aspekt.module.adminprivate.model.ChunkFlag
import ru.astrainteractive.aspekt.plugin.PluginTranslation
import ru.astrainteractive.astralibs.async.BukkitDispatchers
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.astralibs.command.api.exception.BadArgumentException
import ru.astrainteractive.astralibs.command.api.exception.NoPermissionException
import ru.astrainteractive.astralibs.command.api.util.PluginExt.registerCommand
import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astralibs.util.StringListExt.withEntry

Expand All @@ -26,7 +26,7 @@ internal class AdminPrivateCommandRegister(
when {
args.size <= 1 -> listOf("claim", "unclaim", "flag", "map").withEntry(args.getOrNull(0))
args.getOrNull(0) == "flag" -> when (args.size) {
2 -> ChunkFlag.values().map(ChunkFlag::toString).withEntry(args.getOrNull(1))
2 -> ChunkFlag.entries.map(ChunkFlag::toString).withEntry(args.getOrNull(1))
3 -> listOf("true", "false").withEntry(args.getOrNull(2))
else -> emptyList()
}
Expand All @@ -37,7 +37,7 @@ internal class AdminPrivateCommandRegister(

fun register() {
adminPrivateCompleter()
val command = BukkitCommandFactory.create(
plugin.registerCommand(
alias = "adminprivate",
commandParser = AdminPrivateCommandParser(),
commandExecutor = AdminPrivateCommandExecutor(
Expand All @@ -47,57 +47,21 @@ internal class AdminPrivateCommandRegister(
dispatchers = dispatchers,
kyoriComponentSerializer = kyoriComponentSerializer
),
commandSideEffect = { context, result ->
when (result) {
AdminPrivateCommand.Output.NoPermission -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.noPermission.let(::toComponent))
errorHandler = { context, throwable ->
when (throwable) {
is AdminPrivateCommand.Error.NotPlayer -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.onlyPlayerCommand.component)
}

AdminPrivateCommand.Output.NotPlayer -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.onlyPlayerCommand.let(::toComponent))
is BadArgumentException -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.wrongUsage.component)
}

AdminPrivateCommand.Output.WrongUsage -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.wrongUsage.let(::toComponent))
is NoPermissionException -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.general.noPermission.component)
}

else -> Unit
}
},
mapper = {
when (it) {
is AdminPrivateCommand.Output.Claim -> {
AdminPrivateCommand.Input.Claim(
player = it.player
)
}

is AdminPrivateCommand.Output.SetFlag -> {
AdminPrivateCommand.Input.SetFlag(
player = it.player,
flag = it.flag,
value = it.value
)
}

is AdminPrivateCommand.Output.ShowMap -> {
AdminPrivateCommand.Input.ShowMap(
player = it.player
)
}

is AdminPrivateCommand.Output.UnClaim -> {
AdminPrivateCommand.Input.UnClaim(
player = it.player
)
}

AdminPrivateCommand.Output.NoPermission,
AdminPrivateCommand.Output.NotPlayer,
AdminPrivateCommand.Output.WrongUsage -> null
}
}
)
BukkitCommandRegistry.register(command, plugin.toCommandRegistryContext())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ internal interface AdminPrivateControllerDependencies {
class Default(coreModule: CoreModule) : AdminPrivateControllerDependencies {
override val repository: AdminPrivateRepository by lazy {
AdminPrivateRepositoryImpl(
fileManager = coreModule.adminChunksYml.value,
dispatchers = coreModule.dispatchers.value
file = coreModule.adminChunksYml.value,
dispatchers = coreModule.dispatchers,
stringFormat = coreModule.yamlFormat
)
}
override val dispatchers: KotlinDispatchers by coreModule.dispatchers
override val dispatchers: KotlinDispatchers = coreModule.dispatchers
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package ru.astrainteractive.aspekt.module.adminprivate.data

import ru.astrainteractive.aspekt.module.adminprivate.model.AdminChunk
import ru.astrainteractive.aspekt.module.adminprivate.model.AdminPrivateConfig
import ru.astrainteractive.klibs.kstorage.suspend.SuspendKrate
import ru.astrainteractive.klibs.kstorage.suspend.SuspendMutableKrate

internal interface AdminPrivateRepository {
val krate: SuspendKrate.Mutable<AdminPrivateConfig>
val krate: SuspendMutableKrate<AdminPrivateConfig>
suspend fun getAllChunks(): List<AdminChunk>
suspend fun getChunk(chunk: AdminChunk): AdminChunk
suspend fun saveChunk(chunk: AdminChunk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import kotlinx.serialization.StringFormat
import ru.astrainteractive.aspekt.module.adminprivate.data.krate.AdminPrivateKrate
import ru.astrainteractive.aspekt.module.adminprivate.model.AdminChunk
import ru.astrainteractive.aspekt.module.adminprivate.util.uniqueWorldKey
import ru.astrainteractive.astralibs.filemanager.FileManager
import ru.astrainteractive.astralibs.serialization.YamlStringFormat
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers
import java.io.File

internal class AdminPrivateRepositoryImpl(
fileManager: FileManager,
file: File,
dispatchers: KotlinDispatchers,
stringFormat: StringFormat = YamlStringFormat()
stringFormat: StringFormat
) : AdminPrivateRepository {

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

override val krate = AdminPrivateKrate(
file = fileManager.configFile,
file = file,
stringFormat = stringFormat
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import kotlinx.serialization.StringFormat
import ru.astrainteractive.aspekt.module.adminprivate.model.AdminPrivateConfig
import ru.astrainteractive.astralibs.serialization.StringFormatExt.parse
import ru.astrainteractive.astralibs.serialization.StringFormatExt.writeIntoFile
import ru.astrainteractive.astralibs.serialization.YamlStringFormat
import ru.astrainteractive.klibs.kstorage.suspend.StateFlowSuspendKrate
import ru.astrainteractive.klibs.kstorage.suspend.flow.StateFlowSuspendMutableKrate
import ru.astrainteractive.klibs.kstorage.suspend.impl.DefaultSuspendMutableKrate
import java.io.File

internal class AdminPrivateKrate(
file: File,
stringFormat: StringFormat = YamlStringFormat()
) : StateFlowSuspendKrate.Mutable<AdminPrivateConfig> by DefaultSuspendMutableKrate(
stringFormat: StringFormat
) : StateFlowSuspendMutableKrate<AdminPrivateConfig> by DefaultSuspendMutableKrate(
factory = { AdminPrivateConfig() },
saver = { value -> stringFormat.writeIntoFile(value, file) },
loader = { stringFormat.parse<AdminPrivateConfig>(file).onFailure(Throwable::printStackTrace).getOrNull() },
Expand Down
Loading

0 comments on commit 705cb67

Please sign in to comment.