diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-07-19 17:13:28 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-07-19 17:13:28 +0200 |
commit | dbfd0b107c2abf1966300dfc6dd7f34329fc282c (patch) | |
tree | 19ab3e018214bb932b6a65465135b95baee778bc /src/main/java/at/hannibal2/skyhanni/utils | |
parent | b45fd460c0a91de19fa7cc6e95b4987e9cdc25d5 (diff) | |
download | skyhanni-dbfd0b107c2abf1966300dfc6dd7f34329fc282c.tar.gz skyhanni-dbfd0b107c2abf1966300dfc6dd7f34329fc282c.tar.bz2 skyhanni-dbfd0b107c2abf1966300dfc6dd7f34329fc282c.zip |
Show total amount of all rarities at the end of the chat message
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 */ |