diff options
author | Walker Selby <git@walkerselby.com> | 2023-10-21 07:31:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-21 08:31:19 +0200 |
commit | 68abc8ade546de62aee76e0b786993fc59fbc3cc (patch) | |
tree | bfc1e6fe389c7daf5cbf2ca109be70233d014590 /src/main/java/at/hannibal2/skyhanni/features/event | |
parent | c5745ed4ee07759261da1dc9c0f654617276f163 (diff) | |
download | skyhanni-68abc8ade546de62aee76e0b786993fc59fbc3cc.tar.gz skyhanni-68abc8ade546de62aee76e0b786993fc59fbc3cc.tar.bz2 skyhanni-68abc8ade546de62aee76e0b786993fc59fbc3cc.zip |
Internal Change: Misc Refactoring (#551)
Internal Change: Misc Refactoring #551
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/event')
2 files changed, 186 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasure.kt b/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasure.kt new file mode 100644 index 000000000..e4670ffcc --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasure.kt @@ -0,0 +1,19 @@ +package at.hannibal2.skyhanni.features.event.jerry.frozentreasure + +enum class FrozenTreasure( + val internalName: String, + val displayName: String, + val defaultAmount: Int, + val iceMultiplier: Int = 0, +) { + WHITE_GIFT("WHITE_GIFT", "§fWhite Gift", 1), + GREEN_GIFT("GREEN_GIFT", "§aGreen Gift", 1), + RED_GIFT("RED_GIFT", "§9§cRed Gift", 1), + PACKED_ICE("PACKED_ICE", "§fPacked Ice", 32, 9), + ENCHANTED_ICE("ENCHANTED_ICE", "§aEnchanted Ice", 9, 160), // wiki says 1-16 so assuming 9 + ENCHANTED_PACKED_ICE("ENCHANTED_PACKED_ICE", "§9Enchanted Packed Ice", 1, 25600), + ICE_BAIT("ICE_BAIT", "§aIce Bait", 16), + GLOWY_CHUM_BAIT("GLOWY_CHUM_BAIT", "§aGlowy Chum Bait", 16), + GLACIAL_FRAGMENT("GLACIAL_FRAGMENT", "§5Glacial Fragment", 1), + GLACIAL_TALISMAN("GLACIAL_TALISMAN", "§fGlacial Talisman", 1) +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasureTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasureTracker.kt new file mode 100644 index 000000000..034761654 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/event/jerry/frozentreasure/FrozenTreasureTracker.kt @@ -0,0 +1,167 @@ +package at.hannibal2.skyhanni.features.event.jerry.frozentreasure + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.config.Storage +import at.hannibal2.skyhanni.data.IslandType +import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.data.ScoreboardData +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent +import at.hannibal2.skyhanni.events.PreProfileSwitchEvent +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy +import at.hannibal2.skyhanni.utils.NumberUtil +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.concurrent.fixedRateTimer + +class FrozenTreasureTracker { + private val config get() = SkyHanniMod.feature.event.winter.frozenTreasureTracker + private var display = emptyList<List<Any>>() + private var estimatedIce = 0L + private var lastEstimatedIce = 0L + private var icePerSecond = mutableListOf<Long>() + private var icePerHour = 0 + private var stoppedChecks = 0 + private var compactPattern = "COMPACT! You found an Enchanted Ice!".toPattern() + + init { + fixedRateTimer(name = "skyhanni-frozen-treasure-tracker", period = 1000) { + if (!onJerryWorkshop()) return@fixedRateTimer + calculateIcePerHour() + } + } + + @SubscribeEvent + fun onWorldChange(event: LorenzWorldChangeEvent) { + icePerHour = 0 + stoppedChecks = 0 + icePerSecond = mutableListOf() + saveAndUpdate() + } + + private fun calculateIcePerHour() { + val difference = estimatedIce - lastEstimatedIce + lastEstimatedIce = estimatedIce + + if (difference == estimatedIce) return + + + if (difference == 0L) { + if (icePerSecond.isEmpty()) return + stoppedChecks += 1 + } else { + if (stoppedChecks > 60) { + stoppedChecks = 0 + icePerSecond.clear() + icePerHour = 0 + } + while (stoppedChecks > 0) { + stoppedChecks -= 1 + icePerSecond.add(0) + } + icePerSecond.add(difference) + val listCopy = icePerSecond + while (listCopy.size > 1200) listCopy.removeAt(0) + icePerSecond = listCopy + } + icePerHour = (icePerSecond.average() * 3600).toInt() + } + + private fun formatDisplay(map: List<List<Any>>): List<List<Any>> { + val newList = mutableListOf<List<Any>>() + for (index in config.textFormat) { + newList.add(map[index]) + } + return newList + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!ProfileStorageData.loaded) return + if (!onJerryWorkshop()) return + + val message = event.message.removeColor().trim() + val storage = ProfileStorageData.profileSpecific?.frozenTreasureTracker ?: return + + compactPattern.matchMatcher(message) { + storage.compactProcs += 1 + saveAndUpdate() + if (config.hideMessages) event.blockedReason = "frozen treasure tracker" + } + + for (treasure in FrozenTreasure.entries) { + if ("FROZEN TREASURE! You found ${treasure.displayName.removeColor()}!".toRegex().matches(message)) { + storage.treasuresMined += 1 + val old = storage.treasureCount[treasure] ?: 0 + storage.treasureCount = storage.treasureCount.editCopy { this[treasure] = old + 1 } + saveAndUpdate() + if (config.hideMessages) event.blockedReason = "frozen treasure tracker" + } + } + } + + @SubscribeEvent + fun onPreProfileSwitch(event: PreProfileSwitchEvent) { + display = emptyList() + } + + private fun drawTreasureDisplay(storage: Storage.ProfileSpecific.FrozenTreasureTracker) = buildList<List<Any>> { + addAsSingletonList("§1§lFrozen Treasure Tracker") + addAsSingletonList("§6${formatNumber(storage.treasuresMined)} Treasures Mined") + addAsSingletonList("§3${formatNumber(estimatedIce)} Total Ice") + addAsSingletonList("§3${formatNumber(icePerHour)} Ice/hr") + addAsSingletonList("§8${formatNumber(storage.treasuresMined)} Compact Procs") + addAsSingletonList("") + + for (treasure in FrozenTreasure.entries) { + val count = (storage.treasureCount[treasure] ?: 0) * if (config.showAsDrops) treasure.defaultAmount else 1 + addAsSingletonList("§b${formatNumber(count)} ${treasure.displayName}") + } + addAsSingletonList("") + } + + fun formatNumber(amount: Number): String { + if (amount is Int) return amount.addSeparators() + if (amount is Long) return NumberUtil.format(amount) + return "$amount" + } + + private fun saveAndUpdate() { + val storage = ProfileStorageData.profileSpecific?.frozenTreasureTracker ?: return + calculateIce(storage) + display = formatDisplay(drawTreasureDisplay(storage)) + } + + private fun calculateIce(storage: Storage.ProfileSpecific.FrozenTreasureTracker) { + estimatedIce = 0 + estimatedIce += storage.compactProcs * 160 + for (treasure in FrozenTreasure.entries) { + val amount = storage.treasureCount[treasure] ?: 0 + estimatedIce += amount * treasure.defaultAmount * treasure.iceMultiplier + } + } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) { + if (!config.enabled) return + if (!onJerryWorkshop()) return + if (config.onlyInCave && !inGlacialCave()) return + config.position.renderStringsAndItems(display, posLabel = "Frozen Treasure Tracker") + } + + @SubscribeEvent + fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { + event.move(2, "misc.frozenTreasureTracker", "event.winter.frozenTreasureTracker") + } + + private fun onJerryWorkshop() = LorenzUtils.inIsland(IslandType.WINTER) + + private fun inGlacialCave() = onJerryWorkshop() && ScoreboardData.sidebarLinesFormatted.contains(" §7⏣ §3Glacial Cave") +}
\ No newline at end of file |