From 4954f93cd878d269582ebe6ccc5486a3f8a5918b Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Tue, 7 Nov 2023 01:46:18 +0100 Subject: tracker core (#697) Tracker API #697 --- .../features/garden/farming/DicerDropTracker.kt | 175 +++++++++++++++++++++ .../features/garden/farming/DicerRngDropCounter.kt | 150 ------------------ .../features/mining/powdertracker/PowderTracker.kt | 72 ++++----- .../features/slayer/SlayerItemProfitTracker.kt | 100 +++--------- 4 files changed, 227 insertions(+), 270 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerRngDropCounter.kt (limited to 'src/main/java/at/hannibal2/skyhanni/features') 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 new file mode 100644 index 000000000..a3b4f73cb --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerDropTracker.kt @@ -0,0 +1,175 @@ +package at.hannibal2.skyhanni.features.garden.farming + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.config.Storage.ProfileSpecific.GardenStorage.DicerDropTracker +import at.hannibal2.skyhanni.data.ProfileStorageData +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 +import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.LorenzUtils.addOrPut +import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.tracker.DisplayMode +import at.hannibal2.skyhanni.utils.tracker.SharedTracker +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addDisplayModeToggle +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addSessionResetButton +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.inventory.GuiInventory +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object DicerDropTracker { + private var display = emptyList>() + private val itemDrops = mutableListOf() + private val config get() = SkyHanniMod.feature.garden.dicerCounters + private val currentSessionData = DicerDropTracker() + private var inventoryOpen = false + + init { + itemDrops.add(ItemDrop(CropType.MELON, DropRarity.UNCOMMON, "§a§lUNCOMMON DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Melon§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.MELON, DropRarity.RARE, "§9§lRARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Melon§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.MELON, DropRarity.CRAZY_RARE, "§d§lCRAZY RARE DROP! §r§eDicer dropped §r§[a|9](\\d+)x §r§[a|9]Enchanted Melon(?: Block)?§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.MELON, DropRarity.PRAY_TO_RNGESUS, "§5§lPRAY TO RNGESUS DROP! §r§eDicer dropped §r§9(\\d+)x §r§9Enchanted Melon Block§r§e!".toRegex())) + + itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.UNCOMMON, "§a§lUNCOMMON DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.RARE, "§9§lRARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.CRAZY_RARE, "§d§lCRAZY RARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) + itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.PRAY_TO_RNGESUS, "§5§lPRAY TO RNGESUS DROP! §r§eDicer dropped §r§[a|9](\\d+)x §r§(aEnchanted|9Polished) Pumpkin§r§e!".toRegex())) + } + + enum class DropRarity(val displayName: String) { + UNCOMMON("§a§lUNCOMMON DROP"), + RARE("§9§lRARE DROP"), + CRAZY_RARE("§d§lCRAZY RARE DROP"), + PRAY_TO_RNGESUS("§5§lPRAY TO RNGESUS DROP"), + } + + @SubscribeEvent + fun onPreProfileSwitch(event: PreProfileSwitchEvent) { + display = emptyList() + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!GardenAPI.inGarden()) return + if (!config.hideChat && !config.display) return + + val message = event.message + for (drop in itemDrops) { + if (drop.pattern.matches(message)) { + addDrop(drop.crop, drop.rarity) + if (config.hideChat) { + event.blockedReason = "dicer_drop_tracker" + } + return + } + } + } + + private fun update() { + currentDisplay()?.let { + display = drawDisplay(it) + } + } + + private fun drawDisplay(storage: DicerDropTracker) = buildList> { + val cropInHand = cropInHand ?: return@buildList + val items = storage.drops.getOrPut(cropInHand) { mutableMapOf() } + addAsSingletonList("§7Dicer Drop Tracker for $toolName§7:") + if (inventoryOpen) { + addDisplayModeToggle { + update() + } + } + for ((rarity, amount) in items.sortedDesc()) { + val displayName = rarity.displayName + addAsSingletonList(" §7- §e${amount.addSeparators()}x $displayName") + } + + if (inventoryOpen && TrackerUtils.currentDisplayMode == DisplayMode.CURRENT) { + addSessionResetButton("Dicer Drop Tracker", getSharedTracker()) { + update() + } + } + } + + private var cropInHand: CropType? = null + private var toolName = "" + + @SubscribeEvent + fun onGardenToolChange(event: GardenToolChangeEvent) { + val crop = event.crop + cropInHand = if (crop == CropType.MELON || crop == CropType.PUMPKIN) crop else null + if (cropInHand != null) { + toolName = event.toolItem!!.name!! + } + update() + } + + private fun addDrop(crop: CropType, rarity: DropRarity) { + val sharedTracker = getSharedTracker() ?: return + sharedTracker.modify { + val map = it.drops.getOrPut(crop) { mutableMapOf() } + map.addOrPut(rarity, 1) + } + update() + } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent) { + if (!isEnabled()) return + + val currentlyOpen = Minecraft.getMinecraft().currentScreen is GuiInventory + if (inventoryOpen != currentlyOpen) { + inventoryOpen = currentlyOpen + update() + } + config.pos.renderStringsAndItems(display, posLabel = "Dicer Drop Tracker") + } + + class ItemDrop(val crop: CropType, val rarity: DropRarity, val pattern: Regex) + + fun isEnabled() = GardenAPI.inGarden() && config.display + + @SubscribeEvent + fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { + event.move(3, "garden.dicerCounterDisplay", "garden.dicerCounters.display") + event.move(3, "garden.dicerCounterHideChat", "garden.dicerCounters.hideChat") + event.move(3, "garden.dicerCounterPos", "garden.dicerCounters.pos") + + event.move(7, "#profile.garden.dicerRngDrops", "#profile.garden.dicerDropTracker.drops") { old -> + val items: MutableMap> = mutableMapOf() + val oldItems = ConfigManager.gson.fromJson>(old, Map::class.java) + for ((internalName, amount) in oldItems) { + val split = internalName.split(".") + val crop = CropType.getByName(split[0]) + val rarityName = split[1] + val rarity = DropRarity.valueOf(rarityName) + items.getOrPut(crop) { mutableMapOf() }[rarity] = amount + } + + ConfigManager.gson.toJsonTree(items) + } + } + + private fun currentDisplay() = getSharedTracker()?.getCurrent() + + private fun getSharedTracker(): SharedTracker? { + val profileSpecific = ProfileStorageData.profileSpecific ?: return null + return SharedTracker(profileSpecific.garden.dicerDropTracker, currentSessionData) + } + + fun resetCommand(args: Array) { + TrackerUtils.resetCommand("Dicer Drop Tracker", "shresetdicertracker", args, getSharedTracker()) { + update() + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerRngDropCounter.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerRngDropCounter.kt deleted file mode 100644 index d72540720..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/DicerRngDropCounter.kt +++ /dev/null @@ -1,150 +0,0 @@ -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.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 -import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc -import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators -import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class DicerRngDropCounter { - private var display = emptyList() - private val drops = mutableMapOf>() - private val itemDrops = mutableListOf() - private val config get() = SkyHanniMod.feature.garden.dicerCounters - - init { - initDrops() - itemDrops.add(ItemDrop(CropType.MELON, DropRarity.UNCOMMON, "§a§lUNCOMMON DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Melon§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.MELON, DropRarity.RARE, "§9§lRARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Melon§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.MELON, DropRarity.CRAZY_RARE, "§d§lCRAZY RARE DROP! §r§eDicer dropped §r§[a|9](\\d+)x §r§[a|9]Enchanted Melon(?: Block)?§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.MELON, DropRarity.PRAY_TO_RNGESUS, "§5§lPRAY TO RNGESUS DROP! §r§eDicer dropped §r§9(\\d+)x §r§9Enchanted Melon Block§r§e!".toRegex())) - - itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.UNCOMMON, "§a§lUNCOMMON DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.RARE, "§9§lRARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.CRAZY_RARE, "§d§lCRAZY RARE DROP! §r§eDicer dropped §r§a(\\d+)x §r§aEnchanted Pumpkin§r§e!".toRegex())) - itemDrops.add(ItemDrop(CropType.PUMPKIN, DropRarity.PRAY_TO_RNGESUS, "§5§lPRAY TO RNGESUS DROP! §r§eDicer dropped §r§[a|9](\\d+)x §r§(aEnchanted|9Polished) Pumpkin§r§e!".toRegex())) - } - - private fun initDrops() { - drops[CropType.MELON] = mutableMapOf() - drops[CropType.PUMPKIN] = mutableMapOf() - } - - enum class DropRarity(val displayName: String) { - UNCOMMON("§a§lUNCOMMON DROP"), - RARE("§9§lRARE DROP"), - CRAZY_RARE("§d§lCRAZY RARE DROP"), - PRAY_TO_RNGESUS("§5§lPRAY TO RNGESUS DROP"), - } - - @SubscribeEvent - fun onPreProfileSwitch(event: PreProfileSwitchEvent) { - display = emptyList() - drops.clear() - initDrops() - } - - @SubscribeEvent - fun onChat(event: LorenzChatEvent) { - if (!GardenAPI.inGarden()) return - if (!config.hideChat && !config.display) return - - val message = event.message - for (drop in itemDrops) { - if (drop.pattern.matches(message)) { - addDrop(drop.crop, drop.rarity) - saveConfig() - update() - if (config.hideChat) { - event.blockedReason = "dicer_rng_drop_counter" - } - return - } - } - } - - private fun update() { - display = drawDisplay() - } - - private fun drawDisplay(): List { - val help = mutableListOf() - val items = drops[cropInHand] ?: return help - help.add("§7RNG Drops for $toolName§7:") - for ((rarity, amount) in items.sortedDesc()) { - val displayName = rarity.displayName - help.add(" §7- §e${amount.addSeparators()}x $displayName") - } - - return help - } - - private var cropInHand: CropType? = null - private var toolName = "" - - @SubscribeEvent - fun onGardenToolChange(event: GardenToolChangeEvent) { - val crop = event.crop - cropInHand = if (crop == CropType.MELON || crop == CropType.PUMPKIN) crop else null - if (cropInHand != null) { - toolName = event.toolItem!!.name!! - } - update() - } - - private fun addDrop(crop: CropType, rarity: DropRarity) { - val map = drops[crop]!! - val old = map[rarity] ?: 0 - map[rarity] = old + 1 - } - - @SubscribeEvent - fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) { - if (isEnabled()) { - config.pos.renderStrings(display, posLabel = "Dicer Counter") - } - } - - class ItemDrop(val crop: CropType, val rarity: DropRarity, val pattern: Regex) - - private fun saveConfig() { - val map = GardenAPI.storage?.dicerRngDrops ?: return - map.clear() - for (drop in drops) { - val crop = drop.key - for ((rarity, amount) in drop.value) { - map[crop.cropName + "." + rarity.name] = amount - } - } - } - - @SubscribeEvent - fun onConfigLoad(event: ConfigLoadEvent) { - val map = GardenAPI.storage?.dicerRngDrops ?: return - for ((internalName, amount) in map) { - val split = internalName.split(".") - val crop = CropType.getByName(split[0]) - val rarityName = split[1] - val rarity = DropRarity.valueOf(rarityName) - drops[crop]!![rarity] = amount - } - } - - fun isEnabled() = GardenAPI.inGarden() && config.display - - @SubscribeEvent - fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { - event.move(3, "garden.dicerCounterDisplay", "garden.dicerCounters.display") - event.move(3, "garden.dicerCounterHideChat", "garden.dicerCounters.hideChat") - event.move(3, "garden.dicerCounterPos", "garden.dicerCounters.pos") - } -} 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 9db8553aa..7269797cb 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 @@ -12,19 +12,23 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent 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.LorenzUtils.afterChange import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.tracker.DisplayMode +import at.hannibal2.skyhanni.utils.tracker.SharedTracker +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addDisplayModeToggle +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addSessionResetButton import net.minecraft.client.Minecraft import net.minecraft.client.gui.inventory.GuiInventory import net.minecraft.entity.boss.BossStatus import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.concurrent.fixedRateTimer -class PowderTracker { +object PowderTracker { private val config get() = SkyHanniMod.feature.mining.powderTracker private var display = emptyList>() @@ -42,7 +46,6 @@ class PowderTracker { private val chestInfo = ResourceInfo(0L, 0L, 0, 0.0, mutableListOf()) private var doublePowder = false private var powderTimer = "" - private var currentDisplayMode = DisplayMode.TOTAL private var inventoryOpen = false private var currentSessionData = mutableMapOf() private val gemstones = listOf( @@ -87,7 +90,7 @@ class PowderTracker { fun onChat(event: LorenzChatEvent) { if (!isEnabled()) return val msg = event.message - val both = currentLog() ?: return + val both = getSharedTracker() ?: return if (config.greatExplorerMaxed) { uncovered.matchMatcher(msg) { @@ -187,26 +190,25 @@ class PowderTracker { for (index in config.textFormat.get()) { add(map[index]) } + + if (inventoryOpen && TrackerUtils.currentDisplayMode == DisplayMode.CURRENT) { + addSessionResetButton("Powder Tracker", getSharedTracker()) { + saveAndUpdate() + } + } } private fun drawDisplay() = buildList> { addAsSingletonList("§b§lPowder Tracker") if (inventoryOpen) { - addSelector( - "§7Display Mode: ", - getName = { type -> type.displayName }, - isCurrent = { it == currentDisplayMode }, - onChange = { - currentDisplayMode = it - saveAndUpdate() - } - ) + addDisplayModeToggle { + saveAndUpdate() + } } else { addAsSingletonList("") } - val both = currentLog() ?: return@buildList - val display = both.get(currentDisplayMode) + val display = currentDisplay() ?: return@buildList val chestPerHour = format(chestInfo.perHour) addAsSingletonList("§d${display.totalChestPicked.addSeparators()} Total Chests Picked §7($chestPerHour/h)") @@ -262,13 +264,13 @@ class PowderTracker { val count = rewards.getOrDefault(reward, 0).addSeparators() addAsSingletonList("§b$count ${reward.displayName}") } - } private fun MutableList>.addPerHour( map: MutableMap, reward: PowderChestReward, - info: ResourceInfo) { + info: ResourceInfo + ) { val mithrilCount = map.getOrDefault(reward, 0).addSeparators() val mithrilPerHour = format(info.perHour) addAsSingletonList("§b$mithrilCount ${reward.displayName} §7($mithrilPerHour/h)") @@ -301,16 +303,14 @@ class PowderTracker { } private fun calculate(info: ResourceInfo, reward: PowderChestReward) { - val both = currentLog() ?: return - val display = both.get(currentDisplayMode) + val display = currentDisplay() ?: return val rewards = display.rewards info.estimated = 0 info.estimated += rewards.getOrDefault(reward, 0) } private fun calculateChest() { - val both = currentLog() ?: return - val display = both.get(currentDisplayMode) + val display = currentDisplay() ?: return chestInfo.estimated = 0 chestInfo.estimated += display.totalChestPicked } @@ -342,37 +342,23 @@ class PowderTracker { val perMin: MutableList ) - enum class DisplayMode(val displayName: String) { - TOTAL("Total"), - CURRENT("This Session"), - ; - } + private fun currentDisplay() = getSharedTracker()?.getCurrent() - private fun currentLog(): AbstractPowderTracker? { + private fun getSharedTracker(): SharedTracker? { val profileSpecific = ProfileStorageData.profileSpecific ?: return null - return AbstractPowderTracker( + return SharedTracker( profileSpecific.powderTracker.getOrPut(0) { Storage.ProfileSpecific.PowderTracker() }, currentSessionData.getOrPut(0) { Storage.ProfileSpecific.PowderTracker() } ) } - class AbstractPowderTracker( - private val total: Storage.ProfileSpecific.PowderTracker, - private val currentSession: Storage.ProfileSpecific.PowderTracker, - ) { - - fun modify(modifyFunction: (Storage.ProfileSpecific.PowderTracker) -> Unit) { - modifyFunction(total) - modifyFunction(currentSession) - } + private fun isEnabled() = + LorenzUtils.inSkyBlock && LorenzUtils.skyBlockIsland == IslandType.CRYSTAL_HOLLOWS && config.enabled - fun get(displayMode: DisplayMode) = when (displayMode) { - DisplayMode.TOTAL -> total - DisplayMode.CURRENT -> currentSession + fun resetCommand(args: Array) { + TrackerUtils.resetCommand("Powder Tracker", "shresetpowdertracker", args, getSharedTracker()) { + saveAndUpdate() } } - - private fun isEnabled() = - LorenzUtils.inSkyBlock && LorenzUtils.skyBlockIsland == IslandType.CRYSTAL_HOLLOWS && config.enabled } diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt index 2aebe7e57..71c990fc0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt @@ -33,6 +33,11 @@ import at.hannibal2.skyhanni.utils.StringUtils import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.jsonobjects.SlayerProfitTrackerItemsJson import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.tracker.DisplayMode +import at.hannibal2.skyhanni.utils.tracker.SharedTracker +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addDisplayModeToggle +import at.hannibal2.skyhanni.utils.tracker.TrackerUtils.addSessionResetButton import com.google.common.cache.CacheBuilder import net.minecraft.client.Minecraft import net.minecraft.client.gui.inventory.GuiInventory @@ -53,12 +58,10 @@ object SlayerItemProfitTracker { private val logger = LorenzLogger("slayer/item_profit_tracker") private var inventoryOpen = false private var lastClickDelay = 0L - private var currentDisplayMode = DisplayMode.TOTAL private var currentSessionData = mutableMapOf() private fun addSlayerCosts(price: Int) { - val itemLog = currentLog() ?: return - itemLog.modify { + getSharedTracker()?.modify { it.slayerSpawnCost += price } update() @@ -94,18 +97,14 @@ object SlayerItemProfitTracker { } private fun addMobKillCoins(coins: Int) { - val itemLog = currentLog() ?: return - - itemLog.modify { + getSharedTracker()?.modify { it.mobKillCoins += coins } update() } private fun addItemPickup(internalName: NEUInternalName, stackSize: Int) { - val itemLog = currentLog() ?: return - - itemLog.modify { + getSharedTracker()?.modify { val slayerItemProfit = it.items.getOrPut(internalName) { SlayerProfitList.SlayerItemProfit() } slayerItemProfit.timesDropped++ @@ -115,12 +114,13 @@ object SlayerItemProfitTracker { update() } - private fun currentLog(): AbstractSlayerProfitList? { - if (itemLogCategory == "") return null + private fun currentDisplay() = getSharedTracker()?.getCurrent() + private fun getSharedTracker(): SharedTracker? { + if (itemLogCategory == "") return null val profileSpecific = ProfileStorageData.profileSpecific ?: return null - return AbstractSlayerProfitList( + return SharedTracker( profileSpecific.slayerProfitData.getOrPut(itemLogCategory) { SlayerProfitList() }, currentSessionData.getOrPut(itemLogCategory) { SlayerProfitList() } ) @@ -128,9 +128,7 @@ object SlayerItemProfitTracker { @SubscribeEvent fun onQuestComplete(event: SlayerQuestCompleteEvent) { - val itemLog = currentLog() ?: return - - itemLog.modify { + getSharedTracker()?.modify { it.slayerCompletedCount++ } @@ -202,20 +200,13 @@ object SlayerItemProfitTracker { } private fun drawDisplay() = buildList> { - val both = currentLog() ?: return@buildList - val itemLog = both.get(currentDisplayMode) + val itemLog = currentDisplay() ?: return@buildList addAsSingletonList("§e§l$itemLogCategory Profit Tracker") if (inventoryOpen) { - addSelector( - "§7Display Mode: ", - getName = { type -> type.displayName }, - isCurrent = { it == currentDisplayMode }, - onChange = { - currentDisplayMode = it - update() - } - ) + addDisplayModeToggle { + update() + } } var profit = 0.0 @@ -322,27 +313,13 @@ object SlayerItemProfitTracker { } ) } - if (inventoryOpen && currentDisplayMode == DisplayMode.CURRENT) { - addAsSingletonList( - Renderable.clickAndHover( - "§cReset session!", - listOf("§cThis will reset your", "§ccurrent session for", "§c$itemLogCategory"), - ) { - resetData(DisplayMode.CURRENT) - update() - }) + if (inventoryOpen && TrackerUtils.currentDisplayMode == DisplayMode.CURRENT) { + addSessionResetButton("$itemLogCategory Slayer", getSharedTracker()) { + update() + } } } - private fun resetData(displayMode: DisplayMode) { - val currentLog = currentLog() ?: return - val list = currentLog.get(displayMode) - list.items.clear() - list.mobKillCoins = 0 - list.slayerSpawnCost = 0 - list.slayerCompletedCount = 0 - } - private fun getPrice(internalName: NEUInternalName) = when (config.priceFrom) { 0 -> internalName.getBazaarData()?.sellPrice ?: internalName.getPriceOrNull() ?: 0.0 1 -> internalName.getBazaarData()?.buyPrice ?: internalName.getPriceOrNull() ?: 0.0 @@ -361,53 +338,22 @@ object SlayerItemProfitTracker { update() } - config.pos.renderStringsAndItems(display, posLabel = "Slayer Item Profit Tracker") } - enum class DisplayMode(val displayName: String) { - TOTAL("Total"), - CURRENT("This Session"), - ; - } - - class AbstractSlayerProfitList( - private val total: SlayerProfitList, - private val currentSession: SlayerProfitList, - ) { - - fun modify(modifyFunction: (SlayerProfitList) -> Unit) { - modifyFunction(total) - modifyFunction(currentSession) - } - - fun get(displayMode: DisplayMode) = when (displayMode) { - DisplayMode.TOTAL -> total - DisplayMode.CURRENT -> currentSession - } - } - fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled fun clearProfitCommand(args: Array) { if (itemLogCategory == "") { LorenzUtils.chat( "§c[SkyHanni] No current slayer data found. " + - "Go to a slayer area and start the specific slayer type you want to reset the data of." + "Go to a slayer area and start the specific slayer type you want to reset the data of." ) return } - if (args.size == 1 && args[0].lowercase() == "confirm") { - resetData(DisplayMode.TOTAL) + TrackerUtils.resetCommand("$itemLogCategory Slayer", "shclearslayerprofits", args, getSharedTracker()) { update() - LorenzUtils.chat("§e[SkyHanni] You reset your $itemLogCategory slayer data!") - return } - - LorenzUtils.clickableChat( - "§e[SkyHanni] Are you sure you want to reset all your $itemLogCategory slayer data? Click here to confirm.", - "shclearslayerprofits confirm" - ) } } -- cgit