-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathUserLuckBreakdown.kt
348 lines (307 loc) · 12.5 KB
/
UserLuckBreakdown.kt
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
package at.hannibal2.skyhanni.features.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent
import at.hannibal2.skyhanni.features.skillprogress.SkillType
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.ItemUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.roundTo
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.player.inventory.ContainerLocalMenu
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object UserLuckBreakdown {
private var inMiscStats = false
private var replaceSlot: Int? = null
private var itemCreateCoolDown = SimpleTimeMark.farPast()
private var skillCalcCoolDown = SimpleTimeMark.farPast()
private val storage get() = ProfileStorageData.playerSpecific
private val config get() = SkyHanniMod.feature.misc
private lateinit var mainLuckItem: ItemStack
private val mainLuckID = "ENDER_PEARL".asInternalName()
private const val MAIN_LUCK_NAME = "§a✴ SkyHanni User Luck"
private lateinit var fillerItem: ItemStack
private var fillerID = "STAINED_GLASS_PANE".asInternalName()
private const val FILLER_NAME = " "
private lateinit var limboItem: ItemStack
private var limboID = "ENDER_PEARL".asInternalName()
private const val LIMBO_NAME = "§a✴ Limbo Personal Best"
private lateinit var skillsItem: ItemStack
private var skillsID = "DIAMOND_SWORD".asInternalName()
private const val SKILLS_NAME = "§a✴ Category: Skills"
private var showAllStats = true
/**
* REGEX-TEST: §7Show all stats: §aYes
* REGEX-TEST: §7Show all stats: §cNope
*/
private val showAllStatsPattern by RepoPattern.pattern(
"misc.statsbreakdown.showallstats",
"§7Show all stats: §.(?<toggle>.*)",
)
private const val LUCK_TOOLTIP = "§5§o §a✴ SkyHanni User Luck §f"
private var inCustomBreakdown = false
private val validItemSlots = (10..53).filter { it !in listOf(17, 18, 26, 27, 35, 36) && it !in 44..53 }
private val invalidItemSlots = (0..53).filter { it !in validItemSlots }
private var skillOverflowLuck = mutableMapOf<SkillType, Int>()
@SubscribeEvent
fun replaceItem(event: ReplaceItemEvent) {
if (!config.userluckEnabled) return
if (event.inventory !is ContainerLocalMenu) return
if (!inMiscStats) return
if (event.slot == replaceSlot && !inCustomBreakdown) {
val limboUserLuck = storage?.limbo?.userLuck ?: 0.0f
if (limboUserLuck == 0.0f && !showAllStats) return
if (itemCreateCoolDown.passedSince() > 3.seconds) {
itemCreateCoolDown = SimpleTimeMark.now()
createItems()
}
event.replace(mainLuckItem)
return
}
if (inCustomBreakdown) {
if (itemCreateCoolDown.passedSince() > 3.seconds) {
itemCreateCoolDown = SimpleTimeMark.now()
createItems()
}
checkItemSlot(event)
}
}
private fun checkItemSlot(event: ReplaceItemEvent) {
when (event.slot) {
48, 49 -> return
10 -> event.replace(skillsItem)
11 -> event.replace(limboItem)
in validItemSlots -> event.replace(null)
in invalidItemSlots -> {
if (event.originalItem.item == limboID.getItemStack().item) return
event.replace(fillerItem)
return
}
}
}
@SubscribeEvent
fun openInventory(event: InventoryOpenEvent) {
if (event.inventoryName != "Your Stats Breakdown") {
inMiscStats = false
return
}
val inventoryName = event.inventoryItems[4]?.name.orEmpty()
if (inventoryName != "§dMisc Stats") return
inMiscStats = true
replaceSlot = findValidSlot(event.inventoryItems)
val showAllStatsLore = event.inventoryItems[50]?.getLore() ?: listOf("")
for (line in showAllStatsLore) {
showAllStatsPattern.matchMatcher(line) {
showAllStats = when (group("toggle")) {
"Yes" -> true
else -> false
}
}
}
return
}
@SubscribeEvent
fun closeInventory(event: InventoryCloseEvent) {
inMiscStats = false
inCustomBreakdown = false
}
private fun findValidSlot(input: Map<Int, ItemStack>): Int? {
for (slot in input.keys) {
if (slot !in validItemSlots && slot < 44) continue
val itemStack = input[slot]
if (itemStack?.name == " ") {
return slot
}
}
return null
}
@SubscribeEvent
fun onHoverItem(event: LorenzToolTipEvent) {
if (!config.userluckEnabled) return
if (!LorenzUtils.inSkyBlock) return
if (skillCalcCoolDown.passedSince() > 3.seconds) {
skillCalcCoolDown = SimpleTimeMark.now()
calcSkillLuck()
}
val limboLuck = storage?.limbo?.userLuck?.roundTo(1) ?: 0.0f
when (event.slot.inventory.name) {
"Your Equipment and Stats" -> equipmentMenuTooltip(event, limboLuck)
"Your Stats Breakdown" -> statsBreakdownLoreTooltip(event, limboLuck)
"SkyBlock Menu" -> skyblockMenuTooltip(event, limboLuck)
}
}
private fun equipmentMenuTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
if (event.slot.slotIndex != 25) return
if (limboLuck == 0.0f && !showAllStats) return
val skillLuck = skillOverflowLuck.values.sum()
val totalLuck = skillLuck + limboLuck
val lastIndex = event.toolTip.indexOfLast { it == "§5§o" }
if (lastIndex == -1) return
val luckString = tryTruncateFloat(totalLuck)
event.toolTip.add(lastIndex, "$LUCK_TOOLTIP$luckString")
}
private fun statsBreakdownLoreTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
if (!inMiscStats) return
if (inCustomBreakdown && event.slot.slotIndex == 48) {
event.toolTip[1] = "§7To Your Stats Breakdown"
}
if (event.slot.slotIndex != 4) return
if (limboLuck == 0.0f && !showAllStats) return
val skillLuck = skillOverflowLuck.values.sum()
val totalLuck = skillLuck + limboLuck
val luckString = tryTruncateFloat(totalLuck)
event.toolTip.add("§5§o §a✴ SkyHanni User Luck §f$luckString")
}
private fun skyblockMenuTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
if (event.slot.slotIndex != 13) return
val lastIndex = event.toolTip.indexOfLast { it == "§5§o" }
if (lastIndex == -1) return
val skillLuck = skillOverflowLuck.values.sum()
val totalLuck = skillLuck + limboLuck
if (totalLuck == 0f) return
val luckString = tryTruncateFloat(totalLuck)
event.toolTip.add(lastIndex, "$LUCK_TOOLTIP$luckString")
}
private fun tryTruncateFloat(input: Float): String {
val string = input.addSeparators()
return if (string.endsWith(".0")) return string.dropLast(2)
else string
}
@SubscribeEvent
fun onStackClick(event: GuiContainerEvent.SlotClickEvent) {
if (!config.userluckEnabled) return
if (!inMiscStats) return
val limboUserLuck = storage?.limbo?.userLuck ?: 0.0f
if (limboUserLuck == 0.0f && !showAllStats) return
if (inCustomBreakdown && event.slotId != 49) event.cancel()
when (event.slotId) {
replaceSlot -> {
if (inCustomBreakdown) return
event.cancel()
inCustomBreakdown = true
}
48 -> {
if (!inCustomBreakdown) return
inCustomBreakdown = false
}
else -> return
}
}
private fun createItems() {
fillerItem = ItemUtils.createItemStack(
fillerID.getItemStack().item,
FILLER_NAME,
listOf(),
1,
15,
)
val limboLuck = storage?.limbo?.userLuck ?: 0.0f
val skillLuck = skillOverflowLuck.values.sum()
val totalLuck = skillLuck + limboLuck
mainLuckItem = ItemUtils.createItemStack(
mainLuckID.getItemStack().item,
"$MAIN_LUCK_NAME §f${tryTruncateFloat(totalLuck)}",
createItemLore("mainMenu", totalLuck),
)
limboItem = ItemUtils.createItemStack(
limboID.getItemStack().item,
LIMBO_NAME,
createItemLore("limbo", limboLuck),
)
skillsItem = ItemUtils.createItemStack(
skillsID.getItemStack().item,
SKILLS_NAME,
createItemLore("skills"),
)
}
private fun createItemLore(type: String, luckInput: Float = 0.0f): Array<String> {
calcSkillLuck()
return when (type) {
"mainMenu" -> {
val luckString = tryTruncateFloat(luckInput.roundTo(2))
if (luckInput == 0.0f) {
arrayOf(
"§7SkyHanni User Luck is the best stat.",
"",
"§7Flat: §a+$luckString✴",
"",
"§8You have none of this stat!",
"§eClick to view!",
)
} else {
arrayOf(
"§7SkyHanni User Luck increases your",
"§7overall fortune around Hypixel SkyBlock.",
"",
"§7(Disclaimer: May not affect real drop chances)",
"",
"§eClick to view!",
)
}
}
"limbo" -> {
val luckString = tryTruncateFloat(luckInput.roundTo(2))
arrayOf(
"§8Action",
"",
"§7Value: §a+$luckString✴",
"",
"§8Gain more by going to Limbo,",
"§8and obtaining a higher Personal Best§8.",
)
}
"skills" -> {
val luckString = skillOverflowLuck.values.sum()
val firstHalf = arrayOf(
"§8Grouped",
"",
"§7Value: §a+$luckString✴",
"",
)
val secondHalf = arrayOf(
"§8Stats from your overflow skills.",
"§8Obtain more each 5 overflow levels!",
)
val sourcesList = mutableListOf<String>()
for ((skillType, luck) in skillOverflowLuck) {
if (luck == 0) continue
sourcesList.add(" §a+$luck✴ §f${skillType.displayName} Skill")
}
val finalList = mutableListOf<String>()
finalList.addAll(firstHalf)
if (sourcesList.isNotEmpty()) {
finalList.addAll(sourcesList)
finalList.add("")
}
finalList.addAll(secondHalf)
finalList.toTypedArray()
}
else -> arrayOf("")
}
}
private fun calcSkillLuck() {
val storage = ProfileStorageData.profileSpecific?.skillData ?: return
skillOverflowLuck.clear()
for ((skillType, skillInfo) in storage) {
val level = skillInfo.level
val overflow = skillInfo.overflowLevel
val luck = ((overflow - level) / 5) * 50
skillOverflowLuck.addOrPut(skillType, luck)
}
}
}