From fe0449b9b8161135874ac99c1685233349f06f0d Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Fri, 24 May 2024 14:36:56 +0200 Subject: Backend: GetOrPut in TimeLimitedCache (#1875) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../features/fishing/ShowFishingItemName.kt | 68 +++++++++------------- .../misc/compacttablist/AdvancedPlayerList.kt | 10 +--- .../features/misc/visualwords/ModifyVisualWords.kt | 45 +++++++------- .../features/slayer/SlayerItemsOnGround.kt | 37 ++++++------ 4 files changed, 66 insertions(+), 94 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/features') 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 28c2aa27f..2ce023100 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt @@ -4,12 +4,11 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.features.fishing.FishingAPI.isBait +import at.hannibal2.skyhanni.utils.ConditionalUtils.transformIf import at.hannibal2.skyhanni.utils.EntityUtils -import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture import at.hannibal2.skyhanni.utils.ItemUtils.name -import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzUtils -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 @@ -21,8 +20,7 @@ import kotlin.time.Duration.Companion.milliseconds class ShowFishingItemName { private val config get() = SkyHanniMod.feature.fishing.fishedItemName - private var hasRodInHand = false - private var cache = TimeLimitedCache>(750.milliseconds) + private var itemsOnGround = TimeLimitedCache(750.milliseconds) // Taken from Skytils private val cheapCoins = setOf( @@ -33,53 +31,39 @@ class ShowFishingItemName { @SubscribeEvent fun onTick(event: LorenzTickEvent) { if (!isEnabled()) return + for (entityItem in EntityUtils.getEntitiesNextToPlayer(15.0)) { + val itemStack = entityItem.entityItem + // Hypixel sometimes replaces the bait item mid air with a stone + if (itemStack.name.removeColor() == "Stone") continue + var text = "" - if (event.isMod(10)) { - hasRodInHand = isFishingRod() + val isBait = itemStack.isBait() + if (isBait && !config.showBaits) continue + + if (itemStack.getSkullTexture() in cheapCoins) { + text = "§6Coins" + } else { + val name = itemStack.name.transformIf({isBait}) { "§7" + this.removeColor() } + text += if (isBait) "§c§l- §r" else "§a§l+ §r" + + val size = itemStack.stackSize + if (size != 1) text += "§7x$size §r" + text += name + } + + itemsOnGround.put(entityItem, text) } } - private fun isFishingRod() = InventoryUtils.getItemInHand()?.name?.contains("Rod") ?: false - @SubscribeEvent fun onRenderWorld(event: LorenzRenderWorldEvent) { if (!isEnabled()) return - if (hasRodInHand) { - for (entityItem in EntityUtils.getEntities()) { - val location = event.exactLocation(entityItem).add(y = 0.8) - if (location.distance(LocationUtils.playerLocation()) > 15) continue - val itemStack = entityItem.entityItem - var name = itemStack.name - - // Hypixel sometimes replaces the bait item mid air with a stone - if (name.removeColor() == "Stone") continue - - val size = itemStack.stackSize - val prefix = if (!itemStack.isBait()) { - "§a§l+" - } else { - if (!config.showBaits) continue - name = "§7" + name.removeColor() - "§c§l-" - } - - itemStack?.tagCompound?.getTag("SkullOwner")?.toString()?.let { - for (coin in cheapCoins) { - if (it.contains(coin)) { - name = "§6Coins" - } - } - } - - val sizeText = if (size != 1) "§7x$size §r" else "" - cache.put(entityItem, location to "$prefix §r$sizeText$name") - } - } - for ((location, text) in cache.values()) { + for ((item, text) in itemsOnGround) { + val location = event.exactLocation(item).add(y = 0.8) event.drawString(location, text) } } - fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled + fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled && FishingAPI.holdingRod } 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 70571b5cc..6379e38c2 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 @@ -210,14 +210,8 @@ object AdvancedPlayerList { private var randomOrderCache = TimeLimitedCache(20.minutes) - private fun getRandomOrder(name: String): Int { - val saved = randomOrderCache.getOrNull(name) - if (saved != null) { - return saved - } - val r = (Random.nextDouble() * 500).toInt() - randomOrderCache.put(name, r) - return r + private fun getRandomOrder(name: String) = randomOrderCache.getOrPut(name) { + (Random.nextDouble() * 500).toInt() } private fun getSocialIcon(name: String) = when { 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 2c31f078c..b0a9d651e 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 @@ -29,35 +29,32 @@ object ModifyVisualWords { modifiedWords.addAll(SkyHanniMod.visualWordsData.modifiedWords) } - val cachedResult = textCache.getOrNull(originalText) - if (cachedResult != null) { - return cachedResult - } + return textCache.getOrPut(originalText) { + if (originalText.startsWith("§§")) { + modifiedText = modifiedText.removePrefix("§§") + } else { + for (modifiedWord in modifiedWords) { + if (!modifiedWord.enabled) continue + val phrase = modifiedWord.phrase.convertToFormatted() - if (originalText.startsWith("§§")) { - modifiedText = modifiedText.removePrefix("§§") - } else { - for (modifiedWord in modifiedWords) { - if (!modifiedWord.enabled) continue - val phrase = modifiedWord.phrase.convertToFormatted() + if (phrase.isEmpty()) continue - if (phrase.isEmpty()) continue + modifiedText = modifiedText.replace( + phrase, modifiedWord.replacement.convertToFormatted(), modifiedWord.isCaseSensitive() + ) + } + } - modifiedText = modifiedText.replace( - phrase, modifiedWord.replacement.convertToFormatted(), modifiedWord.isCaseSensitive() - ) + // Disabled, as its only a novelty for 30 seconds and will annoy after that everyone. + /* + if (LorenzUtils.isAprilFoolsDay && !FontRendererHook.cameFromChat && Random.nextDouble() < 0.02) { + modifiedText = modifiedText.replace(reverseRegex) { + it.groupValues[1] + it.groupValues[2].reversed() + } } + */ + modifiedText } - - // Disabled, as its only a novelty for 30 seconds and will annoy after that everyone. - -// if (LorenzUtils.isAprilFoolsDay && !FontRendererHook.cameFromChat && Random.nextDouble() < 0.02) { -// modifiedText = modifiedText.replace(reverseRegex) { -// it.groupValues[1] + it.groupValues[2].reversed() -// } -// } - textCache.put(originalText, modifiedText) - return modifiedText } @SubscribeEvent 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 0864bf658..7474b1088 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt @@ -3,11 +3,10 @@ package at.hannibal2.skyhanni.features.slayer import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.data.SlayerAPI import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent +import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName -import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzUtils -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 @@ -21,33 +20,31 @@ class SlayerItemsOnGround { private val config get() = SkyHanniMod.feature.slayer.itemsOnGround - private var itemsOnGround = TimeLimitedCache>(2.seconds) + private var itemsOnGround = TimeLimitedCache(2.seconds) @SubscribeEvent - fun onRenderWorld(event: LorenzRenderWorldEvent) { - if (!LorenzUtils.inSkyBlock) return - if (!config.enabled) return - if (!SlayerAPI.isInCorrectArea) return - if (!SlayerAPI.hasActiveSlayerQuest()) return - - for (entityItem in EntityUtils.getEntities()) { - val location = event.exactLocation(entityItem).add(y = 0.8) - if (location.distance(LocationUtils.playerLocation()) > 15) continue - + fun onTick(event: LorenzTickEvent) { + if (!isEnabled()) return + for (entityItem in EntityUtils.getEntitiesNextToPlayer(15.0)) { val itemStack = entityItem.entityItem - // happens in spiders den sometimes if (itemStack.item == Items.spawn_egg) continue - if (itemStack.getInternalName().equals("")) continue // TODO remove, should never happen if (itemStack.getInternalName() == NEUInternalName.NONE) continue - - val (itemName, price) = SlayerAPI.getItemNameAndPrice(itemStack.getInternalName(), itemStack.stackSize) + val (name, price) = SlayerAPI.getItemNameAndPrice(itemStack.getInternalName(), itemStack.stackSize) if (config.minimumPrice > price) continue - - itemsOnGround.put(entityItem, location to itemName) + itemsOnGround.put(entityItem, name) } + } - for ((location, text) in itemsOnGround.values()) { + @SubscribeEvent + fun onRenderWorld(event: LorenzRenderWorldEvent) { + if (!isEnabled()) return + + for ((item, text) in itemsOnGround) { + val location = event.exactLocation(item).add(y = 0.8) event.drawString(location, text) } } + + fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled && + SlayerAPI.isInCorrectArea && SlayerAPI.hasActiveSlayerQuest() } -- cgit