-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
JacobContestTimeNeeded.kt
206 lines (186 loc) · 7.93 KB
/
JacobContestTimeNeeded.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
package at.hannibal2.skyhanni.features.garden.contest
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryUpdatedEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.FarmingFortuneDisplay.getLatestTrueFarmingFortune
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed.getLatestBlocksPerSecond
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.buildSelector
import at.hannibal2.skyhanni.utils.CollectionUtils.sorted
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.roundTo
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderable
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.renderables.Container
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object JacobContestTimeNeeded {
private val config get() = GardenAPI.config
private var display: Renderable? = null
private var currentBracket = ContestBracket.GOLD
@SubscribeEvent(priority = EventPriority.LOW)
fun onInventoryUpdated(event: InventoryUpdatedEvent) {
if (FarmingContestAPI.inInventory) {
update()
}
}
private fun update() {
val sorted = mutableMapOf<CropType, Duration>()
val map = mutableMapOf<CropType, Renderable>()
for (crop in CropType.entries) {
testCrop(crop, sorted, map)
}
this.display = Container.vertical {
string("§e§lTime Needed for ${currentBracket.displayName} §eMedal!")
selector<ContestBracket>(
"§7Bracket: ",
getName = { type -> type.name.lowercase() },
isCurrent = { it == currentBracket },
onChange = {
currentBracket = it
update()
}
)
spacer()
for (crop in sorted.sorted().keys) {
val text = map[crop]!!
horizontal {
item(crop.icon)
renderable(text)
}
}
}
}
private fun testCrop(
crop: CropType,
sorted: MutableMap<CropType, Duration>,
map: MutableMap<CropType, Renderable>,
) {
val bps = crop.getBps()
if (bps == null) {
sorted[crop] = Duration.INFINITE
map[crop] = Renderable.hoverTips(
"§9${crop.cropName} §cNo speed data!",
listOf("§cFarm ${crop.cropName} to show data!")
)
return
}
val ff = crop.getLatestTrueFarmingFortune()
if (ff == null) {
sorted[crop] = Duration.INFINITE
map[crop] = Renderable.hoverTips(
"§9${crop.cropName} §cNo Farming Fortune data!",
listOf("§cHold a ${crop.cropName} specific", "§cfarming tool in hand to show data!")
)
return
}
val averages = FarmingContestAPI.calculateAverages(crop).second
if (averages.isEmpty()) {
sorted[crop] = Duration.INFINITE - 2.milliseconds
map[crop] = Renderable.hoverTips(
"§9${crop.cropName} §cNo contest data!",
listOf(
"§cOpen more pages or participate",
"§cin a ${crop.cropName} Contest to show data!"
)
)
return
}
val speed = (ff * crop.baseDrops * bps / 100).roundTo(1).toInt()
renderCrop(speed, crop, averages, sorted, map)
}
private fun renderCrop(
speed: Int,
crop: CropType,
averages: Map<ContestBracket, Int>,
sorted: MutableMap<CropType, Duration>,
map: MutableMap<CropType, Renderable>,
) {
var lowBPSWarning = listOf<String>()
val rawSpeed = speed.toDouble()
val speedForFormula = crop.getBps()?.let {
if (it < 15) {
val v = rawSpeed / it
(v * 19.9).toInt()
} else speed
} ?: speed
var showLine = ""
val brackets = mutableListOf<String>()
for (bracket in ContestBracket.entries) {
val amount = averages[bracket]
if (amount == null) {
sorted[crop] = Duration.INFINITE - 1.milliseconds
brackets.add("${bracket.displayName} §cBracket not revealed!")
showLine = "§9${crop.cropName} §cBracket not revealed!"
continue
}
val timeInMinutes = (amount.toDouble() / speedForFormula).seconds
val formatDuration = timeInMinutes.format()
val color = if (timeInMinutes < 20.minutes) "§b" else "§c"
var marking = ""
var bracketText = "${bracket.displayName} $color$formatDuration"
var blocksPerSecond = crop.getBps()
if (blocksPerSecond == null) {
marking += "§0§l !" // hoping this never shows
blocksPerSecond = 19.9
lowBPSWarning = listOf("§cYour Blocks/second is too low,", "§cshowing 19.9 Blocks/second instead!")
} else {
if (blocksPerSecond < 15.0) {
marking += "§4§l !"
blocksPerSecond = 19.9
lowBPSWarning =
listOf("§cYour Blocks/second is too low,", "§cshowing 19.9 Blocks/second instead!")
} else {
marking += " "
}
}
val line = if (timeInMinutes < 20.minutes) {
"§9${crop.cropName} §7in §b$formatDuration" + marking
} else {
val cropFF = crop.getLatestTrueFarmingFortune() ?: 0.0
val cropsPerSecond = amount.toDouble() / blocksPerSecond / 60
val ffNeeded = cropsPerSecond * 100 / 20 / crop.baseDrops
val missing = (ffNeeded - cropFF).toInt()
bracketText += " §7(Need ${missing.addSeparators()} FF more)"
"§9${crop.cropName} §cNo ${currentBracket.displayName} §cmedal possible!" + marking
}
brackets.add(bracketText)
if (bracket == currentBracket) {
sorted[crop] = timeInMinutes
showLine = line
}
}
map[crop] = Renderable.hoverTips(
showLine,
buildList {
add("§7Time Needed for §9${crop.cropName} Medals§7:")
addAll(brackets)
add("")
val latestFF = crop.getLatestTrueFarmingFortune() ?: 0.0
add("§7Latest FF: §e${(latestFF).addSeparators()}")
val bps = crop.getBps()?.roundTo(1) ?: 0
add("§7${addBpsTitle()}§e${bps.addSeparators()}")
addAll(lowBPSWarning)
}
)
}
private fun addBpsTitle() = if (config.jacobContestCustomBps) "Custom Blocks/Second: " else "Your Blocks/Second: "
private fun CropType.getBps() = if (config.jacobContestCustomBps) {
config.jacobContestCustomBpsValue
} else getLatestBlocksPerSecond()
@SubscribeEvent
fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
if (!isEnabled()) return
if (!FarmingContestAPI.inInventory) return
config.jacobContestTimesPosition.renderRenderable(display, posLabel = "Jacob Contest Time Needed")
}
fun isEnabled() = LorenzUtils.inSkyBlock && config.jacobContestTimes
}