Merge pull request #16 from SKBotNL/master
Add caching
This commit was merged in pull request #16.
This commit is contained in:
21 files changed
+432
-138
No files matched your search
@@ -57,6 +57,7 @@ dependencies {
|
||||
compileOnly("net.luckperms:api:5.5") // Import the LuckPerms API.
|
||||
implementation("io.lettuce:lettuce-core:6.7.1.RELEASE") // Import the Lettuce API for keydb.
|
||||
implementation("com.github.jasync-sql:jasync-postgresql:2.2.4") // Import the jasync Postgres API.
|
||||
implementation("it.unimi.dsi:fastutil-core:8.5.16")
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib") // Import the Kotlin standard library.
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package net.trueog.diamondbankog
|
||||
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
@@ -41,7 +41,7 @@ internal object AutoDeposit {
|
||||
|
||||
scope.launch {
|
||||
transactionLock.withLockSuspend(player.uniqueId) {
|
||||
postgreSQL.addToPlayerShards(player.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(player.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
handleError(player.uniqueId, shards, null)
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package net.trueog.diamondbankog
|
||||
|
||||
import net.trueog.diamondbankog.DiamondBankException.InvalidArgumentException
|
||||
import net.trueog.diamondbankog.PostgreSQL.PlayerShards
|
||||
import net.trueog.diamondbankog.PostgreSQL.ShardType
|
||||
import java.sql.SQLException
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
internal class BalanceManager {
|
||||
val cache = Cache()
|
||||
val postgreSQL = PostgreSQL()
|
||||
val beingModified = ConcurrentHashMap<Pair<UUID, ShardType>, AtomicInteger>()
|
||||
|
||||
@Throws(SQLException::class, ClassNotFoundException::class)
|
||||
fun init() {
|
||||
postgreSQL.initDB()
|
||||
}
|
||||
|
||||
private fun increment(uuid: UUID, type: ShardType) {
|
||||
beingModified.computeIfAbsent(uuid to type) { AtomicInteger(0) }.incrementAndGet()
|
||||
}
|
||||
|
||||
private fun decrement(uuid: UUID, type: ShardType) {
|
||||
beingModified.computeIfPresent(uuid to type) { _, counter ->
|
||||
val newValue = counter.decrementAndGet()
|
||||
if (newValue == 0) null else counter
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setPlayerShards(uuid: UUID, shards: Long, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
|
||||
increment(uuid, type)
|
||||
postgreSQL.setPlayerShards(uuid, shards, type).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.setBalance(uuid, shards, type)
|
||||
decrement(uuid, type)
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
suspend fun addToPlayerShards(uuid: UUID, shards: Long, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
|
||||
increment(uuid, type)
|
||||
postgreSQL.addToPlayerShards(uuid, shards, type).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.addBalance(uuid, shards, type)
|
||||
decrement(uuid, type)
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
suspend fun subtractFromBankShards(uuid: UUID, shards: Long): Result<Unit> =
|
||||
addToPlayerShards(uuid, -shards, ShardType.BANK)
|
||||
|
||||
suspend fun getBankShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.BANK)
|
||||
|
||||
suspend fun getInventoryShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.INVENTORY)
|
||||
|
||||
suspend fun getEnderChestShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.ENDER_CHEST)
|
||||
|
||||
suspend fun getShardTypeShards(uuid: UUID, type: ShardType): Result<Long> {
|
||||
if ((beingModified[uuid to type]?.get() ?: 0) > 0) {
|
||||
return postgreSQL.getShardTypeShards(uuid, type)
|
||||
}
|
||||
val cacheBalance = cache.getBalance(uuid, type)
|
||||
if (cacheBalance == -1L) {
|
||||
increment(uuid, type)
|
||||
val dbBalance =
|
||||
postgreSQL.getShardTypeShards(uuid, type).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.setBalance(uuid, dbBalance, type)
|
||||
decrement(uuid, type)
|
||||
return Result.success(dbBalance)
|
||||
}
|
||||
return Result.success(cacheBalance)
|
||||
}
|
||||
|
||||
suspend fun getTotalShards(uuid: UUID): Result<Long> {
|
||||
val anyBeingModified = beingModified.any {
|
||||
it.key.first == uuid && it.value.get() > 0
|
||||
}
|
||||
if (anyBeingModified) {
|
||||
return postgreSQL.getTotalShards(uuid)
|
||||
}
|
||||
val cacheBalance = cache.getBalance(uuid, ShardType.TOTAL)
|
||||
if (cacheBalance == -1L) {
|
||||
increment(uuid, ShardType.TOTAL)
|
||||
val dbBalance =
|
||||
postgreSQL.getTotalShards(uuid).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.setBalance(uuid, dbBalance, ShardType.TOTAL)
|
||||
decrement(uuid, ShardType.TOTAL)
|
||||
return Result.success(dbBalance)
|
||||
}
|
||||
return Result.success(cacheBalance)
|
||||
}
|
||||
|
||||
suspend fun getAllShards(uuid: UUID): Result<PlayerShards> {
|
||||
val anyBeingModified = beingModified.any {
|
||||
it.key.first == uuid && it.value.get() > 0
|
||||
}
|
||||
if (anyBeingModified) {
|
||||
return postgreSQL.getAllShards(uuid)
|
||||
}
|
||||
val cacheBankBalance = cache.getBalance(uuid, ShardType.BANK)
|
||||
val cacheInventoryBalance = cache.getBalance(uuid, ShardType.INVENTORY)
|
||||
val cacheEnderChestBalance = cache.getBalance(uuid, ShardType.ENDER_CHEST)
|
||||
if (cacheBankBalance == -1L || cacheInventoryBalance == -1L || cacheEnderChestBalance == -1L) {
|
||||
increment(uuid, ShardType.TOTAL)
|
||||
val dbPlayerShards =
|
||||
postgreSQL.getAllShards(uuid).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.setBalance(uuid, dbPlayerShards.bank, ShardType.BANK)
|
||||
cache.setBalance(uuid, dbPlayerShards.inventory, ShardType.INVENTORY)
|
||||
cache.setBalance(uuid, dbPlayerShards.enderChest, ShardType.ENDER_CHEST)
|
||||
decrement(uuid, ShardType.TOTAL)
|
||||
return Result.success(dbPlayerShards)
|
||||
}
|
||||
return Result.success(PlayerShards(cacheBankBalance, cacheInventoryBalance, cacheEnderChestBalance))
|
||||
}
|
||||
|
||||
suspend fun getBaltop(offset: Int): Result<Map<UUID?, Long>> = postgreSQL.getBaltop(offset)
|
||||
|
||||
suspend fun getBaltopWithUuid(uuid: UUID): Result<Pair<Map<UUID?, Long>, Long>> = postgreSQL.getBaltopWithUuid(uuid)
|
||||
|
||||
suspend fun getNumberOfRows(): Result<Long> = postgreSQL.getNumberOfRows()
|
||||
|
||||
suspend fun insertTransactionLog(
|
||||
playerUuid: UUID,
|
||||
transferredShards: Long,
|
||||
playerToUuid: UUID?,
|
||||
transactionReason: String,
|
||||
notes: String?,
|
||||
): Result<Unit> =
|
||||
postgreSQL.insertTransactionLog(playerUuid, transferredShards, playerToUuid, transactionReason, notes)
|
||||
|
||||
suspend fun hasEntry(uuid: UUID) = postgreSQL.hasEntry(uuid)
|
||||
|
||||
suspend fun cacheForPlayer(uuid: UUID): Result<Unit> {
|
||||
val playerShards =
|
||||
postgreSQL.getAllShards(uuid).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
cache.setBalance(uuid, playerShards.bank, ShardType.BANK)
|
||||
cache.setBalance(uuid, playerShards.inventory, ShardType.INVENTORY)
|
||||
cache.setBalance(uuid, playerShards.enderChest, ShardType.ENDER_CHEST)
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
fun removeCacheForPlayer(uuid: UUID) {
|
||||
cache.removeAll(uuid)
|
||||
}
|
||||
|
||||
fun shutdown() {
|
||||
postgreSQL.pool.disconnect().get()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package net.trueog.diamondbankog
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import kotlin.concurrent.read
|
||||
import kotlin.concurrent.write
|
||||
import net.trueog.diamondbankog.DiamondBankException.InvalidArgumentException
|
||||
import net.trueog.diamondbankog.PostgreSQL.ShardType
|
||||
|
||||
internal class Cache {
|
||||
val bankBalanceCache = Object2LongOpenHashMap<UUID>().apply { defaultReturnValue(-1L) }
|
||||
val bankBalanceCacheLock = ReentrantReadWriteLock()
|
||||
val inventoryBalanceCache = Object2LongOpenHashMap<UUID>().apply { defaultReturnValue(-1L) }
|
||||
val inventoryBalanceCacheLock = ReentrantReadWriteLock()
|
||||
val enderChestBalanceCache = Object2LongOpenHashMap<UUID>().apply { defaultReturnValue(-1L) }
|
||||
val enderChestBalanceCacheLock = ReentrantReadWriteLock()
|
||||
|
||||
fun setBalance(uuid: UUID, value: Long, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
when (type) {
|
||||
ShardType.BANK -> {
|
||||
bankBalanceCacheLock.write { bankBalanceCache.put(uuid, value) }
|
||||
}
|
||||
ShardType.INVENTORY -> {
|
||||
inventoryBalanceCacheLock.write { inventoryBalanceCache.put(uuid, value) }
|
||||
}
|
||||
ShardType.ENDER_CHEST -> {
|
||||
enderChestBalanceCacheLock.write { enderChestBalanceCache.put(uuid, value) }
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
fun addBalance(uuid: UUID, value: Long, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
when (type) {
|
||||
ShardType.BANK -> {
|
||||
bankBalanceCacheLock.write { bankBalanceCache.addTo(uuid, value) }
|
||||
}
|
||||
ShardType.INVENTORY -> {
|
||||
inventoryBalanceCacheLock.write { inventoryBalanceCache.addTo(uuid, value) }
|
||||
}
|
||||
ShardType.ENDER_CHEST -> {
|
||||
enderChestBalanceCacheLock.write { enderChestBalanceCache.addTo(uuid, value) }
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
fun getBalance(uuid: UUID, type: ShardType): Long {
|
||||
return when (type) {
|
||||
ShardType.BANK -> {
|
||||
bankBalanceCacheLock.read { bankBalanceCache.getLong(uuid) }
|
||||
}
|
||||
ShardType.INVENTORY -> {
|
||||
inventoryBalanceCacheLock.read { inventoryBalanceCache.getLong(uuid) }
|
||||
}
|
||||
ShardType.ENDER_CHEST -> {
|
||||
enderChestBalanceCacheLock.read { enderChestBalanceCache.getLong(uuid) }
|
||||
}
|
||||
ShardType.TOTAL -> {
|
||||
bankBalanceCacheLock.read {
|
||||
inventoryBalanceCacheLock.read {
|
||||
enderChestBalanceCacheLock.read {
|
||||
bankBalanceCache.getLong(uuid) +
|
||||
inventoryBalanceCache.getLong(uuid) +
|
||||
enderChestBalanceCache.getLong(uuid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeBalance(uuid: UUID, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
when (type) {
|
||||
ShardType.BANK -> {
|
||||
bankBalanceCacheLock.write { bankBalanceCache.removeLong(uuid) }
|
||||
}
|
||||
ShardType.INVENTORY -> {
|
||||
inventoryBalanceCacheLock.write { inventoryBalanceCache.removeLong(uuid) }
|
||||
}
|
||||
ShardType.ENDER_CHEST -> {
|
||||
enderChestBalanceCacheLock.write { enderChestBalanceCache.removeLong(uuid) }
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
fun removeAll(uuid: UUID) {
|
||||
bankBalanceCacheLock.write {
|
||||
inventoryBalanceCacheLock.write {
|
||||
enderChestBalanceCacheLock.write {
|
||||
bankBalanceCache.removeLong(uuid)
|
||||
inventoryBalanceCache.removeLong(uuid)
|
||||
enderChestBalanceCache.removeLong(uuid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,10 +18,8 @@ internal class Config private constructor() {
|
||||
companion object {
|
||||
fun create(): Config? {
|
||||
val config = Config()
|
||||
plugin.saveDefaultConfig()
|
||||
val file = File(plugin.dataFolder, "config.yml")
|
||||
if (!file.exists()) {
|
||||
plugin.saveDefaultConfig()
|
||||
}
|
||||
val yamlConfig = YamlConfiguration.loadConfiguration(file)
|
||||
|
||||
try {
|
||||
|
||||
@@ -4,9 +4,9 @@ import java.util.*
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.trueog.diamondbankog.DiamondBankException.*
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.eventManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
import net.trueog.diamondbankog.PostgreSQL.PlayerShards
|
||||
@@ -33,9 +33,9 @@ class DiamondBankAPIJava() {
|
||||
|
||||
return runBlocking {
|
||||
transactionLock.withLockSuspend(uuid) {
|
||||
postgreSQL.addToPlayerShards(uuid, shards, ShardType.BANK).getOrElse { throw it }
|
||||
balanceManager.addToPlayerShards(uuid, shards, ShardType.BANK).getOrElse { throw it }
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
}
|
||||
@@ -61,9 +61,9 @@ class DiamondBankAPIJava() {
|
||||
|
||||
return runBlocking {
|
||||
transactionLock.withLockSuspend(uuid) {
|
||||
postgreSQL.subtractFromBankShards(uuid, shards).getOrElse { throw it }
|
||||
balanceManager.subtractFromBankShards(uuid, shards).getOrElse { throw it }
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ class DiamondBankAPIJava() {
|
||||
|
||||
return runBlocking {
|
||||
transactionLock.withLockSuspend(uuid) {
|
||||
val result = postgreSQL.getAllShards(uuid)
|
||||
val result = balanceManager.getAllShards(uuid)
|
||||
result.getOrThrow()
|
||||
}
|
||||
}
|
||||
@@ -152,10 +152,10 @@ class DiamondBankAPIJava() {
|
||||
val result =
|
||||
transactionLock.withLockSuspend(uuid) {
|
||||
when (type) {
|
||||
ShardType.BANK -> postgreSQL.getBankShards(uuid)
|
||||
ShardType.INVENTORY -> postgreSQL.getInventoryShards(uuid)
|
||||
ShardType.ENDER_CHEST -> postgreSQL.getEnderChestShards(uuid)
|
||||
ShardType.TOTAL -> postgreSQL.getTotalShards(uuid)
|
||||
ShardType.BANK -> balanceManager.getBankShards(uuid)
|
||||
ShardType.INVENTORY -> balanceManager.getInventoryShards(uuid)
|
||||
ShardType.ENDER_CHEST -> balanceManager.getEnderChestShards(uuid)
|
||||
ShardType.TOTAL -> balanceManager.getTotalShards(uuid)
|
||||
}
|
||||
}
|
||||
result.getOrThrow()
|
||||
@@ -176,7 +176,7 @@ class DiamondBankAPIJava() {
|
||||
fun getBaltop(offset: Int): Map<UUID?, Long> {
|
||||
if (economyDisabled) throw EconomyDisabledException()
|
||||
|
||||
return runBlocking { postgreSQL.getBaltop(offset).getOrElse { throw it } }
|
||||
return runBlocking { balanceManager.getBaltop(offset).getOrElse { throw it } }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +213,7 @@ class DiamondBankAPIJava() {
|
||||
if (!player.isOnline) throw PlayerNotOnlineException()
|
||||
val playerPlayer = player.player ?: throw InvalidPlayerException()
|
||||
|
||||
val balance = postgreSQL.getTotalShards(uuid).getOrElse { throw it }
|
||||
val balance = balanceManager.getTotalShards(uuid).getOrElse { throw it }
|
||||
if (balance - shards < 0) {
|
||||
throw InsufficientBalanceException(balance)
|
||||
}
|
||||
@@ -223,7 +223,7 @@ class DiamondBankAPIJava() {
|
||||
throw it
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
}
|
||||
@@ -269,7 +269,7 @@ class DiamondBankAPIJava() {
|
||||
val receiver = Bukkit.getPlayer(receiverUuid) ?: Bukkit.getOfflinePlayer(receiverUuid)
|
||||
if (!receiver.hasPlayedBefore()) throw InvalidPlayerException()
|
||||
|
||||
val balance = postgreSQL.getTotalShards(payerUuid).getOrElse { throw it }
|
||||
val balance = balanceManager.getTotalShards(payerUuid).getOrElse { throw it }
|
||||
if (balance - shards < 0) {
|
||||
throw InsufficientBalanceException(balance)
|
||||
}
|
||||
@@ -279,14 +279,14 @@ class DiamondBankAPIJava() {
|
||||
throw it
|
||||
}
|
||||
|
||||
postgreSQL.addToPlayerShards(receiverUuid, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(receiverUuid, shards, ShardType.BANK).getOrElse {
|
||||
handleError(payerUuid, shards, null)
|
||||
throw it
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(payerUuid, shards, receiverUuid, transactionReason, notes).getOrElse {
|
||||
handleError(payerUuid, shards, null, receiverUuid, true)
|
||||
}
|
||||
balanceManager
|
||||
.insertTransactionLog(payerUuid, shards, receiverUuid, transactionReason, notes)
|
||||
.getOrElse { handleError(payerUuid, shards, null, receiverUuid, true) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.trueog.diamondbankog
|
||||
import java.util.*
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import net.trueog.diamondbankog.DiamondBankException.*
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.eventManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
import net.trueog.diamondbankog.PostgreSQL.PlayerShards
|
||||
@@ -30,11 +30,11 @@ class DiamondBankAPIKotlin() {
|
||||
if (economyDisabled) return Result.failure(EconomyDisabledException())
|
||||
|
||||
return transactionLock.withLockSuspend(uuid) {
|
||||
postgreSQL.addToPlayerShards(uuid, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(uuid, shards, ShardType.BANK).getOrElse {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
|
||||
@@ -58,11 +58,11 @@ class DiamondBankAPIKotlin() {
|
||||
if (economyDisabled) return Result.failure(EconomyDisabledException())
|
||||
|
||||
return transactionLock.withLockSuspend(uuid) {
|
||||
postgreSQL.subtractFromBankShards(uuid, shards).getOrElse {
|
||||
balanceManager.subtractFromBankShards(uuid, shards).getOrElse {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ class DiamondBankAPIKotlin() {
|
||||
suspend fun getAllShards(uuid: UUID): Result<PlayerShards> {
|
||||
if (economyDisabled) return Result.failure(EconomyDisabledException())
|
||||
|
||||
return transactionLock.withLockSuspend(uuid) { postgreSQL.getAllShards(uuid) }
|
||||
return transactionLock.withLockSuspend(uuid) { balanceManager.getAllShards(uuid) }
|
||||
}
|
||||
|
||||
private suspend fun getShardTypeShards(uuid: UUID, type: ShardType): Result<Long> {
|
||||
@@ -98,10 +98,10 @@ class DiamondBankAPIKotlin() {
|
||||
return transactionLock.withLockSuspend(uuid) {
|
||||
val result =
|
||||
when (type) {
|
||||
ShardType.BANK -> postgreSQL.getBankShards(uuid)
|
||||
ShardType.INVENTORY -> postgreSQL.getInventoryShards(uuid)
|
||||
ShardType.ENDER_CHEST -> postgreSQL.getEnderChestShards(uuid)
|
||||
ShardType.TOTAL -> postgreSQL.getTotalShards(uuid)
|
||||
ShardType.BANK -> balanceManager.getBankShards(uuid)
|
||||
ShardType.INVENTORY -> balanceManager.getInventoryShards(uuid)
|
||||
ShardType.ENDER_CHEST -> balanceManager.getEnderChestShards(uuid)
|
||||
ShardType.TOTAL -> balanceManager.getTotalShards(uuid)
|
||||
}.getOrElse {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
@@ -114,7 +114,7 @@ class DiamondBankAPIKotlin() {
|
||||
if (economyDisabled) return Result.failure(EconomyDisabledException())
|
||||
|
||||
val baltop =
|
||||
postgreSQL.getBaltop(offset).getOrElse {
|
||||
balanceManager.getBaltop(offset).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
return Result.success(baltop)
|
||||
@@ -137,7 +137,7 @@ class DiamondBankAPIKotlin() {
|
||||
val playerPlayer = player.player ?: return@withLockSuspend Result.failure(InvalidPlayerException())
|
||||
|
||||
val balance =
|
||||
postgreSQL.getTotalShards(uuid).getOrElse {
|
||||
balanceManager.getTotalShards(uuid).getOrElse {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
if (balance - shards < 0) {
|
||||
@@ -149,7 +149,7 @@ class DiamondBankAPIKotlin() {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(uuid, shards, null, transactionReason, notes).getOrElse {
|
||||
handleError(uuid, shards, null, null, true)
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ class DiamondBankAPIKotlin() {
|
||||
if (!receiver.hasPlayedBefore()) return@withLockSuspend Result.failure(InvalidPlayerException())
|
||||
|
||||
val balance =
|
||||
postgreSQL.getTotalShards(payerUuid).getOrElse {
|
||||
balanceManager.getTotalShards(payerUuid).getOrElse {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
if (balance - shards < 0) {
|
||||
@@ -199,12 +199,12 @@ class DiamondBankAPIKotlin() {
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
|
||||
postgreSQL.addToPlayerShards(receiverUuid, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(receiverUuid, shards, ShardType.BANK).getOrElse {
|
||||
handleError(payerUuid, shards, null)
|
||||
return@withLockSuspend Result.failure(it)
|
||||
}
|
||||
|
||||
postgreSQL.insertTransactionLog(payerUuid, shards, receiverUuid, transactionReason, notes).getOrElse {
|
||||
balanceManager.insertTransactionLog(payerUuid, shards, receiverUuid, transactionReason, notes).getOrElse {
|
||||
handleError(payerUuid, shards, null, receiverUuid, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,4 +16,6 @@ sealed class DiamondBankException(message: String) : Exception(message) {
|
||||
class CouldNotRemoveEnoughException(val notRemoved: Int) : Exception("Could not remove enough")
|
||||
|
||||
class OtherException : DiamondBankException("Other exception")
|
||||
|
||||
class InvalidArgumentException : Exception()
|
||||
}
|
||||
@@ -19,13 +19,13 @@ internal class DiamondBankOG : JavaPlugin() {
|
||||
lateinit var plugin: DiamondBankOG
|
||||
lateinit var config: Config
|
||||
lateinit var eventManager: EventManager
|
||||
lateinit var postgreSQL: PostgreSQL
|
||||
lateinit var balanceManager: BalanceManager
|
||||
lateinit var redis: Redis
|
||||
lateinit var luckPerms: LuckPerms
|
||||
|
||||
fun isRedisInitialized() = ::redis.isInitialized
|
||||
|
||||
fun isPostgreSQLInitialized() = ::postgreSQL.isInitialized
|
||||
fun isBalanceManagerInitialized() = ::balanceManager.isInitialized
|
||||
|
||||
var mm =
|
||||
MiniMessage.builder()
|
||||
@@ -55,11 +55,11 @@ internal class DiamondBankOG : JavaPlugin() {
|
||||
|
||||
eventManager = EventManager()
|
||||
|
||||
postgreSQL = PostgreSQL()
|
||||
balanceManager = BalanceManager()
|
||||
try {
|
||||
postgreSQL.initDB()
|
||||
balanceManager.init()
|
||||
} catch (e: Exception) {
|
||||
logger.info(e.toString())
|
||||
e.printStackTrace()
|
||||
Bukkit.getPluginManager().disablePlugin(this)
|
||||
return
|
||||
}
|
||||
@@ -124,8 +124,8 @@ internal class DiamondBankOG : JavaPlugin() {
|
||||
redis.shutdown()
|
||||
}
|
||||
|
||||
if (isPostgreSQLInitialized()) {
|
||||
postgreSQL.pool.disconnect().get()
|
||||
if (isBalanceManagerInitialized()) {
|
||||
balanceManager.shutdown()
|
||||
}
|
||||
|
||||
scope.cancel()
|
||||
|
||||
@@ -22,7 +22,7 @@ internal object ErrorHandler {
|
||||
|
||||
Player UUID: $uuid
|
||||
${
|
||||
if (otherUuid != null) "Other Player UUID: $otherUuid" else ""
|
||||
if (otherUuid != null) "Other Player UUID: $otherUuid\n" else ""
|
||||
}Expected Mutated Shards = $expectedMutatedShards${
|
||||
if (playerShards != null) {
|
||||
if (playerShards.bank != -1L) "Player Bank Balance: ${playerShards.bank}" else ""
|
||||
|
||||
@@ -4,10 +4,10 @@ import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.AutoCompress.compress
|
||||
import net.trueog.diamondbankog.AutoDeposit.deposit
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.redis
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
@@ -30,12 +30,13 @@ import org.bukkit.event.player.PlayerDropItemEvent
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent
|
||||
import org.bukkit.event.player.PlayerInteractEvent
|
||||
import org.bukkit.event.player.PlayerJoinEvent
|
||||
import org.bukkit.event.player.PlayerQuitEvent
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
internal class Events : Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
fun onPlayerJoin(event: PlayerJoinEvent) {
|
||||
if (economyDisabled) {
|
||||
event.player.sendMessage(
|
||||
@@ -49,7 +50,7 @@ internal class Events : Listener {
|
||||
|
||||
scope.launch {
|
||||
val hasEntry =
|
||||
postgreSQL.hasEntry(event.player.uniqueId).getOrElse {
|
||||
balanceManager.hasEntry(event.player.uniqueId).getOrElse {
|
||||
economyDisabled = true
|
||||
return@launch
|
||||
}
|
||||
@@ -60,7 +61,7 @@ internal class Events : Listener {
|
||||
PersistentDataType.DOUBLE,
|
||||
)
|
||||
if (legacyBalance != null) {
|
||||
postgreSQL
|
||||
balanceManager
|
||||
.setPlayerShards(event.player.uniqueId, legacyBalance.toLong() * 9, ShardType.BANK)
|
||||
.getOrElse {
|
||||
handleError(event.player.uniqueId, legacyBalance.toLong() * 9, null)
|
||||
@@ -75,16 +76,31 @@ internal class Events : Listener {
|
||||
)
|
||||
}
|
||||
|
||||
val inventoryShards = event.player.inventory.countTotal()
|
||||
postgreSQL.setPlayerShards(event.player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(event.player.uniqueId, inventoryShards, null)
|
||||
return@launch
|
||||
}
|
||||
transactionLock.withLockSuspend(event.player.uniqueId) {
|
||||
val inventoryShards = event.player.inventory.countTotal()
|
||||
balanceManager.setPlayerShards(event.player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(event.player.uniqueId, inventoryShards, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
|
||||
val enderChestDiamonds = event.player.enderChest.countTotal()
|
||||
postgreSQL.setPlayerShards(event.player.uniqueId, enderChestDiamonds, ShardType.ENDER_CHEST).getOrElse {
|
||||
handleError(event.player.uniqueId, enderChestDiamonds, null)
|
||||
return@launch
|
||||
val enderChestDiamonds = event.player.enderChest.countTotal()
|
||||
balanceManager
|
||||
.setPlayerShards(event.player.uniqueId, enderChestDiamonds, ShardType.ENDER_CHEST)
|
||||
.getOrElse {
|
||||
handleError(event.player.uniqueId, enderChestDiamonds, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
|
||||
balanceManager.cacheForPlayer(event.player.uniqueId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
fun onPlayerQuit(event: PlayerQuitEvent) {
|
||||
scope.launch {
|
||||
transactionLock.withLockSuspend(event.player.uniqueId) {
|
||||
balanceManager.removeCacheForPlayer(event.player.uniqueId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,7 +150,7 @@ internal class Events : Listener {
|
||||
transactionLock.withLockSuspend(player.uniqueId) {
|
||||
val inventoryShards = runOnMainThread { player.inventory.countTotal() }
|
||||
|
||||
postgreSQL.setPlayerShards(player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
balanceManager.setPlayerShards(player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(player.uniqueId, inventoryShards, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
@@ -176,7 +192,7 @@ internal class Events : Listener {
|
||||
scope.launch {
|
||||
transactionLock.withLockSuspend(event.player.uniqueId) {
|
||||
val inventoryShards = runOnMainThread { event.player.inventory.countTotal() }
|
||||
postgreSQL.setPlayerShards(event.player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
balanceManager.setPlayerShards(event.player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(event.player.uniqueId, inventoryShards, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
@@ -339,13 +355,13 @@ internal class Events : Listener {
|
||||
else null,
|
||||
)
|
||||
}
|
||||
postgreSQL.setPlayerShards(player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
balanceManager.setPlayerShards(player.uniqueId, inventoryShards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(player.uniqueId, inventoryShards, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
|
||||
if (enderChestShards == null) return@withLockSuspend
|
||||
postgreSQL.setPlayerShards(player.uniqueId, enderChestShards, ShardType.ENDER_CHEST).getOrElse {
|
||||
balanceManager.setPlayerShards(player.uniqueId, enderChestShards, ShardType.ENDER_CHEST).getOrElse {
|
||||
handleError(player.uniqueId, enderChestShards, null)
|
||||
return@withLockSuspend
|
||||
}
|
||||
@@ -354,7 +370,7 @@ internal class Events : Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
fun onPrepareItemCraft(event: PrepareItemCraftEvent) {
|
||||
val resultType = event.recipe?.result?.type
|
||||
if (
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.trueog.diamondbankog
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
@@ -87,7 +87,7 @@ internal object InventoryExtensions {
|
||||
|
||||
val leftOver = addMap[0]!!.amount.toLong()
|
||||
|
||||
val result = postgreSQL.addToPlayerShards(player.uniqueId, leftOver, ShardType.BANK)
|
||||
val result = balanceManager.addToPlayerShards(player.uniqueId, leftOver, ShardType.BANK)
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: The change of $leftOver <aqua>Diamond ${if (leftOver == 1L) "Shard" else "Shards"} <reset>has been deposited into your bank."
|
||||
@@ -111,7 +111,7 @@ internal object InventoryExtensions {
|
||||
|
||||
if (inventoryAddMap.isNotEmpty()) {
|
||||
val inventoryLeftOver = inventoryAddMap[0]!!.amount
|
||||
postgreSQL.addToPlayerShards(player.uniqueId, inventoryLeftOver.toLong(), ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(player.uniqueId, inventoryLeftOver.toLong(), ShardType.BANK).getOrElse {
|
||||
return true
|
||||
}
|
||||
player.sendMessage(
|
||||
@@ -201,7 +201,7 @@ internal object InventoryExtensions {
|
||||
scope.launch {
|
||||
transactionLock.withLockSuspend(player.uniqueId) {
|
||||
val inventoryShards = player.inventory.countTotal()
|
||||
postgreSQL
|
||||
balanceManager
|
||||
.setPlayerShards(player.uniqueId, inventoryShards, ShardType.INVENTORY)
|
||||
.getOrElse {
|
||||
handleError(player.uniqueId, inventoryShards, null)
|
||||
|
||||
@@ -9,6 +9,8 @@ import java.util.*
|
||||
import kotlinx.coroutines.future.await
|
||||
import net.trueog.diamondbankog.DiamondBankException.DatabaseException
|
||||
import net.trueog.diamondbankog.DiamondBankException.InsufficientBalanceException
|
||||
import net.trueog.diamondbankog.DiamondBankException.InvalidArgumentException
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.plugin
|
||||
@@ -16,8 +18,6 @@ import net.trueog.diamondbankog.DiamondBankOG.Companion.plugin
|
||||
class PostgreSQL {
|
||||
lateinit var pool: ConnectionPool<PostgreSQLConnection>
|
||||
|
||||
class InvalidArgumentException : Exception()
|
||||
|
||||
class NoRowsException : Exception()
|
||||
|
||||
enum class ShardType(val string: String) {
|
||||
@@ -36,7 +36,7 @@ class PostgreSQL {
|
||||
)
|
||||
val createTable =
|
||||
pool.sendPreparedStatement(
|
||||
"CREATE TABLE IF NOT EXISTS ${config.postgresTable}(uuid UUID PRIMARY KEY, bank_shards BIGINT, inventory_shards BIGINT, ender_chest_shards BIGINT, total_shards BIGINT GENERATED ALWAYS AS ( COALESCE(bank_shards, 0) + COALESCE(inventory_shards, 0) + COALESCE(ender_chest_shards, 0) ) STORED)"
|
||||
"CREATE TABLE IF NOT EXISTS ${config.postgresTable}(uuid UUID PRIMARY KEY, bank_shards BIGINT DEFAULT 0, inventory_shards BIGINT DEFAULT 0, ender_chest_shards BIGINT DEFAULT 0, total_shards BIGINT GENERATED ALWAYS AS ( COALESCE(bank_shards, 0) + COALESCE(inventory_shards, 0) + COALESCE(ender_chest_shards, 0) ) STORED)"
|
||||
)
|
||||
createTable.join()
|
||||
val createTotalShardsIndex =
|
||||
@@ -125,41 +125,46 @@ class PostgreSQL {
|
||||
|
||||
suspend fun addToPlayerShards(uuid: UUID, shards: Long, type: ShardType): Result<Unit> {
|
||||
if (type == ShardType.TOTAL) return Result.failure(InvalidArgumentException())
|
||||
val playerShards: PlayerShards
|
||||
|
||||
val playerShards =
|
||||
when (type) {
|
||||
ShardType.BANK -> getBankShards(uuid)
|
||||
ShardType.INVENTORY -> getInventoryShards(uuid)
|
||||
ShardType.ENDER_CHEST -> getEnderChestShards(uuid)
|
||||
else -> {
|
||||
return Result.failure(InvalidArgumentException())
|
||||
}
|
||||
}.getOrElse {
|
||||
return Result.failure(it)
|
||||
try {
|
||||
val connection = pool.asSuspending.connect()
|
||||
|
||||
val preparedStatement =
|
||||
connection.sendPreparedStatement(
|
||||
"INSERT INTO ${config.postgresTable}(uuid, ${type.string}) VALUES(?, ?) ON CONFLICT (uuid) DO UPDATE SET ${type.string} = ${config.postgresTable}.${type.string} + excluded.${type.string} " +
|
||||
"WHERE ${config.postgresTable}.${type.string} + excluded.${type.string} >= 0 RETURNING bank_shards, inventory_shards, ender_chest_shards",
|
||||
listOf(uuid, shards),
|
||||
)
|
||||
val result = preparedStatement.await()
|
||||
|
||||
if (result.rows.isEmpty()) {
|
||||
val shards =
|
||||
balanceManager.getShardTypeShards(uuid, type).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
throw InsufficientBalanceException(shards)
|
||||
}
|
||||
val row = result.rows[0]
|
||||
val bankShards = row.getLong("bank_shards")
|
||||
val inventoryShards = row.getLong("inventory_shards")
|
||||
val enderChestShards = row.getLong("ender_chest_shards")
|
||||
if (bankShards == null || inventoryShards == null || enderChestShards == null) {
|
||||
throw Exception()
|
||||
}
|
||||
|
||||
return setPlayerShards(uuid, playerShards + shards, type)
|
||||
}
|
||||
|
||||
suspend fun subtractFromBankShards(uuid: UUID, shards: Long): Result<Unit> {
|
||||
val bankShards =
|
||||
getBankShards(uuid).getOrElse {
|
||||
return Result.failure(it)
|
||||
playerShards = PlayerShards(bankShards, inventoryShards, enderChestShards)
|
||||
} catch (e: Exception) {
|
||||
if (e is InsufficientBalanceException) {
|
||||
return Result.failure(e)
|
||||
}
|
||||
val newBalance = bankShards - shards
|
||||
if (newBalance < 0) {
|
||||
return Result.failure(InsufficientBalanceException(bankShards))
|
||||
return Result.failure(DatabaseException(e.message ?: "Database exception"))
|
||||
}
|
||||
|
||||
return setPlayerShards(uuid, newBalance, ShardType.BANK)
|
||||
DiamondBankOG.eventManager.sendUpdate(uuid, playerShards)
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
suspend fun getBankShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.BANK)
|
||||
|
||||
suspend fun getInventoryShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.INVENTORY)
|
||||
|
||||
suspend fun getEnderChestShards(uuid: UUID) = getShardTypeShards(uuid, ShardType.ENDER_CHEST)
|
||||
|
||||
suspend fun getTotalShards(uuid: UUID): Result<Long> {
|
||||
var totalShards: Long?
|
||||
try {
|
||||
@@ -219,7 +224,7 @@ class PostgreSQL {
|
||||
return Result.success(PlayerShards(bankShards, inventoryShards, enderChestShards))
|
||||
}
|
||||
|
||||
private suspend fun getShardTypeShards(uuid: UUID, type: ShardType): Result<Long> {
|
||||
suspend fun getShardTypeShards(uuid: UUID, type: ShardType): Result<Long> {
|
||||
var shards: Long?
|
||||
try {
|
||||
val connection = pool.asSuspending.connect()
|
||||
|
||||
@@ -3,22 +3,22 @@ package net.trueog.diamondbankog
|
||||
import kotlin.math.floor
|
||||
import net.trueog.diamondbankog.DiamondBankException.CouldNotRemoveEnoughException
|
||||
import net.trueog.diamondbankog.DiamondBankException.OtherException
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.InventoryExtensions.withdraw
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
internal object WithdrawHelper {
|
||||
suspend fun withdrawFromPlayer(player: Player, shards: Long): Result<Unit> {
|
||||
val playerShards =
|
||||
postgreSQL.getAllShards(player.uniqueId).getOrElse {
|
||||
balanceManager.getAllShards(player.uniqueId).getOrElse {
|
||||
return Result.failure(it)
|
||||
}
|
||||
|
||||
// Withdraw everything
|
||||
if (shards == -1L) {
|
||||
postgreSQL.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
balanceManager.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>A severe error has occurred. Please notify a staff member."
|
||||
@@ -52,7 +52,7 @@ internal object WithdrawHelper {
|
||||
}
|
||||
|
||||
if (shards <= playerShards.bank) {
|
||||
postgreSQL.subtractFromBankShards(player.uniqueId, shards).getOrElse {
|
||||
balanceManager.subtractFromBankShards(player.uniqueId, shards).getOrElse {
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>A severe error has occurred. Please notify a staff member."
|
||||
@@ -64,13 +64,15 @@ internal object WithdrawHelper {
|
||||
}
|
||||
|
||||
if (shards <= playerShards.bank + playerShards.inventory) {
|
||||
postgreSQL.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>A severe error has occurred. Please notify a staff member."
|
||||
if (playerShards.bank != 0L) {
|
||||
balanceManager.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>A severe error has occurred. Please notify a staff member."
|
||||
)
|
||||
)
|
||||
)
|
||||
return Result.failure(it)
|
||||
return Result.failure(it)
|
||||
}
|
||||
}
|
||||
|
||||
val notRemoved = player.inventory.withdraw((shards - playerShards.bank).toInt())
|
||||
@@ -81,7 +83,7 @@ internal object WithdrawHelper {
|
||||
}
|
||||
}
|
||||
|
||||
postgreSQL.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
balanceManager.subtractFromBankShards(player.uniqueId, playerShards.bank).getOrElse {
|
||||
player.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>A severe error has occurred. Please notify a staff member."
|
||||
|
||||
@@ -4,10 +4,10 @@ import java.util.*
|
||||
import kotlin.math.floor
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
|
||||
import org.bukkit.Bukkit
|
||||
@@ -60,7 +60,7 @@ internal class Balance : CommandExecutor {
|
||||
}
|
||||
|
||||
val balance =
|
||||
postgreSQL.getAllShards(otherPlayer.uniqueId).getOrElse {
|
||||
balanceManager.getAllShards(otherPlayer.uniqueId).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get their balance."
|
||||
@@ -113,7 +113,7 @@ internal class Balance : CommandExecutor {
|
||||
otherPlayer
|
||||
}
|
||||
val balance =
|
||||
postgreSQL.getAllShards(balancePlayer.uniqueId).getOrElse {
|
||||
balanceManager.getAllShards(balancePlayer.uniqueId).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get ${
|
||||
|
||||
@@ -5,10 +5,10 @@ import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
|
||||
import org.bukkit.Bukkit
|
||||
@@ -75,7 +75,7 @@ internal class Balancetop : CommandExecutor {
|
||||
|
||||
if (player != null) {
|
||||
val baltopWithUuid =
|
||||
postgreSQL.getBaltopWithUuid(player.uniqueId).getOrElse {
|
||||
balanceManager.getBaltopWithUuid(player.uniqueId).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get the information for balancetop."
|
||||
@@ -113,7 +113,7 @@ internal class Balancetop : CommandExecutor {
|
||||
}
|
||||
|
||||
val baltop =
|
||||
postgreSQL.getBaltop(offset).getOrElse {
|
||||
balanceManager.getBaltop(offset).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get the information for balancetop."
|
||||
@@ -123,7 +123,7 @@ internal class Balancetop : CommandExecutor {
|
||||
}
|
||||
|
||||
val numberOfRows =
|
||||
postgreSQL.getNumberOfRows().getOrElse {
|
||||
balanceManager.getNumberOfRows().getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get the information for balancetop."
|
||||
|
||||
@@ -3,10 +3,10 @@ package net.trueog.diamondbankog.commands
|
||||
import kotlin.math.floor
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
@@ -70,7 +70,7 @@ internal class Deposit : CommandExecutor {
|
||||
}
|
||||
|
||||
val playerInventoryShards =
|
||||
postgreSQL.getInventoryShards(sender.uniqueId).getOrElse {
|
||||
balanceManager.getInventoryShards(sender.uniqueId).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get your balance."
|
||||
@@ -149,7 +149,7 @@ internal class Deposit : CommandExecutor {
|
||||
)
|
||||
}
|
||||
|
||||
postgreSQL.addToPlayerShards(sender.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(sender.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
handleError(sender.uniqueId, shards, PlayerShards(-1, playerInventoryShards, -1))
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
@@ -182,7 +182,7 @@ internal class Deposit : CommandExecutor {
|
||||
)
|
||||
)
|
||||
|
||||
postgreSQL
|
||||
balanceManager
|
||||
.insertTransactionLog(
|
||||
sender.uniqueId,
|
||||
shards,
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package net.trueog.diamondbankog.commands
|
||||
|
||||
import net.trueog.diamondbankog.BalanceManager
|
||||
import net.trueog.diamondbankog.Config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.plugin
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.PostgreSQL
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.command.Command
|
||||
import org.bukkit.command.CommandExecutor
|
||||
@@ -23,11 +23,11 @@ internal class DiamondBankReload : CommandExecutor {
|
||||
return true
|
||||
}
|
||||
|
||||
postgreSQL = PostgreSQL()
|
||||
balanceManager = BalanceManager()
|
||||
try {
|
||||
postgreSQL.initDB()
|
||||
balanceManager.init()
|
||||
} catch (e: Exception) {
|
||||
plugin.logger.info(e.toString())
|
||||
e.printStackTrace()
|
||||
sender.sendMessage(
|
||||
mm.deserialize("<red>Failed to reload the config. Check the console for more information.")
|
||||
)
|
||||
|
||||
@@ -5,10 +5,10 @@ import kotlin.math.floor
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankException
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
@@ -145,7 +145,7 @@ internal class Pay : CommandExecutor {
|
||||
)
|
||||
}
|
||||
|
||||
postgreSQL.addToPlayerShards(receiver.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
balanceManager.addToPlayerShards(receiver.uniqueId, shards, ShardType.BANK).getOrElse {
|
||||
handleError(sender.uniqueId, shards, null, receiver.uniqueId)
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
@@ -196,7 +196,7 @@ internal class Pay : CommandExecutor {
|
||||
)
|
||||
}
|
||||
|
||||
postgreSQL
|
||||
balanceManager
|
||||
.insertTransactionLog(
|
||||
sender.uniqueId,
|
||||
shards,
|
||||
|
||||
@@ -3,10 +3,10 @@ package net.trueog.diamondbankog.commands
|
||||
import java.util.*
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
|
||||
@@ -73,7 +73,7 @@ internal class SetBankBalance : CommandExecutor {
|
||||
return@launch
|
||||
}
|
||||
|
||||
postgreSQL.setPlayerShards(player.uniqueId, balance, ShardType.BANK).getOrElse {
|
||||
balanceManager.setPlayerShards(player.uniqueId, balance, ShardType.BANK).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to set that player's balance."
|
||||
@@ -92,7 +92,7 @@ internal class SetBankBalance : CommandExecutor {
|
||||
)
|
||||
)
|
||||
|
||||
postgreSQL
|
||||
balanceManager
|
||||
.insertTransactionLog(
|
||||
player.uniqueId,
|
||||
balance,
|
||||
|
||||
@@ -5,10 +5,10 @@ import kotlin.math.floor
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import net.trueog.diamondbankog.*
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.mm
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.postgreSQL
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
|
||||
import net.trueog.diamondbankog.DiamondBankOG.Companion.transactionLock
|
||||
import net.trueog.diamondbankog.ErrorHandler.handleError
|
||||
@@ -104,7 +104,7 @@ internal class Withdraw : CommandExecutor {
|
||||
val lockResult =
|
||||
transactionLock.tryWithLockSuspend(sender.uniqueId) {
|
||||
val playerShards =
|
||||
postgreSQL.getAllShards(sender.uniqueId).getOrElse {
|
||||
balanceManager.getAllShards(sender.uniqueId).getOrElse {
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
"${config.prefix}<reset>: <red>Something went wrong while trying to get your balance."
|
||||
@@ -162,7 +162,7 @@ internal class Withdraw : CommandExecutor {
|
||||
return@tryWithLockSuspend true
|
||||
}
|
||||
|
||||
postgreSQL.subtractFromBankShards(sender.uniqueId, shards).getOrElse {
|
||||
balanceManager.subtractFromBankShards(sender.uniqueId, shards).getOrElse {
|
||||
handleError(sender.uniqueId, shards, PostgreSQL.PlayerShards(playerBankShards, -1, -1))
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
@@ -192,7 +192,7 @@ internal class Withdraw : CommandExecutor {
|
||||
return@tryWithLockSuspend true
|
||||
}
|
||||
|
||||
postgreSQL.addToPlayerShards(sender.uniqueId, shards, ShardType.INVENTORY).getOrElse {
|
||||
balanceManager.addToPlayerShards(sender.uniqueId, shards, ShardType.INVENTORY).getOrElse {
|
||||
handleError(sender.uniqueId, shards, playerShards)
|
||||
sender.sendMessage(
|
||||
mm.deserialize(
|
||||
@@ -225,7 +225,7 @@ internal class Withdraw : CommandExecutor {
|
||||
)
|
||||
)
|
||||
|
||||
postgreSQL.insertTransactionLog(sender.uniqueId, shards, null, "Withdraw", null).getOrElse {
|
||||
balanceManager.insertTransactionLog(sender.uniqueId, shards, null, "Withdraw", null).getOrElse {
|
||||
handleError(sender.uniqueId, shards, null, null, true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user