summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/inventory/ChestValue.kt
blob: bd032fe66cdaa7f038628fec82776e7e60d75a03 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.features.inventory.ChestValueConfig.NumberFormatEntry
import at.hannibal2.skyhanni.config.features.inventory.ChestValueConfig.SortingTypeEntry
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.features.dungeon.DungeonAPI
import at.hannibal2.skyhanni.features.inventory.bazaar.BazaarApi
import at.hannibal2.skyhanni.features.minion.MinionFeatures
import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValue
import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValueCalculator
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.ConfigUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull
import at.hannibal2.skyhanni.utils.ItemUtils.itemName
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.addButton
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.client.gui.inventory.GuiInventory
import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object ChestValue {

    private val config get() = SkyHanniMod.feature.inventory.chestValueConfig
    private var display = emptyList<List<Any>>()
    private val chestItems = mutableMapOf<String, Item>()
    private val inInventory get() = isValidStorage()
    private var inOwnInventory = false
    private var compactInventory = true

    @SubscribeEvent
    fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
        if (!isEnabled()) return
        if (DungeonAPI.inDungeon() && !config.enableInDungeons) return
        if (!inOwnInventory) {
            if (InventoryUtils.openInventoryName() == "") return
        }

        if (!config.showDuringEstimatedItemValue && EstimatedItemValue.isCurrentlyShowing()) return

        if (inInventory) {
            config.position.renderStringsAndItems(
                display,
                extraSpace = -1,
                itemScale = 0.7,
                posLabel = featureName(),
            )
        }
    }

    fun featureName() = if (inOwnInventory) "Estimated Inventory Value" else "Estimated Chest Value"

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!isEnabled()) return
        if (!event.isMod(5)) return
        val inInv = Minecraft.getMinecraft().currentScreen is GuiInventory
        inOwnInventory = inInv && config.enableInOwnInventory
        if (!inInventory) return
        update()
    }

    @SubscribeEvent
    fun onInventoryOpen(event: InventoryOpenEvent) {
        if (!isEnabled()) return
        if (inInventory) {
            update()
        }
    }

    @SubscribeEvent
    fun onInventoryClose(event: InventoryCloseEvent) {
        chestItems.clear()
    }

    private fun update() {
        display = drawDisplay()
    }

    private fun drawDisplay(): List<List<Any>> {
        val newDisplay = mutableListOf<List<Any>>()

        init()

        if (chestItems.isEmpty()) return newDisplay

        addList(newDisplay)
        addButton(newDisplay)

        return newDisplay
    }

    private fun addList(newDisplay: MutableList<List<Any>>) {
        val sortedList = sortedList()
        var totalPrice = 0.0
        var rendered = 0

        val amountShowing = if (config.itemToShow > sortedList.size) sortedList.size else config.itemToShow
        newDisplay.addAsSingletonList("§7${featureName()}: §o(Showing $amountShowing of ${sortedList.size} items)")
        for ((index, amount, stack, total, tips) in sortedList) {
            totalPrice += total
            if (rendered >= config.itemToShow) continue
            if (total < config.hideBelow) continue
            val textAmount = " §7x${amount.addSeparators()}:"
            val width = Minecraft.getMinecraft().fontRendererObj.getStringWidth(textAmount)
            val name = "${stack.itemName.reduceStringLength((config.nameLength - width), ' ')} $textAmount"
            val price = "§6${(total).formatPrice()}"
            val text = if (config.alignedDisplay)
                "$name $price"
            else
                "${stack.itemName} §7x$amount: §6${total.formatPrice()}"
            newDisplay.add(
                buildList {
                    val renderable = Renderable.hoverTips(
                        text,
                        tips,
                        stack = stack,
                        highlightsOnHoverSlots = if (config.enableHighlight) index else emptyList(),
                    )
                    add(" §7- ")
                    if (config.showStacks) add(stack)
                    add(renderable)
                },
            )
            rendered++
        }
        newDisplay.addAsSingletonList("§aTotal value: §6${totalPrice.formatPrice()} coins")
    }

    private fun sortedList() = when (config.sortingType) {
        SortingTypeEntry.DESCENDING -> chestItems.values.sortedByDescending { it.total }
        SortingTypeEntry.ASCENDING -> chestItems.values.sortedBy { it.total }
        else -> chestItems.values.sortedByDescending { it.total }
    }

    private fun addButton(newDisplay: MutableList<List<Any>>) {
        newDisplay.addButton(
            "§7Sorted By: ",
            getName = SortType.entries[config.sortingType.ordinal].longName, // todo avoid ordinal
            onChange = {
                // todo avoid ordinals
                config.sortingType = SortingTypeEntry.entries[(config.sortingType.ordinal + 1) % 2]
                update()
            },
        )

        newDisplay.addButton(
            "§7Value format: ",
            getName = FormatType.entries[config.formatType.ordinal].type, // todo avoid ordinal
            onChange = {
                // todo avoid ordinal
                config.formatType = NumberFormatEntry.entries[(config.formatType.ordinal + 1) % 2]
                update()
            },
        )

        newDisplay.addButton(
            "§7Display Type: ",
            getName = DisplayType.entries[if (config.alignedDisplay) 1 else 0].type,
            onChange = {
                config.alignedDisplay = !config.alignedDisplay
                update()
            },
        )
    }

    private fun init() {
        if (!inInventory) return
        val slots = if (inOwnInventory) {
            InventoryUtils.getSlotsInOwnInventory()
        } else {
            val isMinion = InventoryUtils.openInventoryName().contains(" Minion ")
            InventoryUtils.getItemsInOpenChest().filter {
                it.hasStack && it.inventory != Minecraft.getMinecraft().thePlayer.inventory && (!isMinion || it.slotNumber % 9 != 1)
            }
        }
        val stacks = buildMap {
            slots.forEach {
                put(it.slotIndex, it.stack)
            }
        }
        chestItems.clear()
        for ((i, stack) in stacks) {
            val internalName = stack.getInternalNameOrNull() ?: continue
            if (internalName.getItemStackOrNull() == null) continue
            val list = mutableListOf<String>()
            var total = EstimatedItemValueCalculator.calculate(stack, list).first
            val key = "$internalName+$total"
            if (stack.item == Items.enchanted_book)
                total /= 2
            list.add("§aTotal: §6§l${total.formatPrice()} coins")
            if (total == 0.0) continue
            val item = chestItems.getOrPut(key) {
                Item(mutableListOf(), 0, stack, 0.0, list)
            }
            item.index.add(i)
            item.amount += stack.stackSize
            item.total += total * stack.stackSize
        }
    }

    private fun Double.formatPrice(): String {