Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Aug 9, 2024
1 parent 5d87032 commit 8da46a4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package ru.astrainteractive.astrarating.command.rating
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.command.api.command.BukkitCommand
import ru.astrainteractive.astralibs.command.api.exception.CommandException

internal interface RatingCommand : BukkitCommand {
internal interface RatingCommand {
sealed interface Result {
data object WrongUsage : Result
class ChangeRating(
val value: Int,
val message: String,
Expand All @@ -18,6 +17,9 @@ internal interface RatingCommand : BukkitCommand {
class OpenPlayerRatingGui(val player: Player, val selectedPlayerName: String) : Result
class OpenRatingsGui(val executor: Player) : Result
class Reload(val executor: CommandSender) : Result
data object NotPlayer : Result
}
sealed class Error(message: String) : CommandException(message) {
data object NotPlayer : Error("Not Player")
data object WrongUsage : Error("Wrong usage")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ internal class RatingCommandExecutor(
is RatingCommand.Result.Reload -> {
reload(input)
}

RatingCommand.Result.NotPlayer,
RatingCommand.Result.WrongUsage -> Unit
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser
import ru.astrainteractive.astralibs.command.api.parser.CommandParser

internal class RatingCommandParser : BukkitCommandParser<RatingCommand.Result> {
internal class RatingCommandParser : CommandParser<RatingCommand.Result, BukkitCommandContext> {

@Suppress("ThrowsCount")
private fun parseChangeRating(
label: String,
args: Array<out String>,
Expand All @@ -16,13 +17,13 @@ internal class RatingCommandParser : BukkitCommandParser<RatingCommand.Result> {
val amount = when (label) {
"like", "+" -> 1
"dislike", "-" -> -1
else -> return RatingCommand.Result.WrongUsage
else -> throw RatingCommand.Error.WrongUsage
}

val commandExecutor = (sender as? Player) ?: return RatingCommand.Result.NotPlayer
val commandExecutor = (sender as? Player) ?: throw RatingCommand.Error.NotPlayer

val ratedPlayer = args.getOrNull(1)
?.let(Bukkit::getOfflinePlayer) ?: return RatingCommand.Result.WrongUsage
?.let(Bukkit::getOfflinePlayer) ?: throw RatingCommand.Error.WrongUsage

val message = args.toList().subList(2, args.size).joinToString(" ")
return RatingCommand.Result.ChangeRating(
Expand All @@ -45,14 +46,14 @@ internal class RatingCommandParser : BukkitCommandParser<RatingCommand.Result> {
}

"rating" -> {
if (sender !is Player) return RatingCommand.Result.NotPlayer
if (sender !is Player) throw RatingCommand.Error.NotPlayer
commandContext.args.getOrNull(1)?.takeIf(String::isNotBlank)?.let { requestedPlayer ->
RatingCommand.Result.OpenPlayerRatingGui(sender, requestedPlayer)
} ?: RatingCommand.Result.OpenRatingsGui(sender)
}

"reload" -> RatingCommand.Result.Reload(sender)
else -> RatingCommand.Result.WrongUsage
else -> throw RatingCommand.Error.WrongUsage
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package ru.astrainteractive.astrarating.command.rating

import kotlinx.coroutines.CoroutineScope
import org.bukkit.plugin.java.JavaPlugin
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.error.ErrorHandler
import ru.astrainteractive.astralibs.command.api.util.PluginExt.registerCommand
import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astralibs.logging.JUtiltLogger
import ru.astrainteractive.astralibs.logging.Logger
import ru.astrainteractive.astrarating.core.PluginTranslation
import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase
import ru.astrainteractive.astrarating.gui.router.GuiRouter
Expand All @@ -20,11 +21,12 @@ internal class RatingCommandRegistry(
private val dispatchers: KotlinDispatchers,
private val kyoriComponentSerializer: KyoriComponentSerializer,
private val guiRouter: GuiRouter
) {
) : Logger by JUtiltLogger("RatingCommandRegistry") {

fun register() {
val command = BukkitCommandFactory.create(
plugin.registerCommand(
alias = "arating",
commandParser = RatingCommandParser(),
commandExecutor = RatingCommandExecutor(
addRatingUseCase = addRatingUseCase,
translation = translation,
Expand All @@ -33,53 +35,17 @@ internal class RatingCommandRegistry(
kyoriComponentSerializer = kyoriComponentSerializer,
router = guiRouter
),
commandParser = RatingCommandParser(),
commandSideEffect = { context, result ->
when (result) {
RatingCommand.Result.NotPlayer -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.onlyPlayerCommand.let(::toComponent))
}

RatingCommand.Result.WrongUsage -> with(kyoriComponentSerializer) {
context.sender.sendMessage(translation.wrongUsage.let(::toComponent))
}

is RatingCommand.Result.OpenPlayerRatingGui,
is RatingCommand.Result.Reload,
is RatingCommand.Result.OpenRatingsGui,
is RatingCommand.Result.ChangeRating -> Unit
}
},
mapper = {
when (it) {
is RatingCommand.Result.OpenRatingsGui -> {
RatingCommand.Result.OpenRatingsGui(it.executor)
}

is RatingCommand.Result.Reload -> {
RatingCommand.Result.Reload(it.executor)
errorHandler = ErrorHandler { commandContext, throwable ->
when (throwable) {
is RatingCommand.Error.NotPlayer -> with(kyoriComponentSerializer) {
commandContext.sender.sendMessage(translation.onlyPlayerCommand.component)
}

is RatingCommand.Result.ChangeRating -> {
RatingCommand.Result.ChangeRating(
value = it.value,
message = it.message,
executor = it.executor,
ratedPlayer = it.ratedPlayer
)
}

RatingCommand.Result.WrongUsage -> null
RatingCommand.Result.NotPlayer -> null
is RatingCommand.Result.OpenPlayerRatingGui -> {
RatingCommand.Result.OpenPlayerRatingGui(
it.player,
it.selectedPlayerName
)
is RatingCommand.Error.WrongUsage -> with(kyoriComponentSerializer) {
commandContext.sender.sendMessage(translation.wrongUsage.component)
}
else -> warn { "#register unhandled exception ${throwable::class}" }
}
}
)
BukkitCommandRegistry.register(command, plugin.toCommandRegistryContext())
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package ru.astrainteractive.astrarating.command.reload

import org.bukkit.command.CommandSender
import ru.astrainteractive.astralibs.command.api.command.BukkitCommand

internal interface ReloadCommand : BukkitCommand {
sealed interface Result {
data object NoPermission : Result
class Success(val sender: CommandSender) : Result
}

class Input(val sender: CommandSender)
internal interface ReloadCommand {
class Result(val sender: CommandSender)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ru.astrainteractive.astrarating.command.reload

import ru.astrainteractive.astralibs.command.api.commandfactory.BukkitCommandFactory
import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser
import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistry
import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistryContext.Companion.toCommandRegistryContext
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContextExt.requirePermission
import ru.astrainteractive.astralibs.command.api.error.ErrorHandler
import ru.astrainteractive.astralibs.command.api.exception.NoPermissionException
import ru.astrainteractive.astralibs.command.api.executor.CommandExecutor
import ru.astrainteractive.astralibs.command.api.parser.CommandParser
import ru.astrainteractive.astralibs.command.api.util.PluginExt.registerCommand
import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible
import ru.astrainteractive.astrarating.LifecyclePlugin
import ru.astrainteractive.astrarating.core.PluginTranslation
import ru.astrainteractive.astrarating.core.RatingPermission
Expand All @@ -17,31 +18,24 @@ internal class ReloadCommandRegistry(
) : KyoriComponentSerializer by kyoriComponentSerializer {

fun register() {
val command = BukkitCommandFactory.create(
plugin.registerCommand(
alias = "aratingreload",
commandParser = BukkitCommandParser { context ->
val hasPermission = context.sender.toPermissible().hasPermission(RatingPermission.Reload)
if (!hasPermission) return@BukkitCommandParser ReloadCommand.Result.NoPermission
ReloadCommand.Result.Success(context.sender)
},
commandExecutor = {
commandExecutor = CommandExecutor<ReloadCommand.Result> {
it.sender.sendMessage(translation.reload.let(::toComponent))
plugin.onReload()
it.sender.sendMessage(translation.reloadComplete.let(::toComponent))
},
commandSideEffect = { context, result ->
when (result) {
ReloadCommand.Result.NoPermission -> {
context.sender.sendMessage(translation.noPermission.let(::toComponent))
commandParser = CommandParser { context ->
context.requirePermission(RatingPermission.Reload)
ReloadCommand.Result(context.sender)
},
errorHandler = ErrorHandler { commandContext, throwable ->
when (throwable) {
is NoPermissionException -> with(kyoriComponentSerializer) {
commandContext.sender.sendMessage(translation.noPermission.component)
}

is ReloadCommand.Result.Success -> Unit
}
},
mapper = {
(it as? ReloadCommand.Result.Success)?.sender?.let(ReloadCommand::Input)
}
)
BukkitCommandRegistry.register(command, plugin.toCommandRegistryContext())
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.astrainteractive.astrarating.core.di

import ru.astrainteractive.astralibs.async.AsyncComponent
import ru.astrainteractive.astralibs.filemanager.impl.JVMResourceFileManager
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
import ru.astrainteractive.astralibs.serialization.StringFormatExt.parse
import ru.astrainteractive.astralibs.serialization.StringFormatExt.writeIntoFile
Expand All @@ -27,21 +26,21 @@ interface CoreModule {
) : CoreModule {

override val translation: Reloadable<PluginTranslation> = Reloadable {
val fileManager = JVMResourceFileManager("translations.yml", dataFolder, this::class.java)
val file = dataFolder.resolve("translations.yml")
val serializer = YamlStringFormat()
serializer.parse<PluginTranslation>(fileManager.configFile)
serializer.parse<PluginTranslation>(file)
.onFailure(Throwable::printStackTrace)
.getOrElse { PluginTranslation() }
.also { serializer.writeIntoFile(it, fileManager.configFile) }
.also { serializer.writeIntoFile(it, file) }
}

override val config: Reloadable<EmpireConfig> = Reloadable {
val fileManager = JVMResourceFileManager("config.yml", dataFolder, this::class.java)
val file = dataFolder.resolve("config.yml")
val serializer = YamlStringFormat()
serializer.parse<EmpireConfig>(fileManager.configFile)
serializer.parse<EmpireConfig>(file)
.onFailure(Throwable::printStackTrace)
.getOrElse { EmpireConfig() }
.also { serializer.writeIntoFile(it, fileManager.configFile) }
.also { serializer.writeIntoFile(it, file) }
}
override val scope: Dependency<AsyncComponent> = Single {
AsyncComponent.Default()
Expand Down

0 comments on commit 8da46a4

Please sign in to comment.