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.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.events.GuiContainerEvent
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.misc.items.EstimatedItemValue
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.LorenzUtils.addButton
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.SpecialColour
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.init.Items
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color
class ChestValue {
private val config get() = SkyHanniMod.feature.inventory.chestValueConfig
private var display = emptyList<List<Any>>()
private val chestItems = mutableMapOf<NEUInternalName, Item>()
private val inInventory get() = isValidStorage()
@SubscribeEvent
fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
if (!isEnabled()) return
if (InventoryUtils.openInventoryName() == "") return
if (inInventory) {
config.position.renderStringsAndItems(
display,
extraSpace = -1,
itemScale = 1.3,
posLabel = "Estimated Chest Value"
)
}
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
if (!isEnabled()) return
if (event.isMod(5)) {
update()
}
}
@SubscribeEvent
fun onInventoryOpen(event: InventoryOpenEvent) {
if (!isEnabled()) return
if (inInventory) {
update()
}
}
@SubscribeEvent
fun onInventoryClose(event: InventoryCloseEvent) {
chestItems.clear()
Renderable.list.clear()
}
@SubscribeEvent(priority = EventPriority.LOW)
fun onDrawBackground(event: GuiContainerEvent.BackgroundDrawnEvent) {
if (!isEnabled()) return
if (!config.enableHighlight) return
if (inInventory) {
for ((_, indexes) in Renderable.list) {
for (slot in InventoryUtils.getItemsInOpenChest()) {
if (indexes.contains(slot.slotIndex)) {
slot highlight Color(SpecialColour.specialToChromaRGB(config.highlightColor), true)
}
}
}
}
}
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("§7Estimated Chest Value: §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:"
val width = Minecraft.getMinecraft().fontRendererObj.getStringWidth(textAmount)
val name = "${stack.displayName.reduceStringLength((config.nameLength - width), ' ')} $textAmount"
val price = "§b${(total).formatPrice()}"
val text = if (config.alignedDisplay)
"$name $price"
else
"${stack.displayName} §7x$amount: §b${total.formatPrice()}"
newDisplay.add(buildList {
val renderable = Renderable.hoverTips(
text,
tips,
stack = stack,
indexes = index
)
add(" §7- ")
if (config.showStacks) add(stack)
add(renderable)
})
rendered++
}
newDisplay.addAsSingletonList("§6Total value : §b${totalPrice.formatPrice()}")
}
private fun sortedList(): MutableList<Item> {
return when (config.sortingType) {
0 -> chestItems.values.sortedByDescending { it.total }
1 -> chestItems.values.sortedBy { it.total }
else -> chestItems.values.sortedByDescending { it.total }
}.toMutableList()
}
private fun addButton(newDisplay: MutableList<List<Any>>) {
newDisplay.addButton("§7Sorted By: ",
getName = SortType.entries[config.sortingType].longName,
onChange = {
config.sortingType = (config.sortingType + 1) % 2
update()
})
newDisplay.addButton("§7Value format: ",
getName = FormatType.entries[config.formatType].type,
onChange = {
config.formatType = (config.formatType + 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) {
val isMinion = InventoryUtils.openInventoryName().contains(" Minion ")
val slots = 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>()
val pair = EstimatedItemValue.getEstimatedItemPrice(stack, list)
var (total, _) = pair
if (stack.item == Items.enchanted_book)
total /= 2
list.add("§aTotal: §6§l${total.formatPrice()}")
if (total == 0.0) continue
val item = chestItems.getOrPut(internalName) {
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 {
return when (config.formatType) {
0 -> if (this > 1_000_000_000) NumberUtil.format(this, true) else NumberUtil.format(this)
1 -> this.addSeparators()
else -> "0"
}
}
enum class SortType(val
|