summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishDisplay.kt
blob: ce6352c0ddbe2d774d78f07cca4ba0341a3cd5ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package at.hannibal2.skyhanni.features.fishing.trophy

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.features.fishing.trophyfishing.TrophyFishDisplayConfig.HideCaught
import at.hannibal2.skyhanni.config.features.fishing.trophyfishing.TrophyFishDisplayConfig.TextPart
import at.hannibal2.skyhanni.config.features.fishing.trophyfishing.TrophyFishDisplayConfig.TrophySorting
import at.hannibal2.skyhanni.config.features.fishing.trophyfishing.TrophyFishDisplayConfig.WhenToShow
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.events.fishing.TrophyFishCaughtEvent
import at.hannibal2.skyhanni.features.fishing.FishingAPI
import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValue
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.addSingleString
import at.hannibal2.skyhanni.utils.CollectionUtils.addString
import at.hannibal2.skyhanni.utils.CollectionUtils.sumAllValues
import at.hannibal2.skyhanni.utils.ConditionalUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.ItemUtils.getItemRarityOrNull
import at.hannibal2.skyhanni.utils.ItemUtils.itemName
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiInventory
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object TrophyFishDisplay {
    private val config get() = SkyHanniMod.feature.fishing.trophyFishing.display

    private var recentlyDroppedTrophies = TimeLimitedCache<NEUInternalName, TrophyRarity>(5.seconds)
    private val itemNameCache = mutableMapOf<String, NEUInternalName>()

    private var display = emptyList<Renderable>()

    @SubscribeEvent
    fun onIslandChange(event: IslandChangeEvent) {
        if (event.newIsland == IslandType.CRIMSON_ISLE) {
            DelayedRun.runDelayed(200.milliseconds) {
                update()
            }
        }
    }

    @HandleEvent
    fun onTrophyFishCaught(event: TrophyFishCaughtEvent) {
        recentlyDroppedTrophies[getInternalName(event.trophyFishName)] = event.rarity
        update()
        DelayedRun.runDelayed(5.1.seconds) {
            update()
        }
    }

    @SubscribeEvent
    fun onProfileJoin(event: ProfileJoinEvent) {
        display = emptyList()
        update()
    }

    @SubscribeEvent
    fun onConfigReload(event: ConfigLoadEvent) {
        with(config) {
            ConditionalUtils.onToggle(
                enabled,
                highlightNew,
                extraSpace,
                sortingType,
                reverseOrder,
                textOrder,
                showCross,
                showCheckmark,
                onlyShowMissing,
            ) {
                update()
            }
        }
    }


    fun update() {
        if (!isEnabled()) return
        val list = mutableListOf<Renderable>()
        list.addString("§e§lTrophy Fish Display")
        list.add(Renderable.table(createTable(), yPadding = config.extraSpace.get()))

        display = list
    }

    private fun createTable(): List<List<Renderable>> {
        val trophyFishes = TrophyFishManager.fish ?: return emptyList()
        val table = mutableListOf<List<Renderable>>()
        for ((rawName, data) in getOrder(trophyFishes)) {
            addRow(rawName, data, table)
        }
        if (table.isEmpty()) {
            get(config.onlyShowMissing.get())?.let { rarity ->
                val name = rarity.formattedString
                table.addSingleString("§eYou caught all $name Trophy Fishes")
                if (rarity != TrophyRarity.DIAMOND) {
                    table.addSingleString("§cChange §eOnly Show Missing §cin the config to show more.")
                }
            }
        }
        return table
    }

    private fun addRow(
        rawName: String,
        data: MutableMap<TrophyRarity, Int>,
        table: MutableList<List<Renderable>>,
    ) {
        get(config.onlyShowMissing.get())?.let { atLeast ->
            val list = TrophyRarity.entries.filter { it == atLeast }
            if (list.all { (data[it] ?: 0) > 0 }) {
                return
            }
        }
        val hover = TrophyFishAPI.hoverInfo(rawName)
        fun string(string: String): Renderable = hover?.let {
            Renderable.hoverTips(Renderable.string(string), tips = it.split("\n"))
        } ?: Renderable.string(string)

        val row = mutableMapOf<TextPart, Renderable>()
        row[TextPart.NAME] = string(getItemName(rawName))

        val internalName = getInternalName(rawName)
        row[TextPart.ICON] = Renderable.itemStack(internalName.getItemStack())

        val recentlyDroppedRarity = recentlyDroppedTrophies.getOrNull(internalName).takeIf { config.highlightNew.get() }

        for (rarity in TrophyRarity.entries) {
            val amount = data[rarity] ?: 0
            val recentlyDropped = rarity == recentlyDroppedRarity
            val format = if (config.showCross.get() && amount == 0) "§c✖" else {
                val color = if (recentlyDropped) "§a" else rarity.formatCode
                val numberFormat = if (config.showCheckmark.get() && amount >= 1) "§l✔" else amount.addSeparators()
                "$color$numberFormat"
            }
            row[get(rarity)] = string(format)
        }
        val total = data.sumAllValues()
        val color = if (recentlyDroppedRarity != null) "§a" else "§5"
        row[TextPart.TOTAL] = string("$color${total.addSeparators()}")

        table.add(config.textOrder.get().mapNotNull { row[it] })
    }

    private fun get(value: TrophyRarity) = when (value) {
        TrophyRarity.BRONZE -> TextPart.BRONZE
        TrophyRarity.SILVER -> TextPart.SILVER
        TrophyRarity.GOLD -> TextPart.GOLD
        TrophyRarity.DIAMOND -> TextPart.DIAMOND
    }

    private fun get(value: HideCaught) = when (value) {
        HideCaught.NONE -> null
        HideCaught.BRONZE -> TrophyRarity.BRONZE
        HideCaught.SILVER -> TrophyRarity.SILVER
        HideCaught.GOLD -> TrophyRarity.GOLD
        HideCaught.DIAMOND -> TrophyRarity.DIAMOND
    }

    private fun getOrder(trophyFishes: MutableMap<String, MutableMap<TrophyRarity, Int>>) = sort(trophyFishes).let {
        if (config.reverseOrder.get()) it.reversed() else it
    }

    private fun sort(trophyFishes: Map<String, MutableMap<TrophyRarity, Int>>): List<Map.Entry<String, MutableMap<TrophyRarity, Int>>> =
        when (config.sortingType.get()!!) {
            TrophySorting.TOTAL_AMOUNT -> trophyFishes.entries.sortedBy { it.value.sumAllValues() }

            TrophySorting.BRONZE_AMOUNT -> count(trophyFishes, TrophyRarity.BRONZE)
            TrophySorting.SILVER_AMOUNT -> count(trophyFishes, TrophyRarity.SILVER)
            TrophySorting.GOLD_AMOUNT -> count(trophyFishes, TrophyRarity.GOLD)
            TrophySorting.DIAMOND_AMOUNT -> count(trophyFishes, TrophyRarity.DIAMOND)

            TrophySorting.ITEM_RARITY -> {
                trophyFishes.entries.sortedBy { data ->
                    val name = getInternalName(data.key)
                    name.getItemStack().getItemRarityOrNull()
                }
            }

            TrophySorting.HIGHEST_RARITY -> {
                trophyFishes.entries.sortedBy { data ->
                    TrophyRarity.entries.filter {
                        data.value.contains(it)
                    }.maxByOrNull { it.ordinal }
                }
            }

            TrophySorting.NAME -> {
                trophyFishes.entries.sortedBy { data ->
                    getItemName(data.key).removeColor()
                }
            }
        }

    private fun count(
        trophyFishes: Map<String, MutableMap<TrophyRarity, Int>>, rarity: TrophyRarity,
    ) = trophyFishes.entries.sortedBy { it.value[rarity] ?: 0 }

    private fun