Skip to content

Commit

Permalink
updated astralibs
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Sep 17, 2023
1 parent 578992d commit 21813c8
Show file tree
Hide file tree
Showing 40 changed files with 234 additions and 327 deletions.
4 changes: 3 additions & 1 deletion domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ dependencies {
implementation(libs.minecraft.astralibs.spigot.core)
implementation(libs.minecraft.astralibs.spigot.gui)
implementation(libs.minecraft.astralibs.orm)
implementation(libs.minecraft.astralibs.di)
// klibs
implementation(libs.klibs.kdi)
implementation(libs.klibs.mikro)
implementation(libs.minecraft.bstats)
// Test
testImplementation(libs.bundles.testing.kotlin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.astrainteractive.astrarating.domain.mapping

import com.astrainteractive.astrarating.domain.entities.UserEntity
import com.astrainteractive.astrarating.dto.UserDTO
import ru.astrainteractive.astralibs.domain.mapping.Mapper
import ru.astrainteractive.klibs.mikro.core.domain.Mapper

object UserMapper : Mapper<UserEntity, UserDTO> {
override fun fromDTO(it: UserDTO): UserEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.astrainteractive.astrarating.domain.mapping
import com.astrainteractive.astrarating.domain.entities.UserRatingEntity
import com.astrainteractive.astrarating.dto.RatingType
import com.astrainteractive.astrarating.dto.UserRatingDTO
import ru.astrainteractive.astralibs.domain.mapping.Mapper
import ru.astrainteractive.klibs.mikro.core.domain.Mapper

object UserRatingMapper : Mapper<UserRatingEntity, UserRatingDTO> {
override fun fromDTO(it: UserRatingDTO): UserRatingEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.astrainteractive.astrarating.domain.usecases

import com.astrainteractive.astrarating.domain.api.RatingDBApi
import com.astrainteractive.astrarating.models.UserModel
import ru.astrainteractive.astralibs.domain.UseCase
import ru.astrainteractive.klibs.mikro.core.domain.UseCase
import java.util.*

/**
Expand All @@ -13,19 +13,19 @@ import java.util.*
class InsertUserUseCase(
private val databaseApi: RatingDBApi,
val discordIDProvider: (UUID) -> String?
) : UseCase<Int?, UserModel> {
) : UseCase.Parametrized<UserModel, Int?> {
private val discordUsers = mutableMapOf<String, String>()

override suspend fun run(params: UserModel): Int? {
val uuid = params.minecraftUUID.toString()
val discordID = discordUsers[uuid] ?: discordIDProvider(params.minecraftUUID)?.let {
override suspend operator fun invoke(input: UserModel): Int? {
val uuid = input.minecraftUUID.toString()
val discordID = discordUsers[uuid] ?: discordIDProvider(input.minecraftUUID)?.let {
discordUsers[uuid] = it
it
}
val user = databaseApi.selectUser(params.minecraftName).getOrNull()
val user = databaseApi.selectUser(input.minecraftName).getOrNull()
return user?.let {
databaseApi.updateUser(it.copy(discordID = discordID))
it.id
} ?: databaseApi.insertUser(params).getOrNull()
} ?: databaseApi.insertUser(input).getOrNull()
}
}
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=17
# Project
makeevrserg.project.name=AstraRating
makeevrserg.project.group=com.astrainteractive.astrarating
makeevrserg.project.version.string=1.7.7
makeevrserg.project.version.string=1.8.0-RC1
makeevrserg.project.description=Rating plugin for EmpireProjekt
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ minecraft-protocollib = "4.8.0"
minecraft-wg = "7.0.7"
minecraft-vault = "1.7"
minecraft-coreprotect = "21.2"
minecraft-astralibs = "2.7.4"
minecraft-astralibs = "2.11.0"
minecraft-bstats = "3.0.0"
minecraft-mockbukkit = "v1.19-SNAPSHOT"

Expand Down Expand Up @@ -87,9 +87,12 @@ minecraft-vaultapi = { module = "com.github.MilkBowl:VaultAPI", version.ref = "m
minecraft-bstats = { module = "org.bstats:bstats-bukkit", version.ref = "minecraft-bstats" }
minecraft-mockbukkit = { module = "com.github.MockBukkit:MockBukkit", version.ref = "minecraft-mockbukkit" }

# klibs
klibs-kdi = { module = "ru.astrainteractive.klibs:kdi", version.strictly = "1.1.0" }
klibs-mikro = { module = "ru.astrainteractive.klibs:mikro-core", version.strictly = "1.0.0" }

# AstraLibs
minecraft-astralibs-ktxcore = { module = "ru.astrainteractive.astralibs:ktx-core", version.ref = "minecraft-astralibs" }
minecraft-astralibs-di = { module = "ru.astrainteractive.astralibs:di", version.ref = "minecraft-astralibs" }
minecraft-astralibs-orm = { module = "ru.astrainteractive.astralibs:orm", version.ref = "minecraft-astralibs" }
minecraft-astralibs-spigot-core = { module = "ru.astrainteractive.astralibs:spigot-core", version.ref = "minecraft-astralibs" }
minecraft-astralibs-spigot-gui = { module = "ru.astrainteractive.astralibs:spigot-gui", version.ref = "minecraft-astralibs" }
Expand Down
4 changes: 3 additions & 1 deletion spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ dependencies {
// AstraLibs
implementation(libs.minecraft.astralibs.ktxcore)
implementation(libs.minecraft.astralibs.orm)
implementation(libs.minecraft.astralibs.di)
implementation(libs.minecraft.astralibs.spigot.gui)
implementation(libs.minecraft.astralibs.spigot.core)
// klibs
implementation(libs.klibs.kdi)
implementation(libs.klibs.mikro)
// 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 @@ -9,8 +9,7 @@ import kotlinx.coroutines.runBlocking
import org.bukkit.event.HandlerList
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.kotlin.tooling.core.UnsafeApi
import ru.astrainteractive.astralibs.events.GlobalEventListener
import ru.astrainteractive.astralibs.getValue
import ru.astrainteractive.astralibs.event.GlobalEventListener
import ru.astrainteractive.astralibs.menu.event.GlobalInventoryClickEvent

/**
Expand All @@ -28,11 +27,11 @@ class AstraRating : JavaPlugin() {
override fun onEnable() {
reloadPlugin()
RootModuleImpl.database
RootModuleImpl.bstats.build()
RootModuleImpl.bstats.create()
RootModuleImpl.papiExpansion.value?.onEnable()
GlobalInventoryClickEvent.onEnable(this)
RootModuleImpl.eventManager.build()
CommandManager(this, CommandsModuleImpl)
RootModuleImpl.eventManager.create()
CommandManager(CommandsModuleImpl)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.astrainteractive.astrarating.AstraRating

import com.astrainteractive.astrarating.commands.di.CommandsModule
import com.astrainteractive.astrarating.commands.rating.ratingCommand
import com.astrainteractive.astrarating.commands.reload.reload
Expand All @@ -10,9 +10,8 @@ import com.astrainteractive.astrarating.commands.tabCompleter
* @see Reload
*/
class CommandManager(
val plugin: AstraRating,
module: CommandsModule
) {
) : CommandsModule by module {
/**
* Here you should declare commands for your plugin
*
Expand All @@ -21,8 +20,8 @@ class CommandManager(
* etemp has TabCompleter
*/
init {
tabCompleter(plugin)
reload(plugin, module)
ratingCommand(plugin, module)
tabCompleter()
reload()
ratingCommand()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package com.astrainteractive.astrarating.commands

import CommandManager
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.commands.registerTabCompleter
import ru.astrainteractive.astralibs.command.registerTabCompleter

/**
* Tab completer for your plugin which is called when player typing commands
*/
fun CommandManager.tabCompleter(plugin: JavaPlugin) = plugin.registerTabCompleter("arating") {
fun CommandManager.tabCompleter() = plugin.registerTabCompleter("arating") {
return@registerTabCompleter when {
args.size == 1 -> listOf("reload", "like", "dislike", "rating")
args.size == 2 && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package com.astrainteractive.astrarating.commands.di

import com.astrainteractive.astrarating.commands.rating.di.RatingCommandControllerModule
import com.astrainteractive.astrarating.commands.rating.di.RatingCommandModule
import ru.astrainteractive.astralibs.Module
import com.astrainteractive.astrarating.AstraRating
import com.astrainteractive.astrarating.commands.rating.RatingCommandController
import com.astrainteractive.astrarating.domain.api.RatingDBApi
import com.astrainteractive.astrarating.domain.usecases.InsertUserUseCase
import com.astrainteractive.astrarating.gui.ratings.RatingsGUI
import com.astrainteractive.astrarating.plugin.EmpireConfig
import com.astrainteractive.astrarating.plugin.PluginTranslation
import kotlinx.coroutines.CoroutineScope
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.async.BukkitDispatchers
import ru.astrainteractive.klibs.kdi.Factory
import ru.astrainteractive.klibs.kdi.Module

interface CommandsModule : Module, RatingCommandModule, RatingCommandControllerModule
interface CommandsModule : Module {
val plugin: AstraRating
val dispatchers: BukkitDispatchers
val scope: CoroutineScope
val translation: PluginTranslation
val config: EmpireConfig
val dbApi: RatingDBApi
val insertUseCase: InsertUserUseCase
val ratingCommandController: RatingCommandController
fun ratingsGUIFactory(player: Player): Factory<RatingsGUI>
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package com.astrainteractive.astrarating.commands.rating

import CommandManager
import com.astrainteractive.astrarating.commands.rating.di.RatingCommandModule
import com.astrainteractive.astrarating.dto.RatingType
import com.astrainteractive.astrarating.exception.ValidationExceptionHandler
import com.astrainteractive.astrarating.modules.impl.RootModuleImpl
import kotlinx.coroutines.launch
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.astralibs.commands.registerCommand
import ru.astrainteractive.astralibs.getValue
import ru.astrainteractive.astralibs.command.registerCommand
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.getValue

/**
* /arating reload
* /arating like/dislike <player> <message>
* /arating rating <player>
*/
fun CommandManager.ratingCommand(
plugin: JavaPlugin,
module: RatingCommandModule
) = plugin.registerCommand("arating") {
val controller by module.ratingCommandController
val scope by module.scope
val dispatchers by module.dispatchers
val validationExceptionHandler = ValidationExceptionHandler(module.translation)
fun CommandManager.ratingCommand() = plugin.registerCommand("arating") {
val validationExceptionHandler by Provider {
ValidationExceptionHandler(translation)
}
val argument = args.getOrNull(0)
if (argument == null) {
sender.sendMessage(RootModuleImpl.translation.value.wrongUsage)
Expand All @@ -36,7 +31,7 @@ fun CommandManager.ratingCommand(
val amount = if (argument == "like") 1 else -1

scope.launch(dispatchers.BukkitAsync) {
controller.addRating(
ratingCommandController.addRating(
ratingCreator = sender,
rating = amount,
message = message,
Expand All @@ -46,7 +41,7 @@ fun CommandManager.ratingCommand(
}
}

"rating" -> controller.rating(sender)
"rating" -> ratingCommandController.rating(sender)
"reload" -> Bukkit.dispatchCommand(sender, "aratingreload")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.astrainteractive.astrarating.commands.rating

import com.astrainteractive.astrarating.commands.rating.di.RatingCommandControllerModule
import com.astrainteractive.astrarating.commands.di.CommandsModule
import com.astrainteractive.astrarating.dto.RatingType
import com.astrainteractive.astrarating.dto.UserDTO
import com.astrainteractive.astrarating.exception.ValidationException
Expand All @@ -11,18 +11,11 @@ import kotlinx.coroutines.withContext
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.getValue
import ru.astrainteractive.astralibs.utils.uuid
import ru.astrainteractive.astralibs.util.uuid

class RatingCommandController(
private val module: RatingCommandControllerModule
) {
private val dispatchers by module.dispatchers
private val scope by module.scope
private val translation by module.translation
private val config by module.config
private val databaseApi by module.dbApi
private val insertUserUseCase by module.insertUseCase
module: CommandsModule
) : CommandsModule by module {

@Throws(ValidationException::class)
@Suppress("CyclomaticComplexMethod")
Expand All @@ -47,9 +40,9 @@ class RatingCommandController(
throw ValidationException.NotEnoughOnServer(ratingCreator)
}

val maxVotesPerDay = AstraPermission.MaxRatePerDay.permissionSize(ratingCreator) ?: config.maxRatingPerDay
val maxVotesPerDay = AstraPermission.MaxRatePerDay.maxPermissionSize(ratingCreator) ?: config.maxRatingPerDay
val maxVotePerPlayer =
AstraPermission.SinglePlayerPerDay.permissionSize(ratingCreator) ?: config.maxRatingPerPlayer
AstraPermission.SinglePlayerPerDay.maxPermissionSize(ratingCreator) ?: config.maxRatingPerPlayer

// if (config.needDiscordLinked) {
// val discordMember = getLinkedDiscordID(ratingCreator)
Expand All @@ -61,9 +54,9 @@ class RatingCommandController(
// throw ValidationException.NotEnoughOnDiscord(ratingCreator)
// }

val todayVotedAmount = databaseApi.countPlayerTotalDayRated(ratingCreator.name).getOrNull() ?: 0
val todayVotedAmount = dbApi.countPlayerTotalDayRated(ratingCreator.name).getOrNull() ?: 0
val votedOnPlayerAmount =
databaseApi.countPlayerOnPlayerDayRated(ratingCreator.name, ratedPlayer.name ?: "NULL").getOrNull() ?: 0
dbApi.countPlayerOnPlayerDayRated(ratingCreator.name, ratedPlayer.name ?: "NULL").getOrNull() ?: 0

if (todayVotedAmount > maxVotesPerDay) {
throw ValidationException.AlreadyMaxDayVotes(ratingCreator)
Expand All @@ -77,11 +70,11 @@ class RatingCommandController(
throw ValidationException.WrongMessageLength(ratingCreator)
}

val playerCreatedID = insertUserUseCase(UserModel(ratingCreator.uniqueId, ratingCreator.name))
val playerCreatedID = insertUseCase(UserModel(ratingCreator.uniqueId, ratingCreator.name))
val playerReportedID =
insertUserUseCase(UserModel(ratedPlayer.uniqueId, ratedPlayer?.name ?: "NULL"))
insertUseCase(UserModel(ratedPlayer.uniqueId, ratedPlayer?.name ?: "NULL"))
if (playerCreatedID == null || playerReportedID == null) throw ValidationException.DBException(ratingCreator)
databaseApi.insertUserRating(
dbApi.insertUserRating(
reporter = UserDTO(
id = playerCreatedID,
minecraftUUID = ratingCreator.uuid,
Expand All @@ -106,7 +99,7 @@ class RatingCommandController(

fun rating(sender: CommandSender) {
scope.launch(dispatchers.BukkitAsync) {
val inventory = module.ratingsGUIFactory(sender as Player).build()
val inventory = ratingsGUIFactory(sender as Player).create()
withContext(dispatchers.BukkitMain) {
inventory.open()
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 21813c8

Please sign in to comment.