diff options
author | David Cole <40234707+DavidArthurCole@users.noreply.github.com> | 2024-09-10 15:53:30 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-10 21:53:30 +0200 |
commit | 2f6121bc209fb28e1d9f12fe2d10bfacfb12836d (patch) | |
tree | 1dba111a8d401d596e71c2a949e0c0986cbac76b /src/main/java/at/hannibal2/skyhanni/features | |
parent | e2795490be75dd6e4ddbfa8896d99db956719d3f (diff) | |
download | skyhanni-2f6121bc209fb28e1d9f12fe2d10bfacfb12836d.tar.gz skyhanni-2f6121bc209fb28e1d9f12fe2d10bfacfb12836d.tar.bz2 skyhanni-2f6121bc209fb28e1d9f12fe2d10bfacfb12836d.zip |
Feature: Corpse Tracker (#2306)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseAPI.kt (renamed from src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseAPI.kt) | 2 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseTracker.kt | 142 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseType.kt (renamed from src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseType.kt) | 4 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/MineshaftCorpseProfitPer.kt (renamed from src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/MineshaftCorpseProfitPer.kt) | 2 |
4 files changed, 147 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseAPI.kt index e49ca462f..c8ee81288 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseAPI.kt @@ -1,4 +1,4 @@ -package at.hannibal2.skyhanni.features.mining.mineshaft +package at.hannibal2.skyhanni.features.mining.glacitemineshaft import at.hannibal2.skyhanni.data.IslandType import at.hannibal2.skyhanni.events.LorenzChatEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseTracker.kt new file mode 100644 index 000000000..77aabc1bd --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseTracker.kt @@ -0,0 +1,142 @@ +package at.hannibal2.skyhanni.features.mining.glacitemineshaft + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.IslandType +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.IslandChangeEvent +import at.hannibal2.skyhanni.events.mining.CorpseLootedEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut +import at.hannibal2.skyhanni.utils.CollectionUtils.addSearchString +import at.hannibal2.skyhanni.utils.CollectionUtils.sumAllValues +import at.hannibal2.skyhanni.utils.ItemPriceUtils.getPrice +import at.hannibal2.skyhanni.utils.ItemUtils.itemName +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland +import at.hannibal2.skyhanni.utils.NEUInternalName +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat +import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.renderables.Searchable +import at.hannibal2.skyhanni.utils.renderables.toSearchable +import at.hannibal2.skyhanni.utils.tracker.BucketedItemTrackerData +import at.hannibal2.skyhanni.utils.tracker.ItemTrackerData.TrackedItem +import at.hannibal2.skyhanni.utils.tracker.SkyHanniBucketedItemTracker +import com.google.gson.annotations.Expose +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.EnumMap + +@SkyHanniModule +object CorpseTracker { + private val config get() = SkyHanniMod.feature.mining.glaciteMineshaft.corpseTracker + + private val tracker = SkyHanniBucketedItemTracker<CorpseType, BucketData>( + "Corpse Tracker", + { BucketData() }, + { it.mining.mineshaft.corpseProfitTracker }, + { drawDisplay(it) } + ) + + class BucketData : BucketedItemTrackerData<CorpseType>() { + override fun resetItems() { + corpsesLooted = EnumMap(CorpseType::class.java) + } + + override fun getDescription(timesGained: Long): List<String> { + val divisor = 1.coerceAtLeast(getSelectedBucket()?.let { corpsesLooted[it]?.toInt() } ?: corpsesLooted.sumAllValues().toInt()) + val percentage = timesGained.toDouble() / divisor + val dropRate = LorenzUtils.formatPercentage(percentage.coerceAtMost(1.0)) + return listOf( + "§7Dropped §e${timesGained.addSeparators()} §7times.", + "§7Your drop rate: §c$dropRate.", + ) + } + + override fun getCoinName(bucket: CorpseType?, item: TrackedItem) = "<no coins>" + override fun getCoinDescription(bucket: CorpseType?, item: TrackedItem): List<String> = listOf("<no coins>") + + @Expose + var corpsesLooted: MutableMap<CorpseType, Long> = EnumMap(CorpseType::class.java) + + fun getCorpseCount(): Long = getSelectedBucket()?.let { corpsesLooted[it] } ?: corpsesLooted.values.sum() + } + + private fun addLootedCorpse(type: CorpseType) = tracker.modify { it.corpsesLooted.addOrPut(type, 1) } + + @SubscribeEvent + fun onCorpseLoot(event: CorpseLootedEvent) { + addLootedCorpse(event.corpseType) + for ((itemName, amount) in event.loot) { + NEUInternalName.fromItemNameOrNull(itemName)?.let { item -> + tracker.modify { + it.addItem(event.corpseType, item, amount) + } + } + } + } + + private fun drawDisplay(bucketData: BucketData): List<Searchable> = buildList { + addSearchString("§b§lMineshaft Corpse Profit Tracker") + tracker.addBucketSelector(this, bucketData, "Corpse Type") + + if (bucketData.getCorpseCount() == 0L) return@buildList + + var profit = tracker.drawItems(bucketData, { true }, this) + val applicableKeys: List<CorpseType> = bucketData.getSelectedBucket()?.let { listOf(it) } + ?: enumValues<CorpseType>().toList().filter { bucketData.corpsesLooted[it] != null } + var totalKeyCost = 0.0 + var totalKeyCount = 0 + val keyCostStrings = buildList { + applicableKeys.forEach { keyData -> + keyData.key?.let { key -> + val keyName = key.itemName + val price = key.getPrice() + val count = bucketData.corpsesLooted[keyData] ?: 0 + val totalPrice = price * count + if (totalPrice > 0) { + profit -= totalPrice + totalKeyCost += totalPrice + totalKeyCount += count.toInt() + add("§7${count}x $keyName§7: §c-${totalPrice.shortFormat()}") + } + } + } + } + + if (totalKeyCount > 0) { + val specificKeyFormat = if (applicableKeys.count() == 1) applicableKeys.first().key!!.itemName else "§eCorpse Keys" + val keyFormat = "§7${totalKeyCount}x $specificKeyFormat§7: §c-${totalKeyCost.shortFormat()}" + add( + if (applicableKeys.count() == 1) Renderable.string(keyFormat).toSearchable() + else Renderable.hoverTips( + keyFormat, + keyCostStrings, + ).toSearchable(), + ) + } + + add(tracker.addTotalProfit(profit, bucketData.getCorpseCount(), "loot")) + + tracker.addPriceFromButton(this) + } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent) { + if (!isEnabled()) return + tracker.renderDisplay(config.position) + } + + @SubscribeEvent + fun onIslandChange(event: IslandChangeEvent) { + if (event.newIsland == IslandType.MINESHAFT || event.newIsland == IslandType.DWARVEN_MINES) { + tracker.firstUpdate() + } + } + + fun resetCommand() { + tracker.resetCommand() + } + + fun isEnabled() = + config.enabled && IslandType.DWARVEN_MINES.isInIsland() && (!config.onlyInMineshaft || IslandType.MINESHAFT.isInIsland()) +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseType.kt b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseType.kt index 7ddfca867..42567d5f3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/CorpseType.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/CorpseType.kt @@ -1,4 +1,4 @@ -package at.hannibal2.skyhanni.features.mining.mineshaft +package at.hannibal2.skyhanni.features.mining.glacitemineshaft import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName @@ -10,4 +10,6 @@ enum class CorpseType(val displayName: String, private val keyName: String? = nu ; val key by lazy { keyName?.asInternalName() } + + override fun toString(): String = displayName } diff --git a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/MineshaftCorpseProfitPer.kt b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/MineshaftCorpseProfitPer.kt index 102391b87..eed5f2b9d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/mining/mineshaft/MineshaftCorpseProfitPer.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/mining/glacitemineshaft/MineshaftCorpseProfitPer.kt @@ -1,4 +1,4 @@ -package at.hannibal2.skyhanni.features.mining.mineshaft +package at.hannibal2.skyhanni.features.mining.glacitemineshaft import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.events.mining.CorpseLootedEvent |