diff --git a/src/main/kotlin/net/trueog/diamondbankog/CommonOperations.kt b/src/main/kotlin/net/trueog/diamondbankog/CommonOperations.kt index 15d4b4f..0c07e43 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/CommonOperations.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/CommonOperations.kt @@ -3,7 +3,6 @@ package net.trueog.diamondbankog import java.util.* import kotlin.math.floor import net.trueog.diamondbankog.DiamondBankException.InsufficientFundsException -import net.trueog.diamondbankog.DiamondBankException.InsufficientInventorySpaceException import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager import net.trueog.diamondbankog.DiamondBankRuntimeException.MoreThanOneDecimalDigitRuntimeException import org.bukkit.Bukkit @@ -21,7 +20,7 @@ object CommonOperations { val removedInShards = InventorySnapshotUtils.removeShards(inventorySnapshot, toRemoveShards) .getOrElse { - return Result.failure(InsufficientInventorySpaceException()) + return Result.failure(it) } .toLong() if (removedInShards != toRemoveShards) { @@ -48,6 +47,11 @@ object CommonOperations { fun shardsToDiamonds(shards: Long) = String.format("%.1f", floor((shards / 9.0) * 10) / 10.0) + fun shardsToDiamondsFull(shards: Long): String { + val diamonds = shardsToDiamonds(shards) + return "$diamonds Diamond${if (diamonds != "1.0") "s" else ""}" + } + fun getPlayerUsingUuidOrName(value: String): OfflinePlayer { return try { val uuid = UUID.fromString(value) diff --git a/src/main/kotlin/net/trueog/diamondbankog/DiamondBankException.kt b/src/main/kotlin/net/trueog/diamondbankog/DiamondBankException.kt index a271eda..457bb9d 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/DiamondBankException.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/DiamondBankException.kt @@ -15,8 +15,6 @@ sealed class DiamondBankException(message: String) : Exception(message) { class InsufficientFundsException(val short: Long) : DiamondBankException("Insufficient funds") - class InsufficientInventorySpaceException : DiamondBankException("Insufficient inventory space for the change") - class InvalidArgumentException : Exception() } diff --git a/src/main/kotlin/net/trueog/diamondbankog/InventorySnapshotUtils.kt b/src/main/kotlin/net/trueog/diamondbankog/InventorySnapshotUtils.kt index 9d8d32f..65aefbd 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/InventorySnapshotUtils.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/InventorySnapshotUtils.kt @@ -2,6 +2,9 @@ package net.trueog.diamondbankog import kotlin.math.ceil import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager +import net.trueog.diamondbankog.DiamondBankOG.Companion.config +import net.trueog.diamondbankog.DiamondBankOG.Companion.mm +import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.inventory.ItemStack @@ -23,9 +26,16 @@ object InventorySnapshotUtils { if (notRemovedDiamonds == 0) { val addMap = inventory.addItem(Shard.createItemStack(shardsChange)) if (addMap.isNotEmpty()) { - balanceManager.addToBankShards(inventory.holder, addMap.values.sumOf { it.amount }.toLong()).getOrElse { + val excessShards = addMap.values.sumOf { it.amount }.toLong() + balanceManager.addToBankShards(inventory.holder, excessShards).getOrElse { return Result.failure(it) } + val player = Bukkit.getPlayer(inventory.holder) + player?.sendMessage( + mm.deserialize( + "${config.prefix}: The excess change of ${CommonOperations.shardsToDiamondsFull(excessShards)} has been deposited into your bank account" + ) + ) } return Result.success(shards) } @@ -37,20 +47,27 @@ object InventorySnapshotUtils { inventory.removeItem(ItemStack(Material.DIAMOND_BLOCK, diamondBlocksToBeRemoved)).values.sumOf { it.amount } if (notRemovedDiamondBlocks == 0) { val shardsAddMap = inventory.addItem(Shard.createItemStack(shardsChange)) + val excessShards = shardsAddMap.values.sumOf { it.amount }.toLong() if (shardsAddMap.isNotEmpty()) { - balanceManager - .addToBankShards(inventory.holder, shardsAddMap.values.sumOf { it.amount }.toLong()) - .getOrElse { - return Result.failure(it) - } + balanceManager.addToBankShards(inventory.holder, excessShards).getOrElse { + return Result.failure(it) + } } val diamondsAddMap = inventory.addItem(ItemStack(Material.DIAMOND, diamondsChange)) + val excessDiamondsInShards = diamondsAddMap.values.sumOf { it.amount }.toLong() * 9 if (diamondsAddMap.isNotEmpty()) { - balanceManager - .addToBankShards(inventory.holder, shardsAddMap.values.sumOf { it.amount }.toLong() * 9) - .getOrElse { - return Result.failure(it) - } + balanceManager.addToBankShards(inventory.holder, excessDiamondsInShards).getOrElse { + return Result.failure(it) + } + } + + if (shardsAddMap.isNotEmpty() || diamondsAddMap.isNotEmpty()) { + val player = Bukkit.getPlayer(inventory.holder) + player?.sendMessage( + mm.deserialize( + "${config.prefix}: The excess change of ${CommonOperations.shardsToDiamondsFull(excessShards+excessDiamondsInShards)} has been deposited into your bank account" + ) + ) } return Result.success(shards) } diff --git a/src/main/kotlin/net/trueog/diamondbankog/api/DiamondBankAPIJava.kt b/src/main/kotlin/net/trueog/diamondbankog/api/DiamondBankAPIJava.kt index e601a88..2c6fff7 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/api/DiamondBankAPIJava.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/api/DiamondBankAPIJava.kt @@ -206,14 +206,12 @@ class DiamondBankAPIJava { * @throws DiamondBankException.InvalidPlayerException * @throws DiamondBankException.PlayerNotOnlineException * @throws DiamondBankException.InsufficientFundsException - * @throws DiamondBankException.InsufficientInventorySpaceException */ @Throws( EconomyDisabledException::class, InvalidPlayerException::class, PlayerNotOnlineException::class, InsufficientFundsException::class, - InsufficientInventorySpaceException::class, ) @Suppress("unused") fun consumeFromPlayer(uuid: UUID, shards: Long, transactionReason: String, notes: String?) { @@ -262,14 +260,12 @@ class DiamondBankAPIJava { * @throws DiamondBankException.InvalidPlayerException * @throws DiamondBankException.PayerNotOnlineException * @throws DiamondBankException.InsufficientFundsException - * @throws DiamondBankException.InsufficientInventorySpaceException */ @Throws( EconomyDisabledException::class, InvalidPlayerException::class, PayerNotOnlineException::class, InsufficientFundsException::class, - InsufficientInventorySpaceException::class, ) @Suppress("unused") fun playerPayPlayer(payerUuid: UUID, receiverUuid: UUID, shards: Long, transactionReason: String, notes: String?) { diff --git a/src/main/kotlin/net/trueog/diamondbankog/api/README.md b/src/main/kotlin/net/trueog/diamondbankog/api/README.md index 63206c7..da4a3c8 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/api/README.md +++ b/src/main/kotlin/net/trueog/diamondbankog/api/README.md @@ -187,7 +187,6 @@ public Map<@Nullable UUID, Long> getBaltop(int offset) * @throws DiamondBankException.InvalidPlayerException * @throws DiamondBankException.PlayerNotOnlineException * @throws DiamondBankException.InsufficientFundsException - * @throws DiamondBankException.InsufficientInventorySpaceException */ ``` Kotlin: @@ -213,7 +212,6 @@ public void consumeFromPlayer(UUID uuid, long shards, String transactionReason, * @throws DiamondBankException.InvalidPlayerException * @throws DiamondBankException.PayerNotOnlineException * @throws DiamondBankException.InsufficientFundsException - * @throws DiamondBankException.InsufficientInventorySpaceException */ ``` Kotlin: diff --git a/src/main/kotlin/net/trueog/diamondbankog/commands/Pay.kt b/src/main/kotlin/net/trueog/diamondbankog/commands/Pay.kt index 41abf32..b3fc157 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/commands/Pay.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/commands/Pay.kt @@ -103,16 +103,6 @@ internal class Pay : CommandExecutor { return@tryWithLockSuspend } - is DiamondBankException.InsufficientInventorySpaceException -> { - sender.sendMessage( - mm.deserialize( - "${config.prefix}: You do not have enough inventory space for the change." - ) - ) - sender.inventory.unlock() - return@tryWithLockSuspend - } - else -> { sender.sendMessage( mm.deserialize(