From bfe43b7ebffab551740d32cf919db27504d27ffb Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Sat, 15 Jun 2024 22:12:24 +0200 Subject: 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 Co-authored-by: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Co-authored-by: Cal Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../at/hannibal2/skyhanni/utils/CollectionUtils.kt | 35 ++++++++++++++++++++++ .../java/at/hannibal2/skyhanni/utils/LorenzVec.kt | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') 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 Iterable.countBy(selector: (T) -> K): Map { + val map = mutableMapOf() + for (item in this) { + val key = selector(item) + map[key] = map.getOrDefault(key, 0) + 1 + } + return map + } + fun List.nextAfter(after: String, skip: Int = 1) = nextAfter({ it == after }, skip) fun List.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 List.removeFirst(predicate: (T) -> Boolean): List { + 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 Map.removeFirst(predicate: (Map.Entry) -> Boolean): Map { + 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 MutableSet.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()) -- cgit