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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package at.hannibal2.skyhanni.features.slayer
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.Storage.ProfileSpecific.SlayerProfitList
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.data.SlayerAPI
import at.hannibal2.skyhanni.data.TitleUtils
import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.features.bazaar.BazaarApi.Companion.getBazaarData
import at.hannibal2.skyhanni.features.bazaar.BazaarData
import at.hannibal2.skyhanni.test.PriceSource
import at.hannibal2.skyhanni.utils.*
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.LorenzUtils.addSelector
import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc
import at.hannibal2.skyhanni.utils.NEUItems.getNpcPrice
import at.hannibal2.skyhanni.utils.NEUItems.getPrice
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.renderables.Renderable
import com.google.common.cache.CacheBuilder
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiInventory
import net.minecraft.entity.item.EntityItem
import net.minecraft.network.play.server.S0DPacketCollectItem
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.seconds
object SlayerItemProfitTracker {
private val config get() = SkyHanniMod.feature.slayer.itemProfitTracker
private var collectedCache = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.SECONDS).build<Int, Unit>()
private var itemLogCategory = ""
private var display = emptyList<List<Any>>()
private val logger = LorenzLogger("slayer/item_profit_tracker")
private var inventoryOpen = false
private var lastClickDelay = 0L
private var currentDisplayMode = DisplayMode.TOTAL
private var currentSessionData = mutableMapOf<String, SlayerProfitList>()
private fun addSlayerCosts(price: Int) {
val itemLog = currentLog() ?: return
itemLog.modify {
it.slayerSpawnCost += price
}
update()
}
@SubscribeEvent
fun onPurseChange(event: PurseChangeEvent) {
if (!isEnabled()) return
val coins = event.coins
if (event.reason == PurseChangeCause.GAIN_MOB_KILL) {
if (SlayerAPI.isInSlayerArea) {
logger.log("Coins gained for killing mobs: ${coins.addSeparators()}")
addMobKillCoins(coins.toInt())
}
}
if (event.reason == PurseChangeCause.LOSE_SLAYER_QUEST_STARTED) {
logger.log("Coins paid for starting slayer quest: ${coins.addSeparators()}")
addSlayerCosts(coins.toInt())
}
}
@SubscribeEvent
fun onSlayerChange(event: SlayerChangeEvent) {
val newSlayer = event.newSlayer
itemLogCategory = newSlayer.removeColor()
update()
}
private fun addMobKillCoins(coins: Int) {
val itemLog = currentLog() ?: return
itemLog.modify {
it.mobKillCoins += coins
}
update()
}
private fun addItemPickup(internalName: NEUInternalName, stackSize: Int) {
val itemLog = currentLog() ?: return
itemLog.modify {
val slayerItemProfit = it.items.getOrPut(internalName) { SlayerProfitList.SlayerItemProfit() }
slayerItemProfit.timesDropped++
slayerItemProfit.totalAmount += stackSize
}
update()
}
private fun currentLog(): AbstractSlayerProfitList? {
if (itemLogCategory == "") return null
val profileSpecific = ProfileStorageData.profileSpecific ?: return null
return AbstractSlayerProfitList(
profileSpecific.slayerProfitData.getOrPut(itemLogCategory) { SlayerProfitList() },
currentSessionData.getOrPut(itemLogCategory) { SlayerProfitList() }
)
}
@SubscribeEvent
fun onQuestComplete(event: SlayerQuestCompleteEvent) {
val itemLog = currentLog() ?: return
itemLog.modify {
it.slayerCompletedCount++
}
update()
}
@SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
fun onChatPacket(event: PacketEvent.ReceiveEvent) {
if (!isEnabled()) return
if (!SlayerAPI.isInSlayerArea) return
if (!SlayerAPI.hasActiveSlayerQuest()) return
val packet = event.packet
if (packet !is S0DPacketCollectItem) return
val entityID = packet.collectedItemEntityID
val item = Minecraft.getMinecraft().theWorld.getEntityByID(entityID) ?: return
if (item !is EntityItem) return
if (collectedCache.getIfPresent(entityID) != null) return
collectedCache.put(entityID, Unit)
val itemStack = item.entityItem
val name = itemStack.name ?: return
if (SlayerAPI.ignoreSlayerDrop(name)) return
val internalName = itemStack.getInternalNameOrNull() ?: return
val (itemName, price) = SlayerAPI.getItemNameAndPrice(itemStack)
addItemPickup(internalName, itemStack.stackSize)
logger.log("Coins gained for picking up an item ($itemName) ${price.addSeparators()}")
if (config.priceInChat) {
if (price > config.minimumPrice) {
LorenzUtils.chat("§e[SkyHanni] §a+Slayer Drop§7: §r$itemName")
}
}
if (config.titleWarning) {
if (price > config.minimumPriceWarning) {
TitleUtils.sendTitle("§a+ $itemName", 5.seconds)
}
}
}
fun update() {
display = drawDisplay()
}
private fun drawDisplay() = buildList<List<Any>> {
val both = currentLog() ?: return@buildList
val itemLog = both.get(currentDisplayMode)
addAsSingletonList("§e§l$itemLogCategory Profit Tracker")
if (inventoryOpen) {
addSelector<DisplayMode>(
"§7Display Mode: ",
getName = { type -> type.displayName },
isCurrent = { it == currentDisplayMode },
onChange = {
currentDisplayMode = it
update()
}
)
}
var profit = 0.0
val map = mutableMapOf<Renderable, Long>()
for ((internalName, itemProfit) in itemLog.items) {
val amount = itemProfit.totalAmount
val price = (getPrice(internalName) * amount).toLong()
val cleanName = SlayerAPI.getNameWithEnchantmentFor(internalName)
var name = cleanName
val priceFormat = NumberUtil.format(price)
val hidden = itemProfit.hidden
if (hidden) {
while (name.startsWith("§f")) {
name = name.substring(2)
}
name = StringUtils.addFormat(name, "§m")
}
val text = " §7${amount.addSeparators()}x $name§7: §6$priceFormat"
val timesDropped = itemProfit.timesDropped
val percentage = timesDropped.toDouble() / itemLog.slayerCompletedCount
val perBoss = LorenzUtils.formatPercentage(percentage.coerceAtMost(1.0))
val renderable = if (inventoryOpen) Renderable.clickAndHover(
text, listOf(
"§7Dropped §e${timesDropped.addSeparators()} §7times.",
"§7Your drop rate: §c$perBoss",
"",
"§eClick to " + (if (hidden) "show" else "hide") + "!",
|