diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2024-01-20 20:21:00 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2024-01-20 20:21:00 +0100 |
commit | 680b20370ad4563f202a08eae5713d0d1d7f9078 (patch) | |
tree | 49fad13bd420faa263e8dc28134468357a5cacca /src/main/java | |
parent | dd86bbaf6dd31b3b9c3340cf1ecf80eae1e94eff (diff) | |
download | skyhanni-680b20370ad4563f202a08eae5713d0d1d7f9078.tar.gz skyhanni-680b20370ad4563f202a08eae5713d0d1d7f9078.tar.bz2 skyhanni-680b20370ad4563f202a08eae5713d0d1d7f9078.zip |
creating and using TimeLimitedCache and TimeLimitedSet instead of guava cache.
Diffstat (limited to 'src/main/java')
9 files changed, 63 insertions, 37 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt index ee6899340..ddb5943a0 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt @@ -16,16 +16,14 @@ import at.hannibal2.skyhanni.utils.NEUItems.getNpcPriceOrNull import at.hannibal2.skyhanni.utils.NEUItems.getPrice import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.RecalculatingValue -import com.google.common.cache.CacheBuilder +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.concurrent.TimeUnit +import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds object SlayerAPI { - private var nameCache = - CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES) - .build<Pair<NEUInternalName, Int>, Pair<String, Double>>() + private var nameCache = TimeLimitedCache<Pair<NEUInternalName, Int>, Pair<String, Double>>(1.minutes) var questStartTime = 0L var isInCorrectArea = false @@ -43,7 +41,7 @@ object SlayerAPI { fun getItemNameAndPrice(internalName: NEUInternalName, amount: Int): Pair<String, Double> { val key = internalName to amount - nameCache.getIfPresent(key)?.let { + nameCache.getOrNull(key)?.let { return it } diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt index 4a41b332c..97945eb21 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt @@ -13,17 +13,15 @@ import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.exactLocation import at.hannibal2.skyhanni.utils.StringUtils.removeColor -import com.google.common.cache.CacheBuilder +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraft.entity.item.EntityItem import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.concurrent.TimeUnit +import kotlin.time.Duration.Companion.milliseconds class ShowFishingItemName { private val config get() = SkyHanniMod.feature.fishing.fishedItemName private var hasRodInHand = false - private var cache = - CacheBuilder.newBuilder().expireAfterWrite(750, TimeUnit.MILLISECONDS) - .build<EntityItem, Pair<LorenzVec, String>>() + private var cache = TimeLimitedCache<EntityItem, Pair<LorenzVec, String>>(750.milliseconds) // Taken from Skytils private val cheapCoins = setOf( @@ -77,7 +75,7 @@ class ShowFishingItemName { } } - for ((location, text) in cache.asMap().values) { + for ((location, text) in cache.values()) { event.drawString(location, text) } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt index 86b0be8dd..b5684265f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt @@ -18,10 +18,10 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor -import com.google.common.cache.CacheBuilder +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.concurrent.TimeUnit import kotlin.random.Random +import kotlin.time.Duration.Companion.minutes object AdvancedPlayerList { private val config get() = SkyHanniMod.feature.misc.compactTabList.advancedPlayerList @@ -188,11 +188,10 @@ object AdvancedPlayerList { return "$level $playerName ${suffix.trim()}" } - private var randomOrderCache = - CacheBuilder.newBuilder().expireAfterWrite(20, TimeUnit.MINUTES).build<String, Int>() + private var randomOrderCache = TimeLimitedCache<String, Int>(20.minutes) private fun getRandomOrder(name: String): Int { - val saved = randomOrderCache.getIfPresent(name) + val saved = randomOrderCache.getOrNull(name) if (saved != null) { return saved } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/ModifyVisualWords.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/ModifyVisualWords.kt index 44c4f8cac..5f0ee6494 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/ModifyVisualWords.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/ModifyVisualWords.kt @@ -3,13 +3,12 @@ package at.hannibal2.skyhanni.features.misc.visualwords import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.StringUtils.convertToFormatted -import com.google.common.cache.Cache -import com.google.common.cache.CacheBuilder -import java.util.concurrent.TimeUnit +import at.hannibal2.skyhanni.utils.TimeLimitedCache +import kotlin.time.Duration.Companion.minutes object ModifyVisualWords { private val config get() = SkyHanniMod.feature.gui.modifyWords - var textCache: Cache<String, String> = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build() + var textCache = TimeLimitedCache<String, String>(5.minutes) var modifiedWords = mutableListOf<VisualWord>() @@ -23,7 +22,7 @@ object ModifyVisualWords { modifiedWords = SkyHanniMod.feature.storage.modifiedWords } - val cachedResult = textCache.getIfPresent(originalText) + val cachedResult = textCache.getOrNull(originalText) if (cachedResult != null) { return cachedResult } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/VisualWordGui.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/VisualWordGui.kt index 03cf21b1d..b88171e96 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/VisualWordGui.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/VisualWordGui.kt @@ -554,7 +554,7 @@ open class VisualWordGui : GuiScreen() { private fun saveChanges() { ModifyVisualWords.modifiedWords = modifiedWords - ModifyVisualWords.textCache.invalidateAll() + ModifyVisualWords.textCache.clear() SkyHanniMod.feature.storage.modifiedWords = modifiedWords } diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt index f0f80ace4..fedc9593a 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt @@ -11,18 +11,16 @@ import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.exactLocation -import com.google.common.cache.CacheBuilder +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraft.entity.item.EntityItem import net.minecraft.init.Items import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.concurrent.TimeUnit +import kotlin.time.Duration.Companion.seconds class SlayerItemsOnGround { private val config get() = SkyHanniMod.feature.slayer.itemsOnGround - private var itemsOnGround = - CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.SECONDS) - .build<EntityItem, Pair<LorenzVec, String>>() + private var itemsOnGround = TimeLimitedCache<EntityItem, Pair<LorenzVec, String>>(2.seconds) @SubscribeEvent fun onRenderWorld(event: LorenzRenderWorldEvent) { @@ -47,7 +45,7 @@ class SlayerItemsOnGround { itemsOnGround.put(entityItem, location to itemName) } - for ((location, text) in itemsOnGround.asMap().values) { + for ((location, text) in itemsOnGround.values()) { event.drawString(location, text) } } diff --git a/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt b/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt index eaeed6aba..092800c10 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt @@ -5,20 +5,19 @@ import at.hannibal2.skyhanni.utils.KeyboardManager import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils import at.hannibal2.skyhanni.utils.StringUtils.removeColor -import com.google.common.cache.CacheBuilder +import at.hannibal2.skyhanni.utils.TimeLimitedSet import net.minecraft.client.Minecraft import java.util.UUID -import java.util.concurrent.TimeUnit +import kotlin.time.Duration.Companion.minutes object ErrorManager { // random id -> error message private val errorMessages = mutableMapOf<String, String>() private val fullErrorMessages = mutableMapOf<String, String>() - private var cache = - CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build<Pair<String, Int>, Unit>() + private var cache = TimeLimitedSet<Pair<String, Int>>(10.minutes) fun resetCache() { - cache.asMap().clear() + cache.clear() } fun skyHanniError(message: String): Nothing { @@ -77,10 +76,10 @@ object ErrorManager { if (!ignoreErrorCache) { val pair = if (throwable.stackTrace.isNotEmpty()) { - throwable.stackTrace[0].let { it.fileName to it.lineNumber } + throwable.stackTrace[0].let { it.fileName!! to it.lineNumber } } else message to 0 - if (cache.getIfPresent(pair) != null) return - cache.put(pair, Unit) + if (cache.contains(pair)) return + cache.add(pair) } val fullStackTrace = throwable.getCustomStackTrace(true).joinToString("\n") diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt new file mode 100644 index 000000000..8aab176ef --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt @@ -0,0 +1,21 @@ +package at.hannibal2.skyhanni.utils + +import com.google.common.cache.CacheBuilder +import java.util.concurrent.TimeUnit +import kotlin.time.Duration + +class TimeLimitedCache<K, V>(expireAfterWrite: Duration) { + + private val cache = CacheBuilder.newBuilder() + .expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS).build<K, V>() + + fun put(key: K, value: V) = cache.put(key, value) + + fun getOrNull(key: K): V? = cache.getIfPresent(key) + + fun clear() = cache.invalidateAll() + + fun values(): MutableCollection<V> = cache.asMap().values + + fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt new file mode 100644 index 000000000..97caf70ad --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt @@ -0,0 +1,14 @@ +package at.hannibal2.skyhanni.utils + +import kotlin.time.Duration + +class TimeLimitedSet<T>(expireAfterWrite: Duration) { + + private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite) + + fun add(element: T) = cache.put(element, Unit) + + fun contains(element: T): Boolean = cache.containsKey(element) + + fun clear() = cache.clear() +} |