diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-05-24 23:11:33 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-24 15:11:33 +0200 |
commit | 37b1df827f2f13663d7011057c9b7855aa96ee2c (patch) | |
tree | 6115088202bac4d269ad464924bdc9e2a782cd4a /src/main/java/at | |
parent | f7146145de7a3d597ee9203ccea95d7535d5a4e1 (diff) | |
download | skyhanni-37b1df827f2f13663d7011057c9b7855aa96ee2c.tar.gz skyhanni-37b1df827f2f13663d7011057c9b7855aa96ee2c.tar.bz2 skyhanni-37b1df827f2f13663d7011057c9b7855aa96ee2c.zip |
Cleanup: HighlightBonzoMasks (#1794)
Diffstat (limited to 'src/main/java/at')
3 files changed, 40 insertions, 41 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 0f3193f55..8dc4d0c2e 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -716,7 +716,7 @@ class SkyHanniMod { loadModule(GriffinBurrowParticleFinder) loadModule(BurrowWarpHelper()) loadModule(CollectionTracker()) - loadModule(HighlightBonzoMasks()) + loadModule(HighlightBonzoMasks) loadModule(BazaarCancelledBuyOrderClipboard()) loadModule(CompactSplashPotionMessage()) loadModule(CroesusChestTracker()) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/HighlightBonzoMasks.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/HighlightBonzoMasks.kt index 23702719f..02ca00298 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/HighlightBonzoMasks.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/HighlightBonzoMasks.kt @@ -6,35 +6,38 @@ 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() @@ -44,32 +47,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 } } @@ -83,15 +77,20 @@ class HighlightBonzoMasks { 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 } + } } } } - diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index 3aa530c22..fa1b22187 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -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 } |