blob: d004befd8b88b3e3dc67470d8852bdaccd7f9d4f (
plain)
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
|
package at.hannibal2.skyhanni.features.garden
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.GardenToolChangeEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.features.bazaar.BazaarApi
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.getSpeed
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import kotlinx.coroutines.launch
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.*
class CropMoneyDisplay {
private var display = mutableListOf<List<Any>>()
private val config get() = SkyHanniMod.feature.garden
private var tick = 0
private var loaded = false
private var ready = false
private val multipliers = mutableMapOf<String, Int>()
private val cropNames = mutableMapOf<String, CropType>() // internalName -> cropName
private var hasCropInHand = false
@SubscribeEvent
fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) {
if (!isEnabled()) return
config.moneyPerHourPos.renderStringsAndItems(display, posLabel = "Garden Crop Money Per Hour")
}
@SubscribeEvent
fun onGardenToolChange(event: GardenToolChangeEvent) {
hasCropInHand = event.crop != null
update()
}
@SubscribeEvent
fun onTick(event: TickEvent.ClientTickEvent) {
if (!isEnabled()) return
if (tick++ % (20 * 5) != 0) return
if (!hasCropInHand && !config.moneyPerHourAlwaysOn) return
update()
}
private fun update() {
init()
display = drawDisplay()
}
private fun drawDisplay(): MutableList<List<Any>> {
val newDisplay = mutableListOf<List<Any>>()
val title = if (config.moneyPerHourCompact) {
Collections.singletonList("§7Money/hour:")
} else {
Collections.singletonList("§7Money per hour when selling:")
}
if (!ready) {
newDisplay.add(title)
newDisplay.add(Collections.singletonList("§eLoading..."))
return newDisplay
}
if (!hasCropInHand && !config.moneyPerHourAlwaysOn) return newDisplay
newDisplay.add(title)
val moneyPerHourData = calculateMoneyPerHour()
if (moneyPerHourData.isEmpty()) {
if (!GardenAPI.isSpeedDataEmpty()) {
val message = "money/hr empty but speed data not empty, retry"
LorenzUtils.debug(message)
println(message)
newDisplay.add(Collections.singletonList("§eStill Loading..."))
ready = false
loaded = false
return newDisplay
}
newDisplay.add(Collections.singletonList("§cFarm crops to add them to this list!"))
return newDisplay
}
var number = 0
for ((internalName, moneyPerHour) in moneyPerHourData.sortedDesc()) {
number++
val cropName = cropNames[internalName]!!
val isCurrent = cropName == GardenAPI.cropInHand
if (number > config.moneyPerHourShowOnlyBest && !isCurrent) continue
val list = mutableListOf<Any>()
if (!config.moneyPerHourCompact) {
list.add("§7$number# ")
}
try {
list.add(NEUItems.getItemStack(internalName))
} catch (e: NullPointerException) {
e.printStackTrace()
}
if (!config.moneyPerHourCompact) {
val itemName = NEUItems.getItemStack(internalName).name?.removeColor() ?: continue
val currentColor = if (isCurrent) "§e" else "§7"
val contestFormat = if (GardenNextJacobContest.isNextCrop(cropName)) "§n" else ""
list.add("$currentColor$contestFormat$itemName§7: ")
}
val format = if (config.moneyPerHourCompactPrice) {
NumberUtil.format(moneyPerHour)
} else {
LorenzUtils.formatInteger(moneyPerHour.toLong())
}
list.add("§6$format")
newDisplay.add(list)
}
return newDisplay
}
private fun calculateMoneyPerHour(): Map<String, Double> {
val moneyPerHours = mutableMapOf<String, Double>()
for ((internalName, amount) in multipliers) {
val price = NEUItems.getPrice(internalName)
val crop = cropNames[internalName]!!
val speed = crop.getSpeed()
// No speed data for item in hand
if (speed == -1) continue
// Price not found
if (price == -1.0) continue
val speedPerHr = speed.toDouble() * 60 * 60
val blocksPerHour = speedPerHr / amount.toDouble()
val moneyPerHour = price * blocksPerHour
moneyPerHours[internalName] = moneyPerHour
}
return moneyPerHours
}
private fun init() {
if (loaded) return
if (BazaarApi.bazaarMap.isEmpty()) {
LorenzUtils.debug("bz not ready for money/time!")
return
}
loaded = true
SkyHanniMod.coroutineScope.launch {
for ((internalName, _) in NotEnoughUpdates.INSTANCE.manager.itemInformation) {
if (!BazaarApi.isBazaarItem(internalName)) continue
if (internalName == "ENCHANTED_PAPER") continue
val (newId, amount) = NEUItems.getMultiplier(internalName)
if (amount < 10) continue
val itemName = NEUItems.getItemStack(newId).name?.removeColor() ?: continue
val crop = GardenAPI.itemNameToCropName(itemName)
crop?.let {
multipliers[internalName] = amount
cropNames[internalName] = it
}
}
ready = true
update()
}
}
private fun isEnabled() = GardenAPI.inGarden() && config.moneyPerHourDisplay
}
|