Skip to content

Commit

Permalink
Cleanup: HighlightBonzoMasks
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs committed May 14, 2024
1 parent 4a43033 commit 81eaed2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ class SkyHanniMod {
loadModule(GriffinBurrowParticleFinder)
loadModule(BurrowWarpHelper())
loadModule(CollectionTracker())
loadModule(HighlightBonzoMasks())
loadModule(HighlightBonzoMasks)
loadModule(BazaarCancelledBuyOrderClipboard())
loadModule(CompactSplashPotionMessage())
loadModule(CroesusChestTracker())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.RenderUtils.interpolate
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.item.ItemStack
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.TimeMark
import kotlin.time.TimeSource

/**
* @author Linnea Gräf
*/
@OptIn(ExperimentalTime::class)
class HighlightBonzoMasks {
object HighlightBonzoMasks {

private val config get() = SkyHanniMod.feature.inventory.itemAbilities

private val maskTimers = mutableMapOf<String, CooldownTimer>()
private val maskTimers = mutableMapOf<MaskType, SimpleTimeMark>()

// Technically this timer is overestimating since mage level affects the cooldown, however I don't care.
private val bonzoMaskCooldown = 360.seconds
private val bonzoMaskMessage = "^Your (.*Bonzo's Mask) saved your life!$".toRegex()

private val spiritMaskCooldown = 30.seconds
private val spiritMaskMessage = "^Second Wind Activated! Your Spirit Mask saved your life!$".toRegex()
private val patternGroup = RepoPattern.group("inventory.masks.timers")
private val bonzoMaskPattern by patternGroup.pattern(
"bonzo",
"Your .*Bonzo's Mask saved your life!"
)
private val spiritMaskPattern by patternGroup.pattern(
"spirit",
"Second Wind Activated! Your Spirit Mask saved your life!"
)

private val greenHue = Color.RGBtoHSB(0, 255, 0, null)[0].toDouble()
private val redHue = Color.RGBtoHSB(255, 0, 0, null)[0].toDouble()
Expand All @@ -44,32 +46,23 @@ class HighlightBonzoMasks {
if (!config.depletedBonzosMasks) return
for (slot in event.gui.inventorySlots.inventorySlots) {
val item = slot.stack ?: continue
val maskType = maskType(item) ?: continue
val timer = maskTimers[maskType] ?: continue
if (timer.isActive) {
val hue = interpolate(greenHue, redHue, timer.percentComplete.toFloat())
val maskType = MaskType.getByInternalName(item.getInternalName()) ?: continue
val readyAt = maskTimers[maskType] ?: continue

if (readyAt.isInFuture()) {
val hue = interpolate(redHue, greenHue, maskType.percentageComplete(readyAt.timeUntil()))
slot.highlight(Color(Color.HSBtoRGB(hue.toFloat(), 1F, 1F)))
}
}
}

private fun maskType(item: ItemStack): String? {
return when (item.getInternalName().asString()) {
"STARRED_BONZO_MASK" -> "BONZO_MASK"
"BONZO_MASK" -> "BONZO_MASK"
"SPIRIT_MASK" -> "SPIRIT_MASK"
"STARRED_SPIRIT_MASK" -> "SPIRIT_MASK"
else -> null
}
}

@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
val message = event.message.removeColor()
if (bonzoMaskMessage.matches(message)) {
maskTimers["BONZO_MASK"] = CooldownTimer(TimeSource.Monotonic.markNow(), bonzoMaskCooldown)
} else if (spiritMaskMessage.matches(message)) {
maskTimers["SPIRIT_MASK"] = CooldownTimer(TimeSource.Monotonic.markNow(), spiritMaskCooldown)
if (bonzoMaskPattern.matches(message)) {
maskTimers[MaskType.BONZO_MASK] = SimpleTimeMark.now() + MaskType.BONZO_MASK.cooldown
} else if (spiritMaskPattern.matches(message)) {
maskTimers[MaskType.SPIRIT_MASK] = SimpleTimeMark.now() + MaskType.SPIRIT_MASK.cooldown
}
}

Expand All @@ -78,20 +71,20 @@ class HighlightBonzoMasks {
maskTimers.clear()
}

@SubscribeEvent
fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
event.move(2, "inventory.highlightDepletedBonzosMasks", "itemAbilities.depletedBonzosMasks")
}

companion object {
data class CooldownTimer(val timeMark: TimeMark, val duration: Duration) {
// This timer is overestimating since mage level affects the cooldown
private enum class MaskType(val internalNames: List<NEUInternalName>, val cooldown: Duration) {
BONZO_MASK(listOf("BONZO_MASK".asInternalName(), "STARRED_BONZO_MASK".asInternalName()), 6.minutes),
SPIRIT_MASK(listOf("SPIRIT_MASK".asInternalName(), "STARRED_SPIRIT_MASK".asInternalName()), 30.seconds),
;

val percentComplete: Double
get() =
timeMark.elapsedNow().toDouble(DurationUnit.SECONDS) / duration.toDouble(DurationUnit.SECONDS)
fun percentageComplete(timeUntil: Duration): Double {
return timeUntil.inWholeMilliseconds / cooldown.inWholeMilliseconds.toDouble()
}

val isActive: Boolean get() = timeMark.elapsedNow() < duration
companion object {
fun getByInternalName(internalName: NEUInternalName): MaskType? {
return entries.firstOrNull { internalName in it.internalNames }
}
}
}
}

2 changes: 1 addition & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ object RenderUtils {
GlStateManager.popMatrix()
}

fun interpolate(currentValue: Double, lastValue: Double, multiplier: Float): Double {
fun interpolate(currentValue: Double, lastValue: Double, multiplier: Double): Double {
return lastValue + (currentValue - lastValue) * multiplier
}

Expand Down

0 comments on commit 81eaed2

Please sign in to comment.