Skip to content

Commit

Permalink
Improvement: Powder Tracker revamped (#2394)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <[email protected]>
  • Loading branch information
Jordyrat and hannibal002 authored Sep 8, 2024
1 parent 5ac091a commit 9352ace
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
14 changes: 9 additions & 5 deletions src/main/java/at/hannibal2/skyhanni/api/HotmAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package at.hannibal2.skyhanni.api

import at.hannibal2.skyhanni.data.HotmData
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.mining.PowderGainEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemCategory
import at.hannibal2.skyhanni.utils.ItemUtils.getItemCategoryOrNull
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getDrillUpgrades
import at.hannibal2.skyhanni.utils.StringUtils.firstLetterUppercase
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.item.ItemStack
Expand All @@ -30,7 +32,7 @@ object HotmAPI {
}
} == true

enum class Powder(val displayName: String, val color: String) {
enum class PowderType(val displayName: String, val color: String) {
MITHRIL("Mithril", "§2"),
GEMSTONE("Gemstone", "§d"),
GLACITE("Glacite", "§b"),
Expand Down Expand Up @@ -71,9 +73,11 @@ object HotmAPI {
}

/** Use when new powder gets collected*/
fun gain(value: Long) {
addTotal(value)
addCurrent(value)
fun gain(difference: Long) {
ChatUtils.debug("Gained §a${difference.addSeparators()} §e${displayName} Powder")
addTotal(difference)
addCurrent(difference)
PowderGainEvent(this, difference).post()
}

fun reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public static class MiningConfig {
public HotmTree hotmTree = new HotmTree();

@Expose
public Map<HotmAPI.Powder, PowderStorage> powder = new HashMap<>();
public Map<HotmAPI.PowderType, PowderStorage> powder = new HashMap<>();

public static class PowderStorage {

Expand Down
43 changes: 34 additions & 9 deletions src/main/java/at/hannibal2/skyhanni/data/HotmData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import at.hannibal2.skyhanni.api.HotmAPI.MayhemPerk
import at.hannibal2.skyhanni.api.HotmAPI.SkymallPerk
import at.hannibal2.skyhanni.config.storage.ProfileSpecificStorage
import at.hannibal2.skyhanni.data.jsonobjects.local.HotmTree
import at.hannibal2.skyhanni.data.model.TabWidget
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.events.ScoreboardUpdateEvent
import at.hannibal2.skyhanni.events.WidgetUpdateEvent
import at.hannibal2.skyhanni.features.gui.customscoreboard.ScoreboardPattern
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
Expand All @@ -23,7 +25,6 @@ import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.RegexUtils.indexOfFirstMatch
import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst
Expand Down Expand Up @@ -475,6 +476,15 @@ enum class HotmData(
"§b§lMAYHEM! §r§7(?<perk>.*)",
)

/**
* REGEX-TEST: Mithril: §r§299,918
* REGEX-TEST: Gemstone: §r§d37,670
*/
private val powderPattern by patternGroup.pattern(
"widget.powder",
"\\s*(?<type>\\w+): (?:§.)+(?<amount>[\\d,.]+)"
)

var inInventory = false

var tokens: Int
Expand All @@ -493,7 +503,7 @@ enum class HotmData(

init {
entries.forEach { it.guiNamePattern }
HotmAPI.Powder.entries.forEach {
HotmAPI.PowderType.entries.forEach {
it.heartPattern
it.resetPattern
}
Expand Down Expand Up @@ -527,7 +537,7 @@ enum class HotmData(
it.rawLevel = 0
it.enabled = false
it.isUnlocked = false
HotmAPI.Powder.entries.forEach { it.setCurrent(it.getTotal()) }
HotmAPI.PowderType.entries.forEach { it.setCurrent(it.getTotal()) }
availableTokens = tokens
}

Expand Down Expand Up @@ -585,7 +595,7 @@ enum class HotmData(
if (isHeartItem) { // Reset on the heart Item to remove duplication
tokens = 0
availableTokens = 0
HotmAPI.Powder.entries.forEach { it.reset() }
HotmAPI.PowderType.entries.forEach { it.reset() }
heartItem = this
}

Expand All @@ -595,7 +605,7 @@ enum class HotmData(

lore@ for (line in lore) {

HotmAPI.Powder.entries.forEach {
HotmAPI.PowderType.entries.forEach {
it.pattern(isHeartItem).matchMatcher(line) {
val powder = group("powder").replace(",", "").toLong()
if (isHeartItem) {
Expand Down Expand Up @@ -654,13 +664,12 @@ enum class HotmData(
if (!LorenzUtils.inSkyBlock) return

event.scoreboard.matchFirst(ScoreboardPattern.powderPattern) {
val type = HotmAPI.Powder.entries.firstOrNull { it.displayName == group("type") } ?: return
val type = HotmAPI.PowderType.entries.firstOrNull { it.displayName == group("type") } ?: return
val amount = group("amount").formatLong()
val difference = amount - type.getCurrent()

if (difference > 0) {
type.gain(difference)
ChatUtils.debug("Gained §a${difference.addSeparators()} §e${type.displayName} Powder")
}
}
}
Expand All @@ -686,6 +695,22 @@ enum class HotmData(
}
}

@SubscribeEvent
fun onWidgetUpdate(event: WidgetUpdateEvent) {
if (!event.isWidget(TabWidget.POWDER)) return
event.lines.forEach {
powderPattern.matchMatcher(it) {
val type = HotmAPI.PowderType.entries.firstOrNull { it.displayName == group("type") } ?: return
val amount = group("amount").replace(",", "").toLong()
val difference = amount - type.getCurrent()

if (difference > 0) {
type.gain(difference)
}
}
}
}

@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
if (!LorenzUtils.inSkyBlock) return
Expand Down Expand Up @@ -733,7 +758,7 @@ enum class HotmData(

@SubscribeEvent
fun onProfileSwitch(event: ProfileJoinEvent) {
HotmAPI.Powder.entries.forEach {
HotmAPI.PowderType.entries.forEach {
if (it.getStorage() == null) {
ProfileStorageData.profileSpecific?.mining?.powder?.put(
it,
Expand All @@ -748,7 +773,7 @@ enum class HotmData(
event.title("HotM")
event.addIrrelevant {
add("Tokens : $availableTokens/$tokens")
HotmAPI.Powder.entries.forEach {
HotmAPI.PowderType.entries.forEach {
add("${it.displayName} Powder: ${it.getCurrent()}/${it.getTotal()}")
}
add("Ability: ${HotmAPI.activeMiningAbility?.printName}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package at.hannibal2.skyhanni.events.mining

import at.hannibal2.skyhanni.api.HotmAPI
import at.hannibal2.skyhanni.api.event.SkyHanniEvent

class PowderGainEvent(val powder: HotmAPI.PowderType, val amount: Long) : SkyHanniEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ private fun getQuiverShowWhen(): Boolean {
}

private fun getPowderDisplayPair() = buildList {
val powderTypes = HotmAPI.Powder.values()
val powderTypes = HotmAPI.PowderType.values()
if (informationFilteringConfig.hideEmptyLines && powderTypes.all { it.getTotal() == 0L }) {
return listOf("<hidden>" to HorizontalAlignment.LEFT)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package at.hannibal2.skyhanni.features.mining.powdertracker

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.HotmAPI
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.features.mining.PowderTrackerConfig.PowderDisplayEntry
import at.hannibal2.skyhanni.data.BossbarData
Expand All @@ -12,8 +14,10 @@ import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.mining.PowderGainEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.ConditionalUtils.afterChange
import at.hannibal2.skyhanni.utils.ConfigUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
Expand All @@ -32,6 +36,7 @@ import com.google.gson.annotations.Expose
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object PowderTracker {
Expand Down Expand Up @@ -192,20 +197,32 @@ object PowderTracker {
}

for (reward in PowderChestReward.entries) {
if (reward == PowderChestReward.MITHRIL_POWDER || reward == PowderChestReward.GEMSTONE_POWDER) return
reward.chatPattern.matchMatcher(msg) {
tracker.modify {
val count = it.rewards[reward] ?: 0
var amount = groupOrNull("amount")?.formatLong() ?: 1
if ((reward == PowderChestReward.MITHRIL_POWDER || reward == PowderChestReward.GEMSTONE_POWDER) && doublePowder) {
amount *= 2
}
val amount = groupOrNull("amount")?.formatLong() ?: 1
it.rewards[reward] = count + amount
}
}
}
tracker.update()
}

@HandleEvent(onlyOnIsland = IslandType.CRYSTAL_HOLLOWS)
fun onPowderGain(event: PowderGainEvent) {
if (lastChestPicked.passedSince() > 5.seconds) return
tracker.modify {
val reward = when (event.powder) {
HotmAPI.PowderType.GEMSTONE -> PowderChestReward.GEMSTONE_POWDER
HotmAPI.PowderType.MITHRIL -> PowderChestReward.MITHRIL_POWDER
else -> return@modify
}
it.rewards.addOrPut(reward, event.amount)
}
tracker.update()
}

@SubscribeEvent
fun onConfigLoad(event: ConfigLoadEvent) {
config.textFormat.afterChange {
Expand Down

0 comments on commit 9352ace

Please sign in to comment.