diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 2c355b402..e58d05fab 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -371,11 +371,30 @@ object LorenzUtils { fun IslandType.isInIsland() = inIsland(this) - fun <K, N : Number> MutableMap<K, N>.addOrPut(item: K, amount: N) { + fun <K, N : Number> MutableMap<K, N>.addOrPut(item: K, amount: N): N { val old = this[item] ?: 0 - val d = old.toDouble() + amount.toDouble() + val new = when (old) { + is Double -> old + amount.toDouble() + is Float -> old + amount.toFloat() + is Long -> old + amount.toLong() + else -> old.toInt() + amount.toInt() + } @Suppress("UNCHECKED_CAST") - this[item] = d as N + this[item] = new as N + return new + } + + @Suppress("UNCHECKED_CAST") + fun <K, N : Number> MutableMap<K, N>.sumAllValues(): Double { + if (values.isEmpty()) { + return 0.0 + } + return when (values.first()) { + is Double -> values.sumOf { it.toDouble() } + is Float -> values.sumOf { it.toDouble() } + is Long -> values.sumOf { it.toLong() }.toDouble() + else -> values.sumOf { it.toInt() }.toDouble() + } } /** transfer string colors from the config to java.awt.Color */ |