aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorEmpa <42304516+ItsEmpa@users.noreply.github.com>2024-06-15 22:12:24 +0200
committerGitHub <noreply@github.com>2024-06-15 22:12:24 +0200
commitbfe43b7ebffab551740d32cf919db27504d27ffb (patch)
tree7fb570635c88d98f122d2752be78f135868a9843 /src/main/java/at/hannibal2/skyhanni/utils
parent8c91a9b8bf63059e1a8a099ccbdadea8a3676948 (diff)
downloadskyhanni-bfe43b7ebffab551740d32cf919db27504d27ffb.tar.gz
skyhanni-bfe43b7ebffab551740d32cf919db27504d27ffb.tar.bz2
skyhanni-bfe43b7ebffab551740d32cf919db27504d27ffb.zip
Feature: Mineshaft Pity Display + OreMinedEvent (#1655)
Co-authored-by: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Co-authored-by: Empa <42304516+ItsEmpa@users.noreply.github.com> Co-authored-by: Thunderblade73 <gaidermarkus@gmail.com> Co-authored-by: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Co-authored-by: Cal <cwolfson58@gmail.com> Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt35
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt2
2 files changed, 36 insertions, 1 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
index 87261b6d0..d540daffe 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
@@ -92,6 +92,16 @@ object CollectionUtils {
}
}
+ /** Returns a map containing the count of occurrences of each distinct result of the [selector] function. */
+ inline fun <T, K> Iterable<T>.countBy(selector: (T) -> K): Map<K, Int> {
+ val map = mutableMapOf<K, Int>()
+ for (item in this) {
+ val key = selector(item)
+ map[key] = map.getOrDefault(key, 0) + 1
+ }
+ return map
+ }
+
fun List<String>.nextAfter(after: String, skip: Int = 1) = nextAfter({ it == after }, skip)
fun List<String>.nextAfter(after: (String) -> Boolean, skip: Int = 1): String? {
@@ -210,6 +220,31 @@ object CollectionUtils {
return collection
}
+ /** Removes the first element that matches the given [predicate] in the list. */
+ fun <T> List<T>.removeFirst(predicate: (T) -> Boolean): List<T> {
+ val mutableList = this.toMutableList()
+ val iterator = mutableList.iterator()
+ while (iterator.hasNext()) {
+ if (predicate(iterator.next())) {
+ iterator.remove()
+ break
+ }
+ }
+ return mutableList.toList()
+ }
+
+ /** Removes the first element that matches the given [predicate] in the map. */
+ fun <K, V> Map<K, V>.removeFirst(predicate: (Map.Entry<K, V>) -> Boolean): Map<K, V> {
+ val mutableMap = this.toMutableMap()
+ val iterator = mutableMap.entries.iterator()
+ while (iterator.hasNext()) {
+ if (predicate(iterator.next())) {
+ iterator.remove()
+ break
+ }
+ }
+ return mutableMap.toMap()
+ }
/** Updates a value if it is present in the set (equals), useful if the newValue is not reference equal with the value in the set */
inline fun <reified T> MutableSet<T>.refreshReference(newValue: T) = if (this.contains(newValue)) {
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt
index e369a69ca..474d6dee5 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt
@@ -121,7 +121,7 @@ data class LorenzVec(
}
}
- fun toCleanString(): String = "$x $y $z"
+ fun toCleanString(separator: String = ", "): String = listOf(x, y, z).joinToString(separator)
fun lengthSquared(): Double = x * x + y * y + z * z
fun length(): Double = sqrt(this.lengthSquared())