Merge pull request #23 from SKBotNL/master

Add shardsToDiamonds API and some cleanup
This commit was merged in pull request #23.
This commit is contained in:
NotAlexNoyle authored and GitHub committed 2026-02-13 16:16:02 +00:00
commit 71f20882fc
10 files changed
+67 -103

No files matched your search

@@ -1,10 +1,13 @@
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.DiamondBankException.MoreThanOneDecimalDigitException
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
object CommonOperations {
suspend fun consume(uuid: UUID, shards: Long, inventorySnapshot: InventorySnapshot): Result<Unit> {
@@ -42,4 +45,15 @@ object CommonOperations {
}
return Result.success((split[0].toLong() * 9) + split[1].toLong())
}
fun shardsToDiamonds(shards: Long) = String.format("%.1f", floor((shards / 9.0) * 10) / 10.0)
fun getPlayerUsingUuidOrName(value: String): OfflinePlayer {
return try {
val uuid = UUID.fromString(value)
Bukkit.getPlayer(uuid) ?: Bukkit.getOfflinePlayer(uuid)
} catch (_: Exception) {
Bukkit.getPlayer(value) ?: Bukkit.getOfflinePlayer(value)
}
}
}
@@ -300,11 +300,14 @@ class DiamondBankAPIJava {
}
/**
* Converts a Diamond value to Shards
* Converts a Diamond float to Shards
*
* @throws MoreThanOneDecimalDigitException
*/
@Throws(MoreThanOneDecimalDigitException::class)
@Suppress("unused")
fun diamondsToShards(diamonds: Float): Long = CommonOperations.diamondsToShards(diamonds).getOrThrow()
fun diamondsToShards(diamonds: Float) = CommonOperations.diamondsToShards(diamonds).getOrThrow()
/** Converts Shards into a formatted Diamonds string */
@Suppress("unused") fun shardsToDiamonds(shards: Long) = CommonOperations.shardsToDiamonds(shards)
}
@@ -221,7 +221,9 @@ class DiamondBankAPIKotlin {
eventManager.register(eventListener)
}
/** Converts a Diamond value to Shards */
@Suppress("unused")
fun diamondsToShards(diamonds: Float): Result<Long> = CommonOperations.diamondsToShards(diamonds)
/** Converts a Diamond float to Shards */
@Suppress("unused") fun diamondsToShards(diamonds: Float) = CommonOperations.diamondsToShards(diamonds)
/** Converts Shards into a formatted Diamonds string */
@Suppress("unused") fun shardsToDiamonds(shards: Long) = CommonOperations.shardsToDiamonds(shards)
}
@@ -23,7 +23,7 @@ if (provider == null) {
## Available functions
> [!IMPORTANT]
> Always convert Diamonds to Shards before using the API using [`diamondsToShards`](#diamondstoshards)
> Always convert Diamonds to Shards before using the API using [`diamondsToShards`](#diamondstoshards).
Exceptions are only applicable when using the Java API, when using the Kotlin API the function will return a `Result` instead.
### addToPlayerShards
@@ -251,16 +251,31 @@ public void registerEventListener(PlayerBalanceChangedListener eventListener)
### diamondsToShards
```kotlin
/**
* Converts a Diamond value to Shards
* Converts a Diamond float to Shards
*
* @throws DiamondBankException.MoreThanOneDecimalDigitException
*/
```
Kotlin:
```kotlin
fun diamondsToShards(diamonds: Float): Long
fun diamondsToShards(diamonds: Float): Result<Long>
```
Java:
```java
public long diamondsToShards(float diamonds)
```
```
### shardsToDiamonds
> [!CAUTION]
> This function should only be used for visual output, not calculations.
```kotlin
/** Converts Shards into a formatted Diamonds string */
```
Kotlin:
```kotlin
fun shardsToDiamonds(shards: Long): String
```
Java:
```java
public String shardsToDiamonds(long shards)
```
@@ -1,16 +1,14 @@
package net.trueog.diamondbankog.commands
import java.util.*
import kotlin.math.floor
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import net.trueog.diamondbankog.CommonOperations
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.scope
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
@@ -38,49 +36,10 @@ internal class Balance : CommandExecutor {
if (args == null) return@launch
if (sender !is Player) {
if (args.isEmpty()) {
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>Please provide that name or UUID of the player that you want to check the balance of."
)
)
return@launch
}
val otherPlayer =
try {
Bukkit.getPlayer(UUID.fromString(args[0])) ?: Bukkit.getOfflinePlayer(UUID.fromString(args[0]))
} catch (_: Exception) {
Bukkit.getPlayer(args[0]) ?: Bukkit.getOfflinePlayer(args[0])
}
if (!otherPlayer.hasPlayedBefore()) {
sender.sendMessage(mm.deserialize("${config.prefix}<reset>: <red>That player doesn't exist."))
return@launch
}
val balance =
balanceManager.getAllShards(otherPlayer.uniqueId).getOrElse {
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>Something went wrong while trying to get their balance."
)
)
return@launch
}
val totalBalance = balance.bank + balance.inventory + balance.enderChest
val totalDiamonds = String.format("%.1f", floor((totalBalance / 9.0) * 10) / 10.0)
val bankDiamonds = String.format("%.1f", floor((balance.bank / 9.0) * 10) / 10.0)
val inventoryDiamonds = String.format("%.1f", floor((balance.inventory / 9.0) * 10) / 10.0)
val enderChestDiamonds = String.format("%.1f", floor((balance.enderChest / 9.0) * 10) / 10.0)
if (args.isEmpty() && sender !is Player) {
sender.sendMessage(
mm.deserialize(
"<green>Balance of ${getPrefix(otherPlayer.uniqueId)}${otherPlayer.name}<reset><green>:\n" +
"Bank: <yellow>$bankDiamonds <aqua>Diamond${if (bankDiamonds != "1.0") "s" else ""}\n" +
"<green>Inventory: <yellow>$inventoryDiamonds <aqua>Diamond${if (inventoryDiamonds != "1.0") "s" else ""}\n" +
"<green>Ender Chest: <yellow>$enderChestDiamonds <aqua>Diamond${if (enderChestDiamonds != "1.0") "s" else ""}\n" +
"<bold><green>Total: <yellow>$totalDiamonds <aqua>Diamond${if (totalDiamonds != "1.0") "s" else ""}"
"${config.prefix}<reset>: <red>Please provide that name or UUID of the player that you want to check the balance of."
)
)
return@launch
@@ -88,7 +47,7 @@ internal class Balance : CommandExecutor {
val balancePlayer =
if (args.isEmpty()) {
sender
sender as Player
} else {
if (!sender.hasPermission("diamondbank-og.balance.others")) {
sender.sendMessage(
@@ -99,13 +58,7 @@ internal class Balance : CommandExecutor {
return@launch
}
val otherPlayer =
try {
Bukkit.getPlayer(UUID.fromString(args[0]))
?: Bukkit.getOfflinePlayer(UUID.fromString(args[0]))
} catch (_: Exception) {
Bukkit.getPlayer(args[0]) ?: Bukkit.getOfflinePlayer(args[0])
}
val otherPlayer = CommonOperations.getPlayerUsingUuidOrName(args[0])
if (!otherPlayer.hasPlayedBefore()) {
sender.sendMessage(mm.deserialize("${config.prefix}<reset>: <red>That player doesn't exist."))
return@launch
@@ -117,7 +70,7 @@ internal class Balance : CommandExecutor {
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>Something went wrong while trying to get ${
if (balancePlayer.uniqueId != sender.uniqueId) "their" else "your"
if (if (sender is Player) sender.uniqueId == balancePlayer.uniqueId else false) "your" else "their"
} balance."
)
)
@@ -125,14 +78,14 @@ internal class Balance : CommandExecutor {
}
val totalBalance = balance.bank + balance.inventory + balance.enderChest
val totalDiamonds = String.format("%.1f", floor((totalBalance / 9.0) * 10) / 10.0)
val bankDiamonds = String.format("%.1f", floor((balance.bank / 9.0) * 10) / 10.0)
val inventoryDiamonds = String.format("%.1f", floor((balance.inventory / 9.0) * 10) / 10.0)
val enderChestDiamonds = String.format("%.1f", floor((balance.enderChest / 9.0) * 10) / 10.0)
val totalDiamonds = CommonOperations.shardsToDiamonds(totalBalance)
val bankDiamonds = CommonOperations.shardsToDiamonds(balance.bank)
val inventoryDiamonds = CommonOperations.shardsToDiamonds(balance.inventory)
val enderChestDiamonds = CommonOperations.shardsToDiamonds(balance.enderChest)
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <green>${
if (balancePlayer.uniqueId == sender.uniqueId) "Your Balance" else "Balance of ${
if (if (sender is Player) sender.uniqueId == balancePlayer.uniqueId else false) "Your Balance" else "Balance of ${
getPrefix(
balancePlayer.uniqueId
)
@@ -1,10 +1,9 @@
package net.trueog.diamondbankog.commands
import java.util.*
import kotlin.math.ceil
import kotlin.math.floor
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import net.trueog.diamondbankog.CommonOperations
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
@@ -55,13 +54,7 @@ internal class Balancetop : CommandExecutor {
index = args[0].toInt()
offset = 9 * (index - 1)
} catch (_: Exception) {
player =
try {
Bukkit.getPlayer(UUID.fromString(args[0]))
?: Bukkit.getOfflinePlayer(UUID.fromString(args[0]))
} catch (_: Exception) {
Bukkit.getPlayer(args[0]) ?: Bukkit.getOfflinePlayer(args[0])
}
player = CommonOperations.getPlayerUsingUuidOrName(args[0])
if (!player.hasPlayedBefore()) {
sender.sendMessage(
mm.deserialize(
@@ -100,7 +93,7 @@ internal class Balancetop : CommandExecutor {
val player = Bukkit.getPlayer(uuid) ?: Bukkit.getOfflinePlayer(uuid)
val playerName = player.name ?: player.uniqueId.toString()
val diamonds = String.format("%.1f", floor((it.value / 9.0) * 10) / 10.0)
val diamonds = CommonOperations.shardsToDiamonds(it.value)
baltopMessage +=
"\n<green>${baltopMessage.lines().size + offset}<reset>. ${if (playerName == player.name) "<italic>" else ""}${
getPrefix(
@@ -160,7 +153,7 @@ internal class Balancetop : CommandExecutor {
val player = Bukkit.getPlayer(uuid) ?: Bukkit.getOfflinePlayer(uuid)
val playerName = player.name ?: player.uniqueId.toString()
val diamonds = String.format("%.1f", floor((it.value / 9.0) * 10) / 10.0)
val diamonds = CommonOperations.shardsToDiamonds(it.value)
baltopMessage +=
"\n<green>${baltopMessage.lines().size + (9 * (index - 1))}<reset>. ${if (playerName == player.name) "<italic>" else ""}${
getPrefix(
@@ -1,6 +1,5 @@
package net.trueog.diamondbankog.commands
import kotlin.math.floor
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import net.trueog.diamondbankog.CommonOperations
@@ -96,7 +95,7 @@ internal class Deposit : CommandExecutor {
.toLong()
}
if (shards != -1L && removedInShards != shards) {
val shardsInDiamonds = String.format("%.1f", floor((shards / 9.0) * 10) / 10.0)
val shardsInDiamonds = CommonOperations.shardsToDiamonds(shards)
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>You do not have <yellow>$shardsInDiamonds <aqua>Diamond${if (shardsInDiamonds != "1.0") "s" else ""} <red>to deposit."
@@ -121,7 +120,7 @@ internal class Deposit : CommandExecutor {
sender.inventory.unlock()
}
val diamondsDeposited = String.format("%.1f", floor((removedInShards / 9.0) * 10) / 10.0)
val diamondsDeposited = CommonOperations.shardsToDiamonds(removedInShards)
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <green>Successfully deposited <yellow>$diamondsDeposited <aqua>Diamond${if (diamondsDeposited != "1.0") "s" else ""} <green>into your bank account."
@@ -1,7 +1,5 @@
package net.trueog.diamondbankog.commands
import java.util.*
import kotlin.math.floor
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import net.trueog.diamondbankog.CommonOperations
@@ -19,7 +17,6 @@ import net.trueog.diamondbankog.MainThreadBlock.runOnMainThread
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
import net.trueog.diamondbankog.PostgreSQL.ShardType
import net.trueog.diamondbankog.TransactionLock
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
@@ -48,13 +45,7 @@ internal class Pay : CommandExecutor {
return true
}
val receiver =
try {
Bukkit.getPlayer(UUID.fromString(args[0])) ?: Bukkit.getOfflinePlayer(UUID.fromString(args[0]))
} catch (_: Exception) {
Bukkit.getPlayer(args[0]) ?: Bukkit.getOfflinePlayer(args[0])
}
val receiver = CommonOperations.getPlayerUsingUuidOrName(args[0])
if (sender.uniqueId == receiver.uniqueId) {
sender.sendMessage(mm.deserialize("${config.prefix}<reset>: <red>You cannot pay yourself."))
return true
@@ -103,7 +94,7 @@ internal class Pay : CommandExecutor {
CommonOperations.consume(sender.uniqueId, shards, inventorySnapshot).getOrElse {
when (it) {
is DiamondBankException.InsufficientFundsException -> {
val shortInDiamonds = String.format("%.1f", floor((it.short / 9.0) * 10) / 10.0)
val shortInDiamonds = CommonOperations.shardsToDiamonds(it.short)
sender.sendMessage(
mm.deserialize(
"${config.prefix}<reset>: <red>You are <yellow>$shortInDiamonds <aqua>Diamond${if (shortInDiamonds != "1.0") "s" else ""} <red>short for that payment."
@@ -146,7 +137,7 @@ internal class Pay : CommandExecutor {
return@tryWithLockSuspend
}
val diamondsPaid = String.format("%.1f", floor((shards / 9.0) * 10) / 10.0)
val diamondsPaid = CommonOperations.shardsToDiamonds(shards)
runOnMainThread {
inventorySnapshot.restoreTo(sender.inventory)
@@ -1,8 +1,8 @@
package net.trueog.diamondbankog.commands
import java.util.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import net.trueog.diamondbankog.CommonOperations
import net.trueog.diamondbankog.DiamondBankOG.Companion.balanceManager
import net.trueog.diamondbankog.DiamondBankOG.Companion.config
import net.trueog.diamondbankog.DiamondBankOG.Companion.economyDisabled
@@ -11,7 +11,6 @@ import net.trueog.diamondbankog.DiamondBankOG.Companion.scope
import net.trueog.diamondbankog.ErrorHandler.handleError
import net.trueog.diamondbankog.PlayerPrefix.getPrefix
import net.trueog.diamondbankog.PostgreSQL.ShardType
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
@@ -54,12 +53,7 @@ internal class SetBankBalance : CommandExecutor {
return@launch
}
val player =
try {
Bukkit.getPlayer(UUID.fromString(args[0])) ?: Bukkit.getOfflinePlayer(UUID.fromString(args[0]))
} catch (_: Exception) {
Bukkit.getPlayer(args[0]) ?: Bukkit.getOfflinePlayer(args[0])
}
val player = CommonOperations.getPlayerUsingUuidOrName(args[0])
if (!player.hasPlayedBefore()) {
sender.sendMessage(mm.deserialize("${config.prefix}<reset>: <red>That player doesn't exist."))
return@launch
@@ -96,8 +96,8 @@ internal class Withdraw : CommandExecutor {
shards
}
val diamonds = String.format("%.1f", floor((shardsToWithdraw / 9.0) * 10) / 10.0)
val bankDiamonds = String.format("%.1f", floor((bankShards / 9.0) * 10) / 10.0)
val diamonds = CommonOperations.shardsToDiamonds(shardsToWithdraw)
val bankDiamonds = CommonOperations.shardsToDiamonds(bankShards)
if (shards != -1L && shards > bankShards) {
sender.inventory.unlock()