9
0
mirror of https://github.com/Auxilor/EcoBits.git synced 2025-12-19 15:09:19 +00:00

Converted to BigDecimal

This commit is contained in:
Auxilor
2023-05-17 16:59:52 +01:00
parent b36d7acde9
commit ed88076776
5 changed files with 27 additions and 22 deletions

View File

@@ -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")

View File

@@ -30,6 +30,10 @@ class EcoBitsPlugin : EcoPlugin() {
)
}
override fun getMinimumEcoVersion(): String {
return "6.60.0"
}
companion object {
@JvmStatic
lateinit var instance: EcoBitsPlugin

View File

@@ -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<Int, Optional<LeaderboardPlace>>()
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)
}

View File

@@ -26,13 +26,13 @@ class PriceFactoryCurrency(
private val multipliers = mutableMapOf<UUID, Double>()
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

View File

@@ -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
)