aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEmpa <42304516+ItsEmpa@users.noreply.github.com>2024-05-24 14:36:56 +0200
committerGitHub <noreply@github.com>2024-05-24 14:36:56 +0200
commitfe0449b9b8161135874ac99c1685233349f06f0d (patch)
treec14bd993d1dab1cc771b13fa4cb99c7a7f3a1864 /src
parentcb6869cb79cb4f5679eb90f9120c716b77a9078e (diff)
downloadskyhanni-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')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt30
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt68
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt10
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/misc/visualwords/ModifyVisualWords.kt45
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemsOnGround.kt37
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt1
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt10
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt2
8 files changed, 88 insertions, 115 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
index ec62adda0..f4235cb73 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
@@ -38,27 +38,21 @@ object SlayerAPI {
System.currentTimeMillis()
} else latestProgressChangeTime
- fun getItemNameAndPrice(internalName: NEUInternalName, amount: Int): Pair<String, Double> {
- val key = internalName to amount
- nameCache.getOrNull(key)?.let {
- return it
- }
-
- val amountFormat = if (amount != 1) "§7${amount}x §r" else ""
- val displayName = internalName.itemName
+ fun getItemNameAndPrice(internalName: NEUInternalName, amount: Int): Pair<String, Double> =
+ nameCache.getOrPut(internalName to amount) {
+ val amountFormat = if (amount != 1) "§7${amount}x §r" else ""
+ val displayName = internalName.itemName
- val price = internalName.getPrice()
- val npcPrice = internalName.getNpcPriceOrNull() ?: 0.0
- val maxPrice = npcPrice.coerceAtLeast(price)
- val totalPrice = maxPrice * amount
+ val price = internalName.getPrice()
+ val npcPrice = internalName.getNpcPriceOrNull() ?: 0.0
+ val maxPrice = npcPrice.coerceAtLeast(price)
+ val totalPrice = maxPrice * amount
- val format = NumberUtil.format(totalPrice)
- val priceFormat = " §7(§6$format coins§7)"
+ val format = NumberUtil.format(totalPrice)
+ val priceFormat = " §7(§6$format coins§7)"
- val result = "$amountFormat$displayName$priceFormat" to totalPrice
- nameCache.put(key, result)
- return result
- }
+ "$amountFormat$displayName$priceFormat" to totalPrice
+ }
@SubscribeEvent
fun onDebugDataCollect(event: DebugDataCollectEvent) {
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<EntityItem, Pair<LorenzVec, String>>(750.milliseconds)
+ private var itemsOnGround = TimeLimitedCache<EntityItem, String>(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<EntityItem>(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<EntityItem>()) {
- 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<String, Int>(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<EntityItem, Pair<LorenzVec, String>>(2.seconds)
+ private var itemsOnGround = TimeLimitedCache<EntityItem, String>(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<EntityItem>()) {
- 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<EntityItem>(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()
}
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 = {},
) {