Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: RecalculatingValue as a ReadOnlyProperty #2151

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object SlayerAPI {
}

event.addData {
add("activeSlayer: ${getActiveSlayer()}")
add("activeSlayer: $activeSlayer")
add("isInCorrectArea: $isInCorrectArea")
add("isInAnyArea: $isInAnyArea")
add("latestSlayerProgress: $latestSlayerProgress")
Expand All @@ -82,9 +82,7 @@ object SlayerAPI {
}
}

fun getActiveSlayer() = activeSlayer.getValue()

private val activeSlayer = RecalculatingValue(1.seconds) {
val activeSlayer by RecalculatingValue(1.seconds) {
grabActiveSlayer()
}

Expand Down Expand Up @@ -125,7 +123,7 @@ object SlayerAPI {
} else {
val slayerTypeForCurrentArea = getSlayerTypeForCurrentArea()
isInAnyArea = slayerTypeForCurrentArea != null
slayerTypeForCurrentArea == getActiveSlayer() && slayerTypeForCurrentArea != null
slayerTypeForCurrentArea == activeSlayer && slayerTypeForCurrentArea != null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ object HoppityEggLocator {
)
drawDynamicText(it.add(y = 1), "§aGuess", 1.5)
}
if (!drawLocations) {
if (config.showLine) {
draw3DLine(eyeLocation, it.add(0.5, 0.5, 0.5), LorenzColor.GREEN.toColor(), 2, false)
}
if (!drawLocations && config.showLine) {
draw3DLine(eyeLocation, it.add(0.5, 0.5, 0.5), LorenzColor.GREEN.toColor(), 2, false)
}
}
}
Expand All @@ -168,7 +166,7 @@ object HoppityEggLocator {
}

private fun shouldShowAllEggs() =
config.showAllWaypoints && !hasLocatorInHotbar() && HoppityEggType.eggsRemaining()
config.showAllWaypoints && !locatorInHotbar && HoppityEggType.eggsRemaining()

fun eggFound() {
resetData()
Expand All @@ -177,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 {
Expand Down Expand Up @@ -283,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ object ChestValue {
if (DungeonAPI.inDungeon() && !config.enableInDungeons) return
if (InventoryUtils.openInventoryName() == "") return

if (!config.showDuringEstimatedItemValue) {
if (EstimatedItemValue.isCurrentlyShowing()) return
}
if (!config.showDuringEstimatedItemValue && EstimatedItemValue.isCurrentlyShowing()) return

if (inInventory) {
config.position.renderStringsAndItems(
Expand Down Expand Up @@ -236,7 +234,7 @@ object ChestValue {


if ((name.contains("Backpack") && name.contains("Slot #") || name.startsWith("Ender Chest (")) &&
!InventoryUtils.isNeuStorageEnabled.getValue()
!InventoryUtils.isNeuStorageEnabled
) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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

Expand Down
7 changes: 1 addition & 6 deletions src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,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
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
4 changes: 1 addition & 3 deletions src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading