Merge pull request #28 from SKBotNL/master

Remove InsufficientInventorySpaceException and notify player about depositing of excess change
This commit was merged in pull request #28.
This commit is contained in:
NotAlexNoyle authored and GitHub committed 2026-02-25 16:27:08 +00:00
commit 5c71baf297
6 files changed
+34 -31

No files matched your search

@@ -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 "<yellow>$diamonds <aqua>Diamond${if (diamonds != "1.0") "s" else ""}"
}
fun getPlayerUsingUuidOrName(value: String): OfflinePlayer {
return try {
val uuid = UUID.fromString(value)
@@ -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()
}
@@ -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}<reset>: <yellow>The excess change of ${CommonOperations.shardsToDiamondsFull(excessShards)} <yellow> 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}<reset>: <yellow>The excess change of ${CommonOperations.shardsToDiamondsFull(excessShards+excessDiamondsInShards)} <yellow> has been deposited into your bank account"
)
)
}
return Result.success(shards)
}
@@ -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?) {
@@ -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:
@@ -103,16 +103,6 @@ internal class Pay : CommandExecutor {
return@tryWithLockSuspend
}
is DiamondBankException.InsufficientInventorySpaceException -> {
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>You do not have enough inventory space for the change."
)
)
sender.inventory.unlock()
return@tryWithLockSuspend
}
else -> {
sender.sendMessage(
mm.deserialize(