Skip to content

Commit

Permalink
updated AstraLibs to 2.3.0-RC2; new features on http; commands gui an…
Browse files Browse the repository at this point in the history
…d events changed
  • Loading branch information
makeevrserg committed Feb 7, 2023
1 parent 8c90e7e commit 2cafe01
Show file tree
Hide file tree
Showing 33 changed files with 228 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.astrainteractive.astratemplate.domain.local.entities.RatingRelationTa
import com.astrainteractive.astratemplate.domain.local.entities.User
import com.astrainteractive.astratemplate.domain.local.entities.UserRating
import com.astrainteractive.astratemplate.domain.local.entities.UserTable
import com.astrainteractive.astratemplate.domain.remote.RestApi
import com.astrainteractive.astratemplate.domain.remote.RMResponse
import com.astrainteractive.astratemplate.domain.remote.RickMortyApi
import ru.astrainteractive.astralibs.orm.Database
import java.util.*
import kotlin.random.Random
Expand All @@ -17,21 +18,22 @@ import kotlin.random.Random
*/
class Repository(
private val databaseDataSource: Database,
private val restDataSource: RestApi,
private val restDataSource: RickMortyApi,
) {
suspend fun getRandomCharacter(id: Int = Random.nextInt(1, 30)) =
restDataSource.getRandomCharacter(id)?.await?.invoke()
suspend fun getRandomCharacter(id: Int): Result<RMResponse> {
return restDataSource.getRandomCharacter(id)
}

suspend fun insertUser(user: UserDTO): Int {
return UserTable.insert {
return UserTable.insert(databaseDataSource) {
this[UserTable.discordId] = user.discordId
this[UserTable.minecraftUuid] = user.minecraftUUID
}
}


suspend fun insertRating(user: UserDTO): Int {
return RatingRelationTable.insert {
return RatingRelationTable.insert(databaseDataSource) {
this[RatingRelationTable.userID] = user.id
this[RatingRelationTable.reason] = UUID.randomUUID().toString()
}
Expand All @@ -44,7 +46,7 @@ class Repository(
}

suspend fun updateUser(user: UserDTO) {
UserTable.find(constructor = User) {
UserTable.find(databaseDataSource,User) {
UserTable.id.eq(user.id)
}.firstOrNull()?.apply {
this.minecraftUuid = user.minecraftUUID
Expand All @@ -54,7 +56,7 @@ class Repository(
}

suspend fun deleteUser(user: UserDTO) {
UserTable.delete<User> {
UserTable.delete<User>(databaseDataSource) {
UserTable.id.eq(user.id)
}
}
Expand All @@ -65,5 +67,6 @@ class Repository(
suspend fun getAllUsers(): List<UserDTO>? {
return UserTable.all(databaseDataSource, User).map(UserMapper::toDTO)
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.astrainteractive.astratemplate.domain.local.dto.mapping
import com.astrainteractive.astratemplate.domain.local.dto.RatingDTO
import com.astrainteractive.astratemplate.domain.local.entities.RatingRelationTable
import com.astrainteractive.astratemplate.domain.local.entities.UserRating
import ru.astrainteractive.astralibs.domain.mapping.IMapper
import ru.astrainteractive.astralibs.domain.mapping.Mapper

interface IRatingMapper : IMapper<UserRating, RatingDTO>
interface IRatingMapper : Mapper<UserRating, RatingDTO>
object RatingMapper : IRatingMapper {
override fun fromDTO(it: RatingDTO): UserRating {
return RatingRelationTable.find(constructor = UserRating) {
RatingRelationTable.id.eq(it.id)
}.firstOrNull()!!
throw NotImplementedError()
}

override fun toDTO(it: UserRating): RatingDTO = RatingDTO(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package com.astrainteractive.astratemplate.domain.local.dto.mapping

import com.astrainteractive.astratemplate.domain.local.dto.RatingDTO
import com.astrainteractive.astratemplate.domain.local.dto.UserDTO
import com.astrainteractive.astratemplate.domain.local.entities.RatingRelationTable
import com.astrainteractive.astratemplate.domain.local.entities.User
import com.astrainteractive.astratemplate.domain.local.entities.UserRating
import com.astrainteractive.astratemplate.domain.local.entities.UserTable
import ru.astrainteractive.astralibs.domain.mapping.IMapper
import ru.astrainteractive.astralibs.domain.mapping.Mapper

interface IUserMapper : IMapper<User, UserDTO>
interface IUserMapper : Mapper<User, UserDTO>

object UserMapper : IUserMapper {
override fun fromDTO(it: UserDTO): User {
return UserTable.find(constructor = User) {
UserTable.id.eq(it.id)
}.firstOrNull()!!
throw NotImplementedError()
}

override fun toDTO(it: User): UserDTO = UserDTO(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.astrainteractive.astratemplate.domain.remote

data class ResponseClass(
data class RMResponse(
val name: String,
val status: String,
val species: String,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.astrainteractive.astratemplate.domain.remote

import org.jetbrains.kotlin.com.google.gson.Gson
import ru.astrainteractive.astralibs.http.HttpClient
import ru.astrainteractive.astralibs.rest.*
import kotlin.random.Random

interface RickMortyApi {
suspend fun getRandomCharacter(id: Int): Result<RMResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.astrainteractive.astratemplate.domain.remote

import org.jetbrains.kotlin.com.google.gson.Gson
import ru.astrainteractive.astralibs.http.HttpClient

class RickMortyApiImpl(
private val client: HttpClient = HttpClient
) : RickMortyApi {
override suspend fun getRandomCharacter(id: Int): Result<RMResponse> = kotlin.runCatching {
val json = client.get("https://rickandmortyapi.com/api/character/$id").getOrThrow()
Gson().fromJson(json, RMResponse::class.java)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package ru.astrainteractive.astratemplate
import com.astrainteractive.astratemplate.domain.Repository
import com.astrainteractive.astratemplate.domain.local.entities.RatingRelationTable
import com.astrainteractive.astratemplate.domain.local.entities.UserTable
import com.astrainteractive.astratemplate.domain.remote.RestApi
import com.astrainteractive.astratemplate.domain.remote.RickMortyApi
import com.astrainteractive.astratemplate.domain.remote.RickMortyApiImpl
import com.google.gson.Gson
import kotlinx.coroutines.runBlocking
import ru.astrainteractive.astralibs.di.module
Expand All @@ -14,24 +15,17 @@ import java.io.File

val sqlModule = module {
runBlocking {
val database = Database()
database.openConnection("data.db", DBConnection.SQLite)
UserTable.create(database)
RatingRelationTable.create(database)
database
}
}
val restRequestorModule = module {
RestRequester {
this.baseUrl = "https://rickandmortyapi.com/"
this.converterFactory = { json, clazz ->
json?.let { Gson().fromJson(json, clazz) }
val connection = DBConnection.SQLite("data.db")
DefaultDatabase(connection, DBSyntax.SQLite).also {
it.openConnection()
UserTable.create(it)
RatingRelationTable.create(it)
}
this.decoderFactory = Gson()::toJson
}
}

val restModule = module {
restRequestorModule.value.create(RestApi::class.java)
RickMortyApiImpl() as RickMortyApi
}
val repositoryModule = module {
Repository(sqlModule.value, restModule.value)
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
# Plugin/Mod core
plugin = "4.2.0"
plugin = "4.3.0-RC1"
name = "AstraTemplate"
group = "com.astrainteractive"
description = "Template plugin for EmpireProjekt"
Expand All @@ -10,11 +10,11 @@ destinationDirectoryForgePath = "C:\\Users\\makee\\Desktop\\Forge\\mods"
# Core
kotlin = "1.7.0"
coroutines = "1.6.3"
json = "1.3.3"
kaml = "0.46.0"
json = "1.5.0-RC"
kaml = "0.50.0"
exposed = "0.38.1"
jdbc = "3.36.0.3"
astraLibs = "2.1.1"
astraLibs = "2.3.0-RC2"
mysqlDriver = "8.0.20"
xerialSqliteJdbcVersion = "3.34.0"
# Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.astrainteractive.astratemplate

import CommandManager
import kotlinx.coroutines.cancel
import ru.astrainteractive.astratemplate.modules.SQLDatabaseModule
import ru.astrainteractive.astratemplate.modules.TranslationModule
import kotlinx.coroutines.runBlocking
Expand All @@ -11,13 +10,14 @@ import ru.astrainteractive.astralibs.AstraLibs
import ru.astrainteractive.astralibs.Logger
import ru.astrainteractive.astralibs.async.PluginScope
import ru.astrainteractive.astralibs.di.getValue
import ru.astrainteractive.astralibs.events.GlobalEventManager
import ru.astrainteractive.astralibs.menu.SharedInventoryClickEvent
import ru.astrainteractive.astratemplate.events.EventHandler
import ru.astrainteractive.astralibs.events.GlobalEventListener
import ru.astrainteractive.astralibs.menu.event.SharedInventoryClickEvent
import ru.astrainteractive.astralibs.utils.Singleton
import ru.astrainteractive.astralibs.utils.setupWithSpigot
import ru.astrainteractive.astratemplate.events.EventManager
import ru.astrainteractive.astratemplate.modules.PluginConfigModule
import ru.astrainteractive.astratemplate.modules.eventHandlerModule
import ru.astrainteractive.astratemplate.utils.Files
import ru.astrainteractive.astratemplate.utils.Singleton

/**
* Initial class for your plugin
Expand All @@ -32,7 +32,7 @@ class AstraTemplate : JavaPlugin() {
/**
* Class for handling all of your events
*/
private val eventHandler: EventHandler by eventHandlerModule
private val eventHandler: EventManager by eventHandlerModule



Expand All @@ -41,10 +41,11 @@ class AstraTemplate : JavaPlugin() {
*/
override fun onEnable() {
AstraLibs.rememberPlugin(this)
Logger.prefix = "AstraTemplate"
eventHandler.onEnable()
Logger.setupWithSpigot("AstraTemplate",this)
eventHandler.onEnable(this)
CommandManager()
SharedInventoryClickEvent.onEnable(GlobalEventManager)
GlobalEventListener.onEnable(this)
SharedInventoryClickEvent.onEnable(this)
Logger.log("Logger enabled", "AstraTemplate")
Logger.warn("Warn message from logger", "AstraTemplate")
Logger.error("Error message", "AstraTemplate")
Expand All @@ -57,8 +58,9 @@ class AstraTemplate : JavaPlugin() {
eventHandler.onDisable()
runBlocking { SQLDatabaseModule.value.closeConnection() }
HandlerList.unregisterAll(this)
GlobalEventManager.onDisable()
PluginScope.cancel()
GlobalEventListener.onDisable()
SharedInventoryClickEvent.onDisable()
PluginScope.close()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ import ru.astrainteractive.astralibs.utils.withEntry
import ru.astrainteractive.astratemplate.AstraTemplate



fun CommandManager.addCommandCompleter() = AstraTemplate.instance.registerTabCompleter("add") {
if (args.isEmpty())
return@registerTabCompleter Bukkit.getOnlinePlayers().map { it.name }.withEntry(args.last())

if (args.size == 1)
return@registerTabCompleter Material.values().map { it.name }.withEntry(args.last())

if (args.size == 2)
return@registerTabCompleter IntRange(1, 64).map { it.toString() }.withEntry(args.last())

return@registerTabCompleter listOf()
return@registerTabCompleter when (args.size) {
2 -> Material.values().map { it.name }.withEntry(args.last())
3 -> IntRange(1, 64).map { it.toString() }.withEntry(args.last())
else -> Bukkit.getOnlinePlayers().map { it.name }.withEntry(args.last())
}
}

/**
Expand All @@ -34,15 +28,16 @@ fun CommandManager.addCommand() = AstraTemplate.instance.registerCommand("add")
sender.sendMessage("Sender should be player")
return@registerCommand
}
val player = argument(0) {
it?.let(Bukkit::getPlayer)
}.onFailure { sender.sendMessage("Player not exists") }.successOrNull()?.value ?: return@registerCommand

val amount = argument(2) { it?.toIntOrNull() ?: 1 }.successOrNull()?.value!!

val item = argument(1) {
val material = it?.let(Material::getMaterial)
material?.let { ItemStack(it, amount) }
}.onFailure { sender.sendMessage("Item not found") }.successOrNull()?.value ?: return@registerCommand

val player = Bukkit.getPlayer(args.getOrNull(0) ?: "") ?: run {
sender.sendMessage("Plater not exists")
return@registerCommand
}
val amount = args.getOrNull(2)?.toIntOrNull() ?: 1
val item = args.getOrNull(1)?.let(Material::getMaterial)?.let { ItemStack(it, amount) } ?: run {
sender.sendMessage("Item not found")
return@registerCommand
}
player.inventory.addItem(item)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ru.astrainteractive.astratemplate.commands

import CommandManager
import ru.astrainteractive.astratemplate.commands.Arguments.Companion.getArgumentString
import org.bukkit.Bukkit
import ru.astrainteractive.astralibs.AstraLibs
import ru.astrainteractive.astralibs.commands.registerCommand
import ru.astrainteractive.astralibs.commands.registerTabCompleter
import ru.astrainteractive.astratemplate.AstraTemplate
Expand All @@ -13,19 +11,6 @@ import ru.astrainteractive.astratemplate.utils.AstraPermission
* Damage player command
*/

private class Arguments {
companion object {
val playerName: Pair<String, Int>
get() = "playerName" to 0
val damage: Pair<String, Int>
get() = "damage" to 1

fun get(argument: Pair<String, Int>, args: Array<out String>): String? = args.getOrNull(argument.second)
fun Array<out String>.getArgumentString(argument: Pair<String, Int>) = getOrNull(argument.second)
}
}


fun CommandManager.damageCompleter() = AstraTemplate.instance.registerTabCompleter("adamage") {
return@registerTabCompleter when (args.size) {
0 -> listOf("adamage")
Expand All @@ -41,17 +26,10 @@ fun CommandManager.damageCommand() = AstraTemplate.instance.registerCommand("ada
sender.sendMessage(translation.noPermission)
return@registerCommand
}
val playerName = args.getArgumentString(Arguments.playerName)
if (playerName == null) {
sender.sendMessage(translation.noPlayerName)
return@registerCommand
}
val damage = args.getArgumentString(Arguments.damage)?.toIntOrNull() ?: 1
val player = Bukkit.getPlayer(playerName)
if (player == null) {
sender.sendMessage(translation.noPlayerName)
return@registerCommand
}
val player = argument(0) {
it?.let(Bukkit::getPlayer)
}.onFailure { sender.sendMessage(translation.noPlayerName) }.successOrNull()?.value ?: return@registerCommand
val damage = argument(1) { it?.toIntOrNull() ?: 1 }.successOrNull()?.value!!
player.damage(damage.toDouble())
player.sendMessage(translation.damaged.replace("%player%", sender.name))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import ru.astrainteractive.astratemplate.AstraTemplate
* Tab completer for your plugin which is called when player typing commands
*/
fun CommandManager.tabCompleter() = AstraTemplate.instance.registerTabCompleter("atemp") {
if (args.isEmpty())
return@registerTabCompleter listOf("atemp", "atempreload")
if (args.size == 1)
return@registerTabCompleter listOf("atemp", "atempreload").withEntry(args.last())
return@registerTabCompleter listOf<String>()
return@registerTabCompleter when {
args.isEmpty() -> listOf("atemp", "atempreload")
args.size == 1 -> listOf("atemp", "atempreload").withEntry(args.last())
else -> emptyList()
}
}


Expand Down
Loading

0 comments on commit 2cafe01

Please sign in to comment.