From ed88076776a45077c0e06ef8c134b1d3feb44ee6 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 17 May 2023 16:59:52 +0100 Subject: [PATCH] Converted to BigDecimal --- build.gradle.kts | 2 +- .../com/willfp/ecobits/EcoBitsPlugin.kt | 4 ++++ .../com/willfp/ecobits/currencies/Currency.kt | 19 ++++++++++--------- .../currencies/PriceFactoryCurrency.kt | 6 +++--- .../ecobits/integrations/IntegrationVault.kt | 18 +++++++++--------- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0f2adc5..ed71a64 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,7 +40,7 @@ allprojects { } dependencies { - compileOnly("com.willfp:eco:6.56.0") + compileOnly("com.willfp:eco:6.60.0") compileOnly("org.jetbrains:annotations:23.0.0") compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/EcoBitsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/EcoBitsPlugin.kt index cc28135..4b46812 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/EcoBitsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/EcoBitsPlugin.kt @@ -30,6 +30,10 @@ class EcoBitsPlugin : EcoPlugin() { ) } + override fun getMinimumEcoVersion(): String { + return "6.60.0" + } + companion object { @JvmStatic lateinit var instance: EcoBitsPlugin diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt index 8b9554c..5483be1 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/Currency.kt @@ -21,6 +21,7 @@ import net.milkbowl.vault.economy.Economy import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.plugin.ServicePriority +import java.math.BigDecimal import java.text.DecimalFormat import java.time.Duration import java.util.Optional @@ -38,11 +39,11 @@ class Currency( .expireAfterWrite(Duration.ofSeconds(plugin.configYml.getInt("cache-expire-after").toLong())) .build>() - val default = config.getDouble("default") + val default = BigDecimal(config.getDouble("default")) val name = config.getFormattedString("name") - val max = config.getDouble("max").let { if (it < 0) Double.MAX_VALUE else it } + val max = BigDecimal(config.getDouble("max").let { if (it < 0) Double.POSITIVE_INFINITY else it }) val isPayable = config.getBool("payable") @@ -56,7 +57,7 @@ class Currency( val key = PersistentDataKey( plugin.createNamespacedKey(if (isLocal) "${plugin.serverID}_${id}" else id), - PersistentDataKeyType.DOUBLE, + PersistentDataKeyType.BIG_DECIMAL, default ) @@ -162,10 +163,10 @@ class Currency( data class LeaderboardPlace( val player: OfflinePlayer, - val amount: Double + val amount: BigDecimal ) -fun Double.formatWithExtension(): String { +fun BigDecimal.formatWithExtension(): String { val suffix = charArrayOf(' ', 'k', 'M', 'B', 'T', 'P', 'E') val numValue = this.toLong() val value = floor(log10(numValue.toDouble())).toInt() @@ -179,17 +180,17 @@ fun Double.formatWithExtension(): String { } } -fun OfflinePlayer.getBalance(currency: Currency): Double { +fun OfflinePlayer.getBalance(currency: Currency): BigDecimal { return this.profile.read(currency.key) } -fun OfflinePlayer.setBalance(currency: Currency, value: Double) { +fun OfflinePlayer.setBalance(currency: Currency, value: BigDecimal) { this.profile.write( currency.key, - value.coerceIn(0.0..currency.max) + value.coerceIn(BigDecimal(0.0)..currency.max) ) } -fun OfflinePlayer.adjustBalance(currency: Currency, by: Double) { +fun OfflinePlayer.adjustBalance(currency: Currency, by: BigDecimal) { this.setBalance(currency, this.getBalance(currency) + by) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/PriceFactoryCurrency.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/PriceFactoryCurrency.kt index 5e97370..344c077 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/PriceFactoryCurrency.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/currencies/PriceFactoryCurrency.kt @@ -26,13 +26,13 @@ class PriceFactoryCurrency( private val multipliers = mutableMapOf() override fun canAfford(player: Player, multiplier: Double) = - player.getBalance(currency) >= getValue(player, multiplier) + player.getBalance(currency).toDouble() >= getValue(player, multiplier) override fun pay(player: Player, multiplier: Double) = - player.adjustBalance(currency, -getValue(player, multiplier)) + player.adjustBalance(currency, -getValue(player, multiplier).toBigDecimal()) override fun giveTo(player: Player, multiplier: Double) = - player.adjustBalance(currency, getValue(player, multiplier)) + player.adjustBalance(currency, getValue(player, multiplier).toBigDecimal()) override fun getValue(player: Player, multiplier: Double) = xp(baseContext.copyWithPlayer(player)) * getMultiplier(player) * multiplier diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/integrations/IntegrationVault.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/integrations/IntegrationVault.kt index 825ee86..b69600b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/integrations/IntegrationVault.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobits/integrations/IntegrationVault.kt @@ -30,7 +30,7 @@ class IntegrationVault( } override fun format(amount: Double): String { - return amount.formatWithExtension() + return amount.toBigDecimal().formatWithExtension() } override fun currencyNamePlural(): String { @@ -70,7 +70,7 @@ class IntegrationVault( } override fun getBalance(player: OfflinePlayer): Double { - return player.getBalance(currency) + return player.getBalance(currency).toDouble() } @Deprecated("Deprecated in Java", ReplaceWith("getBalance(playerName)")) @@ -91,7 +91,7 @@ class IntegrationVault( } override fun has(player: OfflinePlayer, amount: Double): Boolean { - return player.getBalance(currency) >= amount + return player.getBalance(currency).toDouble() >= amount } @Deprecated("Deprecated in Java", ReplaceWith("has(playerName, amount)")) @@ -120,7 +120,7 @@ class IntegrationVault( return EconomyResponse(0.0, 0.0, EconomyResponse.ResponseType.FAILURE, "Can't withdraw below 0.") } - if (player.getBalance(currency) - amount < 0) { + if (player.getBalance(currency).toDouble() - amount < 0) { return EconomyResponse( 0.0, 0.0, @@ -129,11 +129,11 @@ class IntegrationVault( ) } - player.adjustBalance(currency, -amount) + player.adjustBalance(currency, -amount.toBigDecimal()) return EconomyResponse( amount, - player.getBalance(currency), + player.getBalance(currency).toDouble(), EconomyResponse.ResponseType.SUCCESS, null ) @@ -165,7 +165,7 @@ class IntegrationVault( return EconomyResponse(0.0, 0.0, EconomyResponse.ResponseType.FAILURE, "Can't deposit below 0.") } - if (player.getBalance(currency) + amount > currency.max) { + if (player.getBalance(currency).toDouble() + amount > currency.max.toDouble()) { return EconomyResponse( 0.0, 0.0, @@ -174,10 +174,10 @@ class IntegrationVault( ) } - player.adjustBalance(currency, amount) + player.adjustBalance(currency, amount.toBigDecimal()) return EconomyResponse( amount, - player.getBalance(currency), + player.getBalance(currency).toDouble(), EconomyResponse.ResponseType.SUCCESS, null )