From d535dee1b9ddf0708801b9f1977231c97462d0fe Mon Sep 17 00:00:00 2001 From: EncryptSL Date: Tue, 14 May 2024 03:10:46 +0200 Subject: [PATCH] Added CompletableFuture to fetch async data from database. Fixed self operations... money actually not added and synchronized into account if account is same as name. --- .../com/github/encryptsl/lite/eco/LiteEco.kt | 6 +----- .../lite/eco/api/economy/LiteEcoEconomyAPI.kt | 8 ++++++-- .../lite/eco/api/interfaces/AdapterLogger.kt | 3 ++- .../eco/api/interfaces/PlayerSQLProvider.kt | 5 +++-- .../encryptsl/lite/eco/commands/EcoCMD.kt | 2 +- .../common/database/models/DatabaseEcoModel.kt | 17 +++++++++++++---- .../database/models/DatabaseMonologModel.kt | 14 ++++++++++---- .../lite/eco/listeners/AccountManageListener.kt | 2 +- .../admin/EconomyMoneyDepositListener.kt | 12 ++++++------ .../listeners/admin/EconomyMoneySetListener.kt | 6 +++--- .../admin/EconomyMoneyWithdrawListener.kt | 11 ++++++----- .../github/encryptsl/lite/eco/utils/Helper.kt | 3 +-- 12 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/LiteEco.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/LiteEco.kt index ae7c940..76f41c8 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/LiteEco.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/LiteEco.kt @@ -203,10 +203,6 @@ class LiteEco : JavaPlugin() { } private fun createAnnotationParser(commandManager: PaperCommandManager): AnnotationParser { - val annotationParser: AnnotationParser = AnnotationParser( - commandManager, - CommandSender::class.java - ) - return annotationParser + return AnnotationParser(commandManager, CommandSender::class.java) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/api/economy/LiteEcoEconomyAPI.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/api/economy/LiteEcoEconomyAPI.kt index 40a0d42..b591dab 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/api/economy/LiteEcoEconomyAPI.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/api/economy/LiteEcoEconomyAPI.kt @@ -39,7 +39,7 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider { } override fun hasAccount(player: OfflinePlayer): Boolean { - return databaseEcoModel.getExistPlayerAccount(player.uniqueId) + return databaseEcoModel.getExistPlayerAccount(player.uniqueId).join() } override fun has(player: OfflinePlayer, amount: Double): Boolean { @@ -50,7 +50,7 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider { return if (PlayerAccount.isPlayerOnline(player.uniqueId) || PlayerAccount.isAccountCached(player.uniqueId)) PlayerAccount.getBalance(player.uniqueId) else - databaseEcoModel.getBalance(player.uniqueId) + databaseEcoModel.getBalance(player.uniqueId).join() } override fun getCheckBalanceLimit(amount: Double): Boolean { @@ -85,8 +85,12 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider { override fun setMoney(player: OfflinePlayer, amount: Double) { if (PlayerAccount.isPlayerOnline(player.uniqueId)) { cacheAccount(player, amount) + println(amount) + println(getBalance(player)) } else { databaseEcoModel.setMoney(player.uniqueId, amount) + println(amount) + println(getBalance(player)) } } diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/AdapterLogger.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/AdapterLogger.kt index 62f35f2..f716b87 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/AdapterLogger.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/AdapterLogger.kt @@ -1,11 +1,12 @@ package com.github.encryptsl.lite.eco.api.interfaces import com.github.encryptsl.lite.eco.common.database.entity.EconomyLog +import java.util.concurrent.CompletableFuture interface AdapterLogger { fun error(message: String) fun warning(message: String) fun info(message: String) fun clearLogs() - fun getLog(): List + fun getLog(): CompletableFuture> } \ No newline at end of file diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/PlayerSQLProvider.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/PlayerSQLProvider.kt index 5706023..17f719e 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/PlayerSQLProvider.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/api/interfaces/PlayerSQLProvider.kt @@ -1,15 +1,16 @@ package com.github.encryptsl.lite.eco.api.interfaces import java.util.* +import java.util.concurrent.CompletableFuture interface PlayerSQLProvider { fun createPlayerAccount(username: String, uuid: UUID, money: Double) fun deletePlayerAccount(uuid: UUID) - fun getExistPlayerAccount(uuid: UUID): Boolean + fun getExistPlayerAccount(uuid: UUID): CompletableFuture fun getTopBalance(top: Int): MutableMap fun getTopBalance(): MutableMap fun getPlayersIds(): MutableCollection - fun getBalance(uuid: UUID): Double + fun getBalance(uuid: UUID): CompletableFuture fun depositMoney(uuid: UUID, money: Double) fun withdrawMoney(uuid: UUID, money: Double) fun setMoney(uuid: UUID, money: Double) diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/commands/EcoCMD.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/commands/EcoCMD.kt index 3623d72..08dc33b 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/commands/EcoCMD.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/commands/EcoCMD.kt @@ -185,7 +185,7 @@ class EcoCMD(private val liteEco: LiteEco) { commandSender.sendMessage(liteEco.locale.translation("messages.admin.purge_default_accounts")) } PurgeKey.MONO_LOG -> { - val logs = liteEco.loggerModel.getLog() + val logs = liteEco.loggerModel.getLog().join() if (logs.isEmpty()) return commandSender.sendMessage(liteEco.locale.translation("messages.error.purge_monolog_fail")) diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseEcoModel.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseEcoModel.kt index 2fc73e6..7276b95 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseEcoModel.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseEcoModel.kt @@ -10,6 +10,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.minus import org.jetbrains.exposed.sql.SqlExpressionBuilder.notInList import org.jetbrains.exposed.sql.SqlExpressionBuilder.plus import java.util.* +import java.util.concurrent.CompletableFuture class DatabaseEcoModel : PlayerSQLProvider { @@ -29,8 +30,11 @@ class DatabaseEcoModel : PlayerSQLProvider { } } - override fun getExistPlayerAccount(uuid: UUID): Boolean = loggedTransaction { - !Account.select(Account.uuid).where(Account.uuid eq uuid.toString()).empty() + override fun getExistPlayerAccount(uuid: UUID): CompletableFuture { + val future = CompletableFuture() + val boolean = loggedTransaction { !Account.select(Account.uuid).where(Account.uuid eq uuid.toString()).empty() } + future.completeAsync { boolean } + return future } override fun getTopBalance(top: Int): MutableMap = loggedTransaction { @@ -49,8 +53,13 @@ class DatabaseEcoModel : PlayerSQLProvider { Account.selectAll().map { UUID.fromString(it[Account.uuid]) }.toMutableList() } - override fun getBalance(uuid: UUID): Double = loggedTransaction { - Account.select(Account.uuid, Account.money).where(Account.uuid eq uuid.toString()).first()[Account.money] + override fun getBalance(uuid: UUID): CompletableFuture { + val future = CompletableFuture() + val balance = loggedTransaction { + Account.select(Account.uuid, Account.money).where(Account.uuid eq uuid.toString()).first()[Account.money] + } + future.completeAsync { balance } + return future } override fun depositMoney(uuid: UUID, money: Double) { diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseMonologModel.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseMonologModel.kt index 4e4fd68..1240200 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseMonologModel.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/common/database/models/DatabaseMonologModel.kt @@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.SortOrder import org.jetbrains.exposed.sql.deleteAll import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.selectAll +import java.util.concurrent.CompletableFuture import java.util.logging.Level class DatabaseMonologModel(val plugin: Plugin) : AdapterLogger { @@ -28,10 +29,15 @@ class DatabaseMonologModel(val plugin: Plugin) : AdapterLogger { loggedTransaction { MonologTable.deleteAll() } } - override fun getLog(): List { - return loggedTransaction { MonologTable.selectAll().orderBy(MonologTable.timestamp, SortOrder.DESC).mapNotNull { - EconomyLog(it[MonologTable.level], it[MonologTable.log], it[MonologTable.timestamp]) - } } + override fun getLog(): CompletableFuture> { + val future = CompletableFuture>() + val log = loggedTransaction { + MonologTable.selectAll().orderBy(MonologTable.timestamp, SortOrder.DESC).mapNotNull { + EconomyLog(it[MonologTable.level], it[MonologTable.log], it[MonologTable.timestamp]) + } + } + future.completeAsync { log } + return future } private fun log(level: Level, message: String) { diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/AccountManageListener.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/AccountManageListener.kt index 0e8bc54..256a609 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/AccountManageListener.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/AccountManageListener.kt @@ -15,7 +15,7 @@ class AccountManageListener(private val liteEco: LiteEco) : Listener { when (event.operationType) { OperationType.CREATE_ACCOUNT -> liteEco.api.createAccount(player, liteEco.config.getDouble("economy.starting_balance")) - OperationType.CACHING_ACCOUNT -> liteEco.api.cacheAccount(player, liteEco.databaseEcoModel.getBalance(player.uniqueId)) + OperationType.CACHING_ACCOUNT -> liteEco.api.cacheAccount(player, liteEco.databaseEcoModel.getBalance(player.uniqueId).join()) OperationType.SYNC_ACCOUNT -> liteEco.api.syncAccount(player) } } diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyDepositListener.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyDepositListener.kt index 039499b..014a4e7 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyDepositListener.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyDepositListener.kt @@ -30,12 +30,6 @@ class EconomyMoneyDepositListener(private val liteEco: LiteEco) : Listener { Placeholder.parsed("account", target.name.toString()) )) - if (sender.name == target.name && !target.isOp) { - return sender.sendMessage( - liteEco.locale.translation("messages.error.self_pay", Placeholder.parsed("money", liteEco.api.fullFormatting(money))) - ) - } - liteEco.increaseTransactions(1) liteEco.api.depositMoney(target, money) liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.deposit") @@ -44,6 +38,12 @@ class EconomyMoneyDepositListener(private val liteEco: LiteEco) : Listener { .replace("", liteEco.api.fullFormatting(money)) ) + if (sender.name == target.name && !target.isOp) { + return sender.sendMessage( + liteEco.locale.translation("messages.error.self_pay", Placeholder.parsed("money", liteEco.api.fullFormatting(money))) + ) + } + sender.sendMessage(liteEco.locale.translation("messages.sender.add_money", TagResolver.resolver( Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money)) diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneySetListener.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneySetListener.kt index 784e8c4..664e3da 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneySetListener.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneySetListener.kt @@ -25,9 +25,6 @@ class EconomyMoneySetListener(private val liteEco: LiteEco) : Listener { liteEco.increaseTransactions(1) - if (sender.name == target.name) - return sender.sendMessage(liteEco.locale.translation("messages.self.set_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))) - liteEco.api.setMoney(target, money) liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.set") .replace("", sender.name) @@ -35,6 +32,9 @@ class EconomyMoneySetListener(private val liteEco: LiteEco) : Listener { .replace("", liteEco.api.fullFormatting(money)) ) + if (sender.name == target.name) + return sender.sendMessage(liteEco.locale.translation("messages.self.set_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))) + sender.sendMessage( liteEco.locale.translation("messages.sender.set_money", TagResolver.resolver(Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money))))) diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyWithdrawListener.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyWithdrawListener.kt index 764b942..8be0aaf 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyWithdrawListener.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/listeners/admin/EconomyMoneyWithdrawListener.kt @@ -25,11 +25,6 @@ class EconomyMoneyWithdrawListener(private val liteEco: LiteEco) : Listener { if (!liteEco.api.has(target, money)) return sender.sendMessage(liteEco.locale.translation("messages.error.insufficient_funds")) - if (sender.name == target.name) - return sender.sendMessage( - liteEco.locale.translation("messages.self.withdraw_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money))) - ) - liteEco.increaseTransactions(1) liteEco.api.withDrawMoney(target, money) liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.withdraw") @@ -38,9 +33,15 @@ class EconomyMoneyWithdrawListener(private val liteEco: LiteEco) : Listener { .replace("", liteEco.api.fullFormatting(money)) ) + if (sender.name == target.name) + return sender.sendMessage( + liteEco.locale.translation("messages.self.withdraw_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money))) + ) + sender.sendMessage( liteEco.locale.translation("messages.sender.withdraw_money", TagResolver.resolver(Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money))))) + if (target.isOnline && liteEco.config.getBoolean("messages.target.notify_withdraw")) { if (silent) { target.player?.sendMessage( diff --git a/src/main/kotlin/com/github/encryptsl/lite/eco/utils/Helper.kt b/src/main/kotlin/com/github/encryptsl/lite/eco/utils/Helper.kt index 526cfae..a050f56 100644 --- a/src/main/kotlin/com/github/encryptsl/lite/eco/utils/Helper.kt +++ b/src/main/kotlin/com/github/encryptsl/lite/eco/utils/Helper.kt @@ -31,8 +31,7 @@ class Helper(private val liteEco: LiteEco) { } fun validateLog(player: String?): List { - val log = liteEco.loggerModel.getLog() - + val log = liteEco.loggerModel.getLog().join() if (player != null) { return log.filter { l -> l.log.contains(player, true) } }