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
|
package at.hannibal2.skyhanni.features.mining
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.HotmData
import at.hannibal2.skyhanni.data.HotmReward
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.MiningAPI
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.mining.OreMinedEvent
import at.hannibal2.skyhanni.features.mining.MineshaftPityDisplay.PityBlock.Companion.getPity
import at.hannibal2.skyhanni.features.mining.MineshaftPityDisplay.PityBlock.Companion.getPityBlock
import at.hannibal2.skyhanni.features.mining.OreType.Companion.getOreType
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.roundTo
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.chat.Text
import at.hannibal2.skyhanni.utils.chat.Text.hover
import at.hannibal2.skyhanni.utils.renderables.Renderable
import com.google.gson.annotations.Expose
import net.minecraft.block.BlockStone
import net.minecraft.init.Blocks
import net.minecraft.item.EnumDyeColor
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@SkyHanniModule
object MineshaftPityDisplay {
private val config get() = SkyHanniMod.feature.mining.mineshaftPityDisplay
private val profileStorage get() = ProfileStorageData.profileSpecific?.mining?.mineshaft
private var minedBlocks: MutableList<PityData>
get() = profileStorage?.blocksBroken ?: mutableListOf()
set(value) {
profileStorage?.blocksBroken = value
}
private var PityBlock.spreadBlocksBroken: Int
get() = minedBlocks.firstOrNull { it.pityBlock == this }?.spreadBlocksBroken ?: 0
set(value) {
minedBlocks.firstOrNull { it.pityBlock == this }?.let { it.spreadBlocksBroken = value } ?: run {
minedBlocks.add(PityData(this, spreadBlocksBroken = value))
}
}
private var PityBlock.blocksBroken: Int
get() = minedBlocks.firstOrNull { it.pityBlock == this }?.blocksBroken ?: 0
set(value) {
minedBlocks.firstOrNull { it.pityBlock == this }?.let { it.blocksBroken = value } ?: run {
minedBlocks.add(PityData(this, blocksBroken = value))
}
}
private var mineshaftTotalBlocks: Long
get() = profileStorage?.mineshaftTotalBlocks ?: 0L
set(value) {
profileStorage?.mineshaftTotalBlocks = value
}
private var mineshaftTotalCount: Int
get() = profileStorage?.mineshaftTotalCount ?: 0
set(value) {
profileStorage?.mineshaftTotalCount = value
}
private var sessionMineshafts = 0
var lastMineshaftSpawn = SimpleTimeMark.farPast()
private var display = listOf<Renderable>()
private const val MAX_COUNTER = 2000
@HandleEvent(onlyOnSkyblock = true)
fun onOreMined(event: OreMinedEvent) {
if (!MiningAPI.inGlacialTunnels()) return
val originalOre = event.originalOre
originalOre?.getPityBlock()?.let { it.blocksBroken++ }
event.extraBlocks.toMutableMap()
.apply {
if (originalOre != null) addOrPut(originalOre, -1)
}
.map { (block, amount) ->
block.getPityBlock()?.let { it.spreadBlocksBroken += amount }
}
update()
}
@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
if (!MiningAPI.inGlacialTunnels()) return
if (MiningNotifications.mineshaftSpawn.matches(event.message)) {
val pityCounter = calculateCounter()
val chance = calculateChance(pityCounter)
val counterUntilPity = MAX_COUNTER - pityCounter
val totalBlocks = PityBlock.entries.sumOf { it.blocksBroken + it.spreadBlocksBroken }
mineshaftTotalBlocks += totalBlocks
mineshaftTotalCount++
sessionMineshafts++
val message = event.message + " §e($counterUntilPity)"
val hoverText = buildList {
add("§7Blocks mined: §e$totalBlocks")
add("§7Pity Counter: §e$pityCounter")
add(
"§7Chance: " +
"§e1§6/§e${chance.roundTo(1)} " +
"§7(§b${((1.0 / chance) * 100).addSeparators()}%§7)",
)
minedBlocks.forEach {
add(
" §7${it.pityBlock.displayName} mined: " +
"§e${it.blocksBroken.addSeparators()} [+${it.spreadBlocksBroken.addSeparators()} spread]" +
" §6(${it.pityBlock.getPity().addSeparators()}/${counterUntilPity.addSeparators()})",
)
}
add("")
add(
"§7Average Blocks/Mineshaft: " +
"§e${(mineshaftTotalBlocks / mineshaftTotalCount.toDouble()).addSeparators()}",
)
if (!lastMineshaftSpawn.isFarPast()) {
add("")
add("§7Time since Last Mineshaft: §b${lastMineshaftSpawn.passedSince().format()}")
}
}
resetCounter()
val newComponent = Text.text(message) {
hover = Text.multiline(hoverText)
}
if (config.modifyChatMessage) event.chatComponent = newComponent
}
}
@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
if (!isDisplayEnabled()) return
update()
}
private fun calculateCounter(): Int {
val counter = MAX_COUNTER
if (minedBlocks.isEmpty()) return counter
val difference = minedBlocks.sumOf { it.pityBlock.getPity() }
return (counter - difference).toInt().coerceAtLeast(0)
}
// if the chance is 1/1500, it will return 1500
private fun calculateChance(counter: Int): Double {
val surveyorPercent = HotmData.SURVEYOR.getReward()[HotmReward.MINESHAFT_CHANCE] ?: 0.0
val peakMountainPercent = HotmData.CORE_OF_THE_MOUNTAIN.getReward()[HotmReward.MINESHAFT_CHANCE] ?: 0.0
val chance = counter / (1 + surveyorPercent / 100 + peakMountainPercent / 100)
return chance
}
// TODO extract into multiple smaller functions
private fun update() {
val pityCounter = calculateCounter()
val chance = calculateChance(pityCounter)
val counterUntilPity = MAX_COUNTER - pityCounter
val blocksToPityList = buildList {
val multipliers = PityBlock.entries.map { it.multiplier }.toSet().sorted()
multipliers.forEach { multiplier ->
val iconsList = PityBlock.entries
.filter { it.multiplier == multiplier }
.map { Renderable.itemStack(it.displayItem) }
add(
Renderable.horizontalContainer(
listOf(
Renderable.horizontalContainer(iconsList),
Renderable.string("§b${pityCounter / multiplier}"),
),
2,
),
)
}
}
val neededToPityRenderable = Renderable.verticalContainer(
listOf(
Renderable.string("§3Needed to pity:"),
Renderable.horizontalContainer(
listOf(
Renderable.placeholder(10, 0),
Renderable.verticalContainer(blocksToPityList),
),
),
),
)
val map = mapOf(
MineshaftPityLine.TITLE to Renderable.string("§9§lMineshaft Pity Counter"),
MineshaftPityLine.
|