diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-05-24 14:36:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-24 14:36:56 +0200 |
commit | fe0449b9b8161135874ac99c1685233349f06f0d (patch) | |
tree | c14bd993d1dab1cc771b13fa4cb99c7a7f3a1864 /src/main/java/at/hannibal2/skyhanni/utils | |
parent | cb6869cb79cb4f5679eb90f9120c716b77a9078e (diff) | |
download | skyhanni-fe0449b9b8161135874ac99c1685233349f06f0d.tar.gz skyhanni-fe0449b9b8161135874ac99c1685233349f06f0d.tar.bz2 skyhanni-fe0449b9b8161135874ac99c1685233349f06f0d.zip |
Backend: GetOrPut in TimeLimitedCache (#1875)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
3 files changed, 10 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index 20bf02bb7..3aa530c22 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -1040,6 +1040,7 @@ object RenderUtils { } fun exactLocation(entity: Entity, partialTicks: Float): LorenzVec { + if (entity.isDead) return entity.getLorenzVec() val x = entity.lastTickPosX + (entity.posX - entity.lastTickPosX) * partialTicks val y = entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * partialTicks val z = entity.lastTickPosZ + (entity.posZ - entity.lastTickPosZ) * partialTicks diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt index 8b4b0c372..182b1381b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt @@ -4,10 +4,10 @@ import com.google.common.cache.CacheBuilder import java.util.concurrent.TimeUnit import kotlin.time.Duration -class TimeLimitedCache<K, V>( +class TimeLimitedCache<K: Any, V: Any>( expireAfterWrite: Duration, private val removalListener: (K?, V?) -> Unit = { _, _ -> }, -) { +): Iterable<Map.Entry<K, V>> { private val cache = CacheBuilder.newBuilder() .expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS) @@ -18,11 +18,17 @@ class TimeLimitedCache<K, V>( fun getOrNull(key: K): V? = cache.getIfPresent(key) + fun getOrPut(key: K, defaultValue: () -> V) = getOrNull(key) ?: defaultValue().also { put(key, it) } + fun clear() = cache.invalidateAll() + fun entries(): Set<Map.Entry<K, V>> = cache.asMap().entries + fun values(): Collection<V> = cache.asMap().values fun keys(): Set<K> = cache.asMap().keys fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null + + override fun iterator(): Iterator<Map.Entry<K, V>> = entries().iterator() } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt index 5f0ccd404..fae86fac5 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt @@ -2,7 +2,7 @@ package at.hannibal2.skyhanni.utils import kotlin.time.Duration -class TimeLimitedSet<T>( +class TimeLimitedSet<T: Any>( expireAfterWrite: Duration, private val removalListener: (T) -> Unit = {}, ) { |