diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-11-09 18:39:30 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-11-09 18:39:30 +0100 |
commit | e03ba746fa0ab6258fcd5e6855d1590b0ba6f5c9 (patch) | |
tree | 2de4b40f7e7466e52e5d1b16eb8a45c3d5f4eabe | |
parent | d2d6162ce5830099418e85b69f02b958625ad6a7 (diff) | |
download | skyhanni-e03ba746fa0ab6258fcd5e6855d1590b0ba6f5c9.tar.gz skyhanni-e03ba746fa0ab6258fcd5e6855d1590b0ba6f5c9.tar.bz2 skyhanni-e03ba746fa0ab6258fcd5e6855d1590b0ba6f5c9.zip |
SkyHanniTracker now handles the display list as well.
8 files changed, 112 insertions, 188 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/combat/endernodetracker/EnderNodeTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/combat/endernodetracker/EnderNodeTracker.kt index 1b10bf7eb..b65021067 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/combat/endernodetracker/EnderNodeTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/combat/endernodetracker/EnderNodeTracker.kt @@ -30,13 +30,15 @@ object EnderNodeTracker { private val config get() = SkyHanniMod.feature.combat.enderNodeTracker private var miteGelInInventory = 0 - private var display = emptyList<List<Any>>() - private var lootProfit = mapOf<EnderNode, Double>() private val enderNodeRegex = Regex("""ENDER NODE!.+You found (\d+x )?§r(.+)§r§f!""") private val endermanRegex = Regex("""(RARE|PET) DROP! §r(.+) §r§b\(""") - private val tracker = SkyHanniTracker("Ender Node Tracker", { Data() }, { it.enderNodeTracker }) { update() } + private val tracker = SkyHanniTracker("Ender Node Tracker", { Data() }, { it.enderNodeTracker }) { + formatDisplay( + drawDisplay(it) + ) + } class Data : TrackerData() { @@ -94,7 +96,6 @@ object EnderNodeTracker { storage.lootCount.addOrPut(it, amount) } } - update() } @SubscribeEvent @@ -119,8 +120,6 @@ object EnderNodeTracker { tracker.modify { storage -> storage.lootCount.addOrPut(EnderNode.MITE_GEL, change.delta) } - - update() } @SubscribeEvent @@ -138,7 +137,6 @@ object EnderNodeTracker { tracker.modify { storage -> storage.lootCount.addOrPut(EnderNode.MITE_GEL, change) } - update() } miteGelInInventory = newMiteGelInInventory } @@ -149,15 +147,15 @@ object EnderNodeTracker { if (!config.enabled) return if (!isInTheEnd()) return - tracker.renderDisplay(config.position, display) + tracker.renderDisplay(config.position) } @SubscribeEvent fun onConfigLoad(event: ConfigLoadEvent) { config.textFormat.afterChange { - update() + tracker.update() } - update() + tracker.update() } @SubscribeEvent @@ -183,12 +181,6 @@ object EnderNodeTracker { return newProfit } - private fun update() { - val storage = tracker.currentDisplay() ?: return - lootProfit = calculateProfit(storage) - display = formatDisplay(drawDisplay(storage)) - } - private fun isInTheEnd() = LorenzUtils.skyBlockArea == "The End" private fun isEnderArmor(displayName: EnderNode) = when (displayName) { @@ -212,7 +204,7 @@ object EnderNodeTracker { } private fun drawDisplay(storage: Data) = buildList<List<Any>> { - if (!ProfileStorageData.loaded) return emptyList<List<Any>>() + val lootProfit = calculateProfit(storage) addAsSingletonList("§5§lEnder Node Tracker") addAsSingletonList("§d${storage.totalNodesMined.addSeparators()} Ender Nodes mined") @@ -254,11 +246,7 @@ object EnderNodeTracker { val newList = mutableListOf<List<Any>>() for (index in config.textFormat.get()) { newList.add(map[index]) - if (newList.size == 1) { - tracker.addDisplayModeToggle(newList) - } } - tracker.addSessionResetButton(newList) return newList } 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 index fa10aa501..bfae2a7d8 100644 --- 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 @@ -8,7 +8,6 @@ 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.addAsSingletonList import at.hannibal2.skyhanni.utils.LorenzUtils.addOrPut import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland @@ -24,7 +23,6 @@ import kotlin.concurrent.fixedRateTimer object 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>() @@ -32,7 +30,7 @@ object FrozenTreasureTracker { private var stoppedChecks = 0 private var compactPattern = "COMPACT! You found an Enchanted Ice!".toPattern() private val tracker = SkyHanniTracker("Frozen Treasure Tracker", { Data() }, { it.frozenTreasureTracker }) - { saveAndUpdate() } + { formatDisplay(drawDisplay(it)) } init { fixedRateTimer(name = "skyhanni-frozen-treasure-tracker", period = 1000) { @@ -64,7 +62,7 @@ object FrozenTreasureTracker { icePerHour = 0 stoppedChecks = 0 icePerSecond = mutableListOf() - saveAndUpdate() + tracker.update() } private fun calculateIcePerHour() { @@ -99,11 +97,7 @@ object FrozenTreasureTracker { val newList = mutableListOf<List<Any>>() for (index in config.textFormat) { newList.add(map[index]) - if (newList.size == 1) { - tracker.addDisplayModeToggle(newList) - } } - tracker.addSessionResetButton(newList) return newList } @@ -118,7 +112,6 @@ object FrozenTreasureTracker { tracker.modify { it.compactProcs += 1 } - saveAndUpdate() if (config.hideMessages) event.blockedReason = "frozen treasure tracker" } @@ -128,18 +121,13 @@ object FrozenTreasureTracker { it.treasuresMined += 1 it.treasureCount.addOrPut(treasure, 1) } - saveAndUpdate() if (config.hideMessages) event.blockedReason = "frozen treasure tracker" } } } - @SubscribeEvent - fun onPreProfileSwitch(event: PreProfileSwitchEvent) { - display = emptyList() - } - - private fun drawTreasureDisplay(data: Data) = buildList<List<Any>> { + private fun drawDisplay(data: Data) = buildList<List<Any>> { + calculateIce(data) addAsSingletonList("§1§lFrozen Treasure Tracker") addAsSingletonList("§6${formatNumber(data.treasuresMined)} Treasures Mined") addAsSingletonList("§3${formatNumber(estimatedIce)} Total Ice") @@ -160,12 +148,6 @@ object FrozenTreasureTracker { return "$amount" } - private fun saveAndUpdate() { - val data = tracker.currentDisplay() ?: return - calculateIce(data) - display = formatDisplay(drawTreasureDisplay(data)) - } - private fun calculateIce(data: Data) { estimatedIce = data.compactProcs * 160L for (treasure in FrozenTreasure.entries) { @@ -180,7 +162,7 @@ object FrozenTreasureTracker { if (!onJerryWorkshop()) return if (config.onlyInCave && !inGlacialCave()) return - tracker.renderDisplay(config.position, display) + tracker.renderDisplay(config.position) } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/ArmorDropTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/ArmorDropTracker.kt index 3825040d0..ac9f36190 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/ArmorDropTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/ArmorDropTracker.kt @@ -2,7 +2,6 @@ package at.hannibal2.skyhanni.features.garden.farming import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator -import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.events.LorenzTickEvent @@ -27,13 +26,13 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.time.Duration.Companion.seconds object ArmorDropTracker { - private var display = emptyList<List<Any>>() private var hasArmor = false private val armorPattern = "(FERMENTO|CROPIE|SQUASH|MELON)_(LEGGINGS|CHESTPLATE|BOOTS|HELMET)".toPattern() private val config get() = SkyHanniMod.feature.garden.farmingArmorDrop - private val tracker = SkyHanniTracker("Armor Drop Tracker", { Data() }, { it.garden.armorDropTracker }) { update() } + private val tracker = SkyHanniTracker("Armor Drop Tracker", { Data() }, { it.garden.armorDropTracker }) + { drawDisplay(it) } class Data : TrackerData() { override fun reset() { @@ -52,7 +51,6 @@ object ArmorDropTracker { @SubscribeEvent fun onPreProfileSwitch(event: PreProfileSwitchEvent) { - display = emptyList() hasArmor = false } @@ -72,29 +70,14 @@ object ArmorDropTracker { tracker.modify { it.drops.addOrPut(drop, 1) } - update() } - private fun update() { - display = drawDisplay() - } - - private fun drawDisplay() = buildList<List<Any>> { - val drops = tracker.currentDisplay()?.drops ?: return@buildList - + private fun drawDisplay(data: Data): List<List<Any>> = buildList { addAsSingletonList("§7RNG Drops for Farming Armor:") - tracker.addDisplayModeToggle(this) - for ((drop, amount) in drops.sortedDesc()) { + for ((drop, amount) in data.drops.sortedDesc()) { val dropName = drop.dropName addAsSingletonList(" §7- §e${amount.addSeparators()}x $dropName") } - - tracker.addSessionResetButton(this) - } - - @SubscribeEvent - fun onConfigLoad(event: ConfigLoadEvent) { - update() } @SubscribeEvent @@ -103,7 +86,7 @@ object ArmorDropTracker { if (!config.enabled) return if (!hasArmor) return - tracker.renderDisplay(config.pos, display) + tracker.renderDisplay(config.pos) } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt index 0d95ed0ca..24f55672b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt @@ -6,7 +6,6 @@ import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.events.GardenToolChangeEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzChatEvent -import at.hannibal2.skyhanni.events.PreProfileSwitchEvent import at.hannibal2.skyhanni.features.garden.CropType import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.utils.ItemUtils.name @@ -20,10 +19,10 @@ import com.google.gson.annotations.Expose import net.minecraftforge.fml.common.eventhandler.SubscribeEvent object DicerDropTracker { - private var display = emptyList<List<Any>>() private val itemDrops = mutableListOf<ItemDrop>() private val config get() = SkyHanniMod.feature.garden.dicerCounters - private val tracker = SkyHanniTracker("Dicer Drop Tracker", { Data() }, { it.garden.dicerDropTracker }) { update() } + private val tracker = SkyHanniTracker("Dicer Drop Tracker", { Data() }, { it.garden.dicerDropTracker }) + { drawDisplay(it) } class Data : TrackerData() { override fun reset() { @@ -54,11 +53,6 @@ object DicerDropTracker { } @SubscribeEvent - fun onPreProfileSwitch(event: PreProfileSwitchEvent) { - display = emptyList() - } - - @SubscribeEvent fun onChat(event: LorenzChatEvent) { if (!GardenAPI.inGarden()) return if (!config.hideChat && !config.display) return @@ -75,23 +69,15 @@ object DicerDropTracker { } } - private fun update() { - tracker.currentDisplay()?.let { - display = drawDisplay(it) - } - } - private fun drawDisplay(storage: Data) = buildList<List<Any>> { val cropInHand = cropInHand ?: return@buildList val items = storage.drops.getOrPut(cropInHand) { mutableMapOf() } addAsSingletonList("§7Dicer Drop Tracker for $toolName§7:") - tracker.addDisplayModeToggle(this) for ((rarity, amount) in items.sortedDesc()) { val displayName = rarity.displayName addAsSingletonList(" §7- §e${amount.addSeparators()}x $displayName") } - tracker.addSessionResetButton(this) } private var cropInHand: CropType? = null @@ -104,7 +90,7 @@ object DicerDropTracker { if (cropInHand != null) { toolName = event.toolItem!!.name!! } - update() + tracker.update() } private fun addDrop(crop: CropType, rarity: DropRarity) { @@ -112,14 +98,13 @@ object DicerDropTracker { val map = it.drops.getOrPut(crop) { mutableMapOf() } map.addOrPut(rarity, 1) } - update() } @SubscribeEvent fun onRenderOverlay(event: GuiRenderEvent) { if (!isEnabled()) return - tracker.renderDisplay(config.pos, display) + tracker.renderDisplay(config.pos) } class ItemDrop(val crop: CropType, val rarity: DropRarity, val pattern: Regex) diff --git a/src/main/java/at/hannibal2/skyhanni/features/mining/powdertracker/PowderTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/mining/powdertracker/PowderTracker.kt index fa2506069..359fb40b9 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/mining/powdertracker/PowderTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/mining/powdertracker/PowderTracker.kt @@ -24,7 +24,6 @@ import kotlin.concurrent.fixedRateTimer object PowderTracker { private val config get() = SkyHanniMod.feature.mining.powderTracker - private var display = emptyList<List<Any>>() private val picked = "§6You have successfully picked the lock on this chest!".toPattern() private val uncovered = "§aYou uncovered a treasure chest!".toPattern() private val powderEvent = ".*§r§b§l2X POWDER STARTED!.*".toPattern() @@ -59,7 +58,8 @@ object PowderTracker { } } - private val tracker = SkyHanniTracker("Powder Tracker", { Data() }, { it.powderTracker }) { saveAndUpdate() } + private val tracker = SkyHanniTracker("Powder Tracker", { Data() }, { it.powderTracker }) + { formatDisplay(drawDisplay(it)) } class Data : TrackerData() { override fun reset() { @@ -80,7 +80,7 @@ object PowderTracker { if (config.onlyWhenPowderGrinding && !isGrinding) return - tracker.renderDisplay(config.position, display) + tracker.renderDisplay(config.position) } @SubscribeEvent @@ -120,7 +120,7 @@ object PowderTracker { } } } - saveAndUpdate() + tracker.update() } @SubscribeEvent @@ -132,7 +132,7 @@ object PowderTracker { powderTimer = group("time") doublePowder = powderTimer != "00:00" - saveAndUpdate() + tracker.update() } } if (System.currentTimeMillis() - lastChestPicked > 60_000) { @@ -142,7 +142,9 @@ object PowderTracker { @SubscribeEvent fun onConfigLoad(event: ConfigLoadEvent) { - config.textFormat.afterChange { saveAndUpdate() } + config.textFormat.afterChange { + tracker.update() + } } @SubscribeEvent @@ -164,7 +166,7 @@ object PowderTracker { chestInfo.stoppedChecks = 0 chestInfo.perMin.clear() doublePowder = false - saveAndUpdate() + tracker.update() } @SubscribeEvent @@ -176,40 +178,32 @@ object PowderTracker { } } - private fun saveAndUpdate() { - calculate(gemstoneInfo, PowderChestReward.GEMSTONE_POWDER) - calculate(mithrilInfo, PowderChestReward.MITHRIL_POWDER) - calculate(diamondEssenceInfo, PowderChestReward.DIAMOND_ESSENCE) - calculate(goldEssenceInfo, PowderChestReward.GOLD_ESSENCE) - calculateChest() - display = formatDisplay(drawDisplay()) - } - private fun formatDisplay(map: List<List<Any>>) = buildList { if (map.isEmpty()) return@buildList for (index in config.textFormat.get()) { add(map[index]) } - - tracker.addSessionResetButton(this) } - private fun drawDisplay() = buildList<List<Any>> { + private fun drawDisplay(data: Data): List<List<Any>> = buildList { + calculate(data, gemstoneInfo, PowderChestReward.GEMSTONE_POWDER) + calculate(data, mithrilInfo, PowderChestReward.MITHRIL_POWDER) + calculate(data, diamondEssenceInfo, PowderChestReward.DIAMOND_ESSENCE) + calculate(data, goldEssenceInfo, PowderChestReward.GOLD_ESSENCE) + calculateChest(data) + addAsSingletonList("§b§lPowder Tracker") - tracker.addDisplayModeToggle(this) if (!tracker.isInventoryOpen()) { addAsSingletonList("") } - val display = tracker.currentDisplay() ?: return@buildList - val chestPerHour = format(chestInfo.perHour) - addAsSingletonList("§d${display.totalChestPicked.addSeparators()} Total Chests Picked §7($chestPerHour/h)") + addAsSingletonList("§d${data.totalChestPicked.addSeparators()} Total Chests Picked §7($chestPerHour/h)") addAsSingletonList("§bDouble Powder: ${if (doublePowder) "§aActive! §7($powderTimer)" else "§cInactive!"}") val entries = PowderChestReward.entries - val rewards = display.rewards + val rewards = data.rewards addPerHour(rewards, entries[0], mithrilInfo) addPerHour(rewards, entries[1], gemstoneInfo) addAsSingletonList("") @@ -296,15 +290,12 @@ object PowderTracker { resourceInfo.stoppedChecks = 0 } - private fun calculate(info: ResourceInfo, reward: PowderChestReward) { - val display = tracker.currentDisplay() ?: return - val rewards = display.rewards - info.estimated = rewards.getOrDefault(reward, 0) + private fun calculate(display: Data, info: ResourceInfo, reward: PowderChestReward) { + info.estimated = display.rewards.getOrDefault(reward, 0) } - private fun calculateChest() { - val display = tracker.currentDisplay() ?: return - chestInfo.estimated = display.totalChestPicked.toLong() + private fun calculateChest(data: Data) { + chestInfo.estimated = data.totalChestPicked.toLong() } private fun convert(roughCount: Long): Gem { diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt index 7efaed3d3..229d9007c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt @@ -48,7 +48,6 @@ object SlayerProfitTracker { private var itemLogCategory = "" private var baseSlayerType = "" - private var display = emptyList<List<Any>>() private val logger = LorenzLogger("slayer/profit_tracker") private var lastClickDelay = 0L private val trackers = mutableMapOf<String, SkyHanniTracker<Data>>() @@ -106,7 +105,6 @@ object SlayerProfitTracker { getTracker()?.modify { it.slayerSpawnCost += price } - update() } private var allowedItems = mapOf<String, List<NEUInternalName>>() @@ -135,14 +133,13 @@ object SlayerProfitTracker { val newSlayer = event.newSlayer itemLogCategory = newSlayer.removeColor() baseSlayerType = itemLogCategory.substringBeforeLast(" ") - update() + getTracker()?.update() } private fun addMobKillCoins(coins: Int) { getTracker()?.modify { it.mobKillCoins += coins } - update() } private fun addItemPickup(internalName: NEUInternalName, stackSize: Int) { @@ -152,8 +149,6 @@ object SlayerProfitTracker { slayerItemProfit.timesDropped++ slayerItemProfit.totalAmount += stackSize } - - update() } private fun getTracker(): SkyHanniTracker<Data>? { @@ -165,7 +160,7 @@ object SlayerProfitTracker { itemLogCategory ) { Data() } } - SkyHanniTracker("$itemLogCategory Profit Tracker", { Data() }, getStorage) { update() } + SkyHanniTracker("$itemLogCategory Profit Tracker", { Data() }, getStorage) { drawDisplay(it) } } } @@ -174,8 +169,6 @@ object SlayerProfitTracker { getTracker()?.modify { it.slayerCompletedCount++ } - - update() } @SubscribeEvent @@ -238,16 +231,9 @@ object SlayerProfitTracker { return internalName in allowedList } - fun update() { - val tracker = getTracker() ?: return - display = drawDisplay(tracker) - } - - private fun drawDisplay(tracker: SkyHanniTracker<Data>) = buildList<List<Any>> { - val itemLog = tracker.currentDisplay() ?: return@buildList - + private fun drawDisplay(itemLog: Data) = buildList<List<Any>> { + val tracker = getTracker() ?: return@buildList addAsSingletonList("§e§l$itemLogCategory Profit Tracker") - tracker.addDisplayModeToggle(this) var profit = 0.0 val map = mutableMapOf<Renderable, Long>() @@ -291,7 +277,7 @@ object SlayerProfitTracker { itemProfit.hidden = !hidden lastClickDelay = System.currentTimeMillis() } - update() + tracker.update() } } else Renderable.string(text) if (tracker.isInventoryOpen() || !hidden) { @@ -349,11 +335,10 @@ object SlayerProfitTracker { isCurrent = { it.ordinal == config.priceFrom }, onChange = { config.priceFrom = it.ordinal - update() + tracker.update() } ) } - tracker.addSessionResetButton(this) } private fun getPrice(internalName: NEUInternalName) = when (config.priceFrom) { @@ -368,7 +353,7 @@ object SlayerProfitTracker { if (!isEnabled()) return if (!SlayerAPI.isInCorrectArea) return - getTracker()?.renderDisplay(config.pos, display) + getTracker()?.renderDisplay(config.pos) } fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index e6bc74e06..4f0704ead 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -334,22 +334,29 @@ object LorenzUtils { isCurrent: (T) -> Boolean, crossinline onChange: (T) -> Unit, ) { - add(buildList { - add(prefix) - for (entry in enumValues<T>()) { - val display = getName(entry) - if (isCurrent(entry)) { - add("§a[$display]") - } else { - add("§e[") - add(Renderable.link("§e$display") { - onChange(entry) - }) - add("§e]") - } - add(" ") + add(buildSelector<T>(prefix, getName, isCurrent, onChange)) + } + + inline fun <reified T : Enum<T>> buildSelector( + prefix: String, + getName: (T) -> String, + isCurrent: (T) -> Boolean, + crossinline onChange: (T) -> Unit + ) = buildList { + add(prefix) + for (entry in enumValues<T>()) { + val display = getName(entry) + if (isCurrent(entry)) { + add("§a[$display]") + } else { + add("§e[") + add(Renderable.link("§e$display") { + onChange(entry) + }) + add("§e]") } - }) + add(" ") + } } inline fun MutableList<List<Any>>.addButton( diff --git a/src/main/java/at/hannibal2/skyhanni/utils/tracker/SkyHanniTracker.kt b/src/main/java/at/hannibal2/skyhanni/utils/tracker/SkyHanniTracker.kt index ec0fc4848..d1b783f7e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/tracker/SkyHanniTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/tracker/SkyHanniTracker.kt @@ -5,7 +5,6 @@ import at.hannibal2.skyhanni.config.core.config.Position import at.hannibal2.skyhanni.data.ProfileStorageData import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList -import at.hannibal2.skyhanni.utils.LorenzUtils.addSelector import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems import at.hannibal2.skyhanni.utils.renderables.Renderable import net.minecraft.client.Minecraft @@ -15,46 +14,15 @@ class SkyHanniTracker<Data : TrackerData>( private val name: String, private val createNewSession: () -> Data, private val getStorage: (Storage.ProfileSpecific) -> Data, - private val update: () -> Unit, + private val drawDisplay: (Data) -> List<List<Any>>, ) { private var inventoryOpen = false private var displayMode = DisplayMode.TOTAL private val currentSessions = mutableMapOf<Storage.ProfileSpecific, Data>() + private var display = emptyList<List<Any>>() fun isInventoryOpen() = inventoryOpen - fun addSessionResetButton(list: MutableList<List<Any>>) { - if (!inventoryOpen || displayMode != DisplayMode.SESSION) return - - list.addAsSingletonList( - Renderable.clickAndHover( - "§cReset session!", - listOf( - "§cThis will reset your", - "§ccurrent session of", - "§c$name" - ), - ) { - reset(DisplayMode.SESSION, "§e[SkyHanni] Reset this session of $name!") - }) - } - - fun addDisplayModeToggle(list: MutableList<List<Any>>) { - if (!inventoryOpen) return - - list.addSelector<DisplayMode>( - "§7Display Mode: ", - getName = { type -> type.displayName }, - isCurrent = { it == displayMode }, - onChange = { - displayMode = it - update() - } - ) - } - - fun currentDisplay() = getSharedTracker()?.get(displayMode) - fun resetCommand(args: Array<String>, command: String) { if (args.size == 1 && args[0].lowercase() == "confirm") { reset(DisplayMode.TOTAL, "§e[SkyHanni] Reset total $name!") @@ -69,9 +37,10 @@ class SkyHanniTracker<Data : TrackerData>( fun modify(modifyFunction: (Data) -> Unit) { getSharedTracker()?.modify(modifyFunction) + update() } - fun renderDisplay(position: Position, display: List<List<Any>>) { + fun renderDisplay(position: Position) { val currentlyOpen = Minecraft.getMinecraft().currentScreen is GuiInventory if (inventoryOpen != currentlyOpen) { inventoryOpen = currentlyOpen @@ -81,6 +50,40 @@ class SkyHanniTracker<Data : TrackerData>( position.renderStringsAndItems(display, posLabel = name) } + fun update() { + display = currentDisplay()?.let { + val list = drawDisplay(it).toMutableList() + if (inventoryOpen) { + list.add(1, LorenzUtils.buildSelector<DisplayMode>( + "§7Display Mode: ", + getName = { type -> type.displayName }, + isCurrent = { it == displayMode }, + onChange = { + displayMode = it + update() + } + )) + } + if (inventoryOpen && displayMode == DisplayMode.SESSION) { + list.addAsSingletonList( + Renderable.clickAndHover( + "§cReset session!", + listOf( + "§cThis will reset your", + "§ccurrent session of", + "§c$name" + ), + ) { + reset(DisplayMode.SESSION, "§e[SkyHanni] Reset this session of $name!") + }) + } + + list + } ?: emptyList() + } + + private fun currentDisplay() = getSharedTracker()?.get(displayMode) + private fun getSharedTracker(): SharedTracker<Data>? { val profileSpecific = ProfileStorageData.profileSpecific ?: return null return SharedTracker(getStorage(profileSpecific), getCurrentSession(profileSpecific)) |