diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-07-15 19:54:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-15 19:54:04 +0200 |
commit | ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b (patch) | |
tree | f52c6c37addc52c27452e0a161ee0b54485e093d /src/main/java/at/hannibal2 | |
parent | 2de249337b48f5de439afd16c6793abbe3084a25 (diff) | |
download | skyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.tar.gz skyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.tar.bz2 skyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.zip |
Backend: RecalculatingValue as a ReadOnlyProperty (#2151)
Diffstat (limited to 'src/main/java/at/hannibal2')
14 files changed, 42 insertions, 39 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt index 3c3c0d90b..4f3b3713c 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt @@ -62,7 +62,7 @@ object SlayerAPI { } event.addData { - add("activeSlayer: ${getActiveSlayer()}") + add("activeSlayer: $activeSlayer") add("isInCorrectArea: $isInCorrectArea") add("isInAnyArea: $isInAnyArea") add("latestSlayerProgress: $latestSlayerProgress") @@ -82,9 +82,7 @@ object SlayerAPI { } } - fun getActiveSlayer() = activeSlayer.getValue() - - private val activeSlayer = RecalculatingValue(1.seconds) { + val activeSlayer by RecalculatingValue(1.seconds) { grabActiveSlayer() } @@ -125,7 +123,7 @@ object SlayerAPI { } else { val slayerTypeForCurrentArea = getSlayerTypeForCurrentArea() isInAnyArea = slayerTypeForCurrentArea != null - slayerTypeForCurrentArea == getActiveSlayer() && slayerTypeForCurrentArea != null + slayerTypeForCurrentArea == activeSlayer && slayerTypeForCurrentArea != null } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt index 419cd115c..aa291eb92 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt @@ -166,7 +166,7 @@ object HoppityEggLocator { } private fun shouldShowAllEggs() = - config.showAllWaypoints && !hasLocatorInHotbar() && HoppityEggType.eggsRemaining() + config.showAllWaypoints && !locatorInHotbar && HoppityEggType.eggsRemaining() fun eggFound() { resetData() @@ -175,7 +175,7 @@ object HoppityEggLocator { @SubscribeEvent fun onReceiveParticle(event: ReceiveParticleEvent) { if (!isEnabled()) return - if (!hasLocatorInHotbar()) return + if (!locatorInHotbar) return if (!event.isVillagerParticle() && !event.isEnchantmentParticle()) return val lastParticlePosition = lastParticlePosition ?: run { @@ -281,9 +281,9 @@ object HoppityEggLocator { private val ItemStack.isLocatorItem get() = getInternalName() == locatorItem - private fun hasLocatorInHotbar() = RecalculatingValue(1.seconds) { + private val locatorInHotbar by RecalculatingValue(1.seconds) { LorenzUtils.inSkyBlock && InventoryUtils.getItemsInHotbar().any { it.isLocatorItem } - }.getValue() + } private fun LorenzVec.getEggLocationWeight(firstPoint: LorenzVec, secondPoint: LorenzVec): Double { val distToLine = this.distanceToLine(firstPoint, secondPoint) diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/AtmosphericFilterDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/AtmosphericFilterDisplay.kt index 91911e140..d00d277dc 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/AtmosphericFilterDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/AtmosphericFilterDisplay.kt @@ -21,7 +21,7 @@ object AtmosphericFilterDisplay { fun onSecondPassed(event: SecondPassedEvent) { if (!isEnabled()) return if (!GardenAPI.inGarden() && !config.outsideGarden) return - display = drawDisplay(SkyblockSeason.getCurrentSeason() ?: return) + display = drawDisplay(SkyblockSeason.currentSeason ?: return) } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/ChestValue.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/ChestValue.kt index a5122fc6c..bd032fe66 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/ChestValue.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/ChestValue.kt @@ -54,9 +54,7 @@ object ChestValue { if (InventoryUtils.openInventoryName() == "") return } - if (!config.showDuringEstimatedItemValue) { - if (EstimatedItemValue.isCurrentlyShowing()) return - } + if (!config.showDuringEstimatedItemValue && EstimatedItemValue.isCurrentlyShowing()) return if (inInventory) { config.position.renderStringsAndItems( @@ -256,7 +254,7 @@ object ChestValue { if ((name.contains("Backpack") && name.contains("Slot #") || name.startsWith("Ender Chest (")) && - !InventoryUtils.isNeuStorageEnabled.getValue() + !InventoryUtils.isNeuStorageEnabled ) { return true } diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryAPI.kt index a6f3626b6..f1cb4be05 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateFactoryAPI.kt @@ -156,7 +156,7 @@ object ChocolateFactoryAPI { fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled // TODO add debug toggle - fun isHoppityEvent() = SkyblockSeason.getCurrentSeason() == SkyblockSeason.SPRING + fun isHoppityEvent() = SkyblockSeason.currentSeason == SkyblockSeason.SPRING fun isMaxPrestige() = currentPrestige >= maxPrestige diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/TimeFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/TimeFeatures.kt index 5512d69a8..a4440295d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/TimeFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/TimeFeatures.kt @@ -28,7 +28,7 @@ object TimeFeatures { private val timeFormat24h = SimpleDateFormat("HH:mm:ss") private val timeFormat12h = SimpleDateFormat("hh:mm:ss a") - private val startOfNextYear = RecalculatingValue(1.seconds) { + private val startOfNextYear by RecalculatingValue(1.seconds) { SkyBlockTime(year = SkyBlockTime.now().year + 1).asTimeMark() } @@ -44,7 +44,7 @@ object TimeFeatures { if (winterConfig.islandCloseTime && IslandType.WINTER.isInIsland()) { if (WinterAPI.isDecember()) return - val timeTillNextYear = startOfNextYear.getValue().timeUntil() + val timeTillNextYear = startOfNextYear.timeUntil() val alreadyInNextYear = timeTillNextYear > 5.days val text = if (alreadyInNextYear) { "§fJerry's Workshop §cis closing!" diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/items/GlowingDroppedItems.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/items/GlowingDroppedItems.kt index 95c87e96d..24cb1614e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/items/GlowingDroppedItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/items/GlowingDroppedItems.kt @@ -66,12 +66,12 @@ object GlowingDroppedItems { return rarity?.color?.toColor()?.rgb } - private val isShowcaseArea = RecalculatingValue(1.seconds) { - showcaseItemIslands.contains(LorenzUtils.skyBlockIsland) || showcaseItemLocations.contains(LorenzUtils.skyBlockArea) + private val isShowcaseArea by RecalculatingValue(1.seconds) { + LorenzUtils.skyBlockIsland in showcaseItemIslands || LorenzUtils.skyBlockArea in showcaseItemLocations } private fun shouldHideShowcaseItem(entity: EntityItem): Boolean { - if (!isShowcaseArea.getValue() || config.highlightShowcase) return false + if (!isShowcaseArea || config.highlightShowcase) return false for (entityArmorStand in entity.worldObj.getEntitiesWithinAABB( EntityArmorStand::class.java, diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerQuestWarning.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerQuestWarning.kt index 188283fbd..a5f8af5c7 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerQuestWarning.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerQuestWarning.kt @@ -102,7 +102,7 @@ object SlayerQuestWarning { if (DianaAPI.isDoingDiana()) return // prevent warnings when mobs are hit by other players if (lastWeaponUse.passedSince() > 500.milliseconds) return - + lastWarning = SimpleTimeMark.now() ChatUtils.chat(chatMessage) @@ -124,7 +124,7 @@ object SlayerQuestWarning { private fun isSlayerMob(entity: EntityLivingBase): Boolean { val slayerType = SlayerAPI.getSlayerTypeForCurrentArea() ?: return false - val activeSlayer = SlayerAPI.getActiveSlayer() + val activeSlayer = SlayerAPI.activeSlayer if (activeSlayer != null) { if (slayerType != activeSlayer) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerRngMeterDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerRngMeterDisplay.kt index 26e0822b1..6396b2876 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerRngMeterDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerRngMeterDisplay.kt @@ -205,9 +205,12 @@ object SlayerRngMeterDisplay { } private fun makeLink(text: String) = - Renderable.clickAndHover(text, listOf("§eClick to open RNG Meter Inventory."), onClick = { - HypixelCommands.showRng("slayer", SlayerAPI.getActiveSlayer()?.rngName) - }) + Renderable.clickAndHover( + text, listOf("§eClick to open RNG Meter Inventory."), + onClick = { + HypixelCommands.showRng("slayer", SlayerAPI.activeSlayer?.rngName) + }, + ) fun drawDisplay(): String { val storage = getStorage() ?: return "" diff --git a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt index 76f9fbfcb..c8492086a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt @@ -71,7 +71,7 @@ object InventoryUtils { fun getLeggings(): ItemStack? = getArmor()[1] fun getBoots(): ItemStack? = getArmor()[0] - val isNeuStorageEnabled = RecalculatingValue(10.seconds) { + val isNeuStorageEnabled by RecalculatingValue(10.seconds) { try { val config = NotEnoughUpdates.INSTANCE.config diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index ea2a935a3..3c97919fc 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -315,13 +315,8 @@ object LorenzUtils { this.cancel() } } - - // TODO move into mayor api - private val recalculateDerpy = - RecalculatingValue(1.seconds) { Perk.DOUBLE_MOBS_HP.isActive } - // TODO move into mayor api - val isDerpy get() = recalculateDerpy.getValue() + val isDerpy by RecalculatingValue(1.seconds) { Perk.DOUBLE_MOBS_HP.isActive } // TODO move into mayor api fun Int.derpy() = if (isDerpy) this / 2 else this diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 51a07c0b3..217d681c3 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -382,7 +382,7 @@ object NEUItems { fun neuHasFocus(): Boolean { if (AuctionSearchOverlay.shouldReplace()) return true if (BazaarSearchOverlay.shouldReplace()) return true - if (InventoryUtils.inStorage() && InventoryUtils.isNeuStorageEnabled.getValue()) return true + if (InventoryUtils.inStorage() && InventoryUtils.isNeuStorageEnabled) return true if (NEUOverlay.searchBarHasFocus) return true return false diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt index 76f0c0c94..6059ed662 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt @@ -1,17 +1,28 @@ package at.hannibal2.skyhanni.utils +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty import kotlin.time.Duration -class RecalculatingValue<T>(private val expireTime: Duration, val calculation: () -> T) { +class RecalculatingValue<T>(private val expireTime: Duration, private val calculation: () -> T) : ReadOnlyProperty<Any?, T> { - private var currentValue = calculation() + private var currentValue: Any? = UNINITIALIZED_VALUE private var lastAccessTime = SimpleTimeMark.farPast() + @Deprecated("use \"by RecalculatingValue\" instead") fun getValue(): T { if (lastAccessTime.passedSince() > expireTime) { currentValue = calculation() lastAccessTime = SimpleTimeMark.now() } - return currentValue + @Suppress("UNCHECKED_CAST") + return currentValue as T + } + + @Suppress("DEPRECATION") + override fun getValue(thisRef: Any?, property: KProperty<*>): T = getValue() + + companion object { + private val UNINITIALIZED_VALUE = Any() } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt b/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt index edeb706b7..d74b80959 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt @@ -21,9 +21,7 @@ enum class SkyblockSeason( companion object { - fun getCurrentSeason(): SkyblockSeason? = currentSeason.getValue() - - private val currentSeason = RecalculatingValue(1.seconds) { + val currentSeason by RecalculatingValue(1.seconds) { getSeasonByName(SkyBlockTime.now().monthName) } |