aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneDisplay.kt
blob: 18b4086663e02bb2f9f760c606bd1b887dd3a25b (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
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
376
package at.hannibal2.skyhanni.features.garden

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ClickType
import at.hannibal2.skyhanni.data.GardenCropMilestones
import at.hannibal2.skyhanni.data.GardenCropMilestones.Companion.getCounter
import at.hannibal2.skyhanni.data.GardenCropMilestones.Companion.setCounter
import at.hannibal2.skyhanni.data.SendTitleHelper
import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.features.garden.CropType.Companion.getCropType
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.addCropIcon
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.getCropType
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.setSpeed
import at.hannibal2.skyhanni.utils.BlockUtils.isBabyCrop
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.SoundUtils.playSound
import at.hannibal2.skyhanni.utils.TimeUtils
import net.minecraft.client.audio.ISound
import net.minecraft.client.audio.PositionedSound
import net.minecraft.item.ItemStack
import net.minecraft.util.ResourceLocation
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.*
import kotlin.concurrent.fixedRateTimer

class GardenCropMilestoneDisplay {
    private var progressDisplay = listOf<List<Any>>()
    private var mushroomCowPerkDisplay = listOf<List<Any>>()
    private val cultivatingData = mutableMapOf<CropType, Int>()
    private val config get() = SkyHanniMod.feature.garden
    private val bestCropTime = GardenBestCropTime()
//    val cropMilestoneLevelUpPattern = Pattern.compile("  §r§b§lGARDEN MILESTONE §3(.*) §8XXIII➜§3(.*)")

    private val sound = object : PositionedSound(ResourceLocation("random.orb")) {
        init {
            volume = 50f
            repeat = false
            repeatDelay = 0
            attenuationType = ISound.AttenuationType.NONE
        }
    }

    private var lastPlaySoundTime = 0L

    private var needsInventory = false

    private var mushroom_cow_nether_warts = true

    @SubscribeEvent
    fun onRepoReload(event: RepositoryReloadEvent) {
        try {
            val constant = event.getConstant("DisabledFeatures")
            mushroom_cow_nether_warts = if (constant != null) {
                if (constant.has("mushroom_cow_nether_warts")) {
                    constant["mushroom_cow_nether_warts"].asBoolean
                } else false
            } else false
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SubscribeEvent
    fun onChatMessage(event: LorenzChatEvent) {
        if (!isEnabled()) return
        // TODO remove this once hypixel counts 64x pumpkin drops to cultivating
        if (event.message == "§a§lUNCOMMON DROP! §r§eDicer dropped §r§f64x §r§fPumpkin§r§e!") {
            CropType.PUMPKIN.setCounter(CropType.PUMPKIN.getCounter() + 64)
        }
//        if (config.cropMilestoneWarnClose) {
//            val matcher = cropMilestoneLevelUpPattern.matcher(event.message)
//            if (matcher.matches()) {
//                val cropType = matcher.group(1)
//                val newLevel = matcher.group(2).romanToDecimalIfNeeded()
//                LorenzUtils.debug("found milestone messsage!")
//                SendTitleHelper.sendTitle("§b$cropType $newLevel", 1_500)
//            }
//        }
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) {
        if (!isEnabled()) return

        config.cropMilestoneProgressDisplayPos.renderStringsAndItems(
            progressDisplay,
            posLabel = "Crop Milestone Progress"
        )

        if (config.cropMilestoneMushroomPetPerkEnabled) {
            config.cropMilestoneMushroomPetPerkPos.renderStringsAndItems(
                mushroomCowPerkDisplay,
                posLabel = "Mushroom Cow Perk"
            )
        }

        if (config.cropMilestoneBestDisplay) {
            config.cropMilestoneNextDisplayPos.renderStringsAndItems(bestCropTime.display, posLabel = "Best Crop Time")
        }
    }

    @SubscribeEvent(priority = EventPriority.LOW)
    fun onProfileJoin(event: ProfileJoinEvent) {
        if (GardenCropMilestones.cropCounter.values.sum() == 0L) {
            needsInventory = true
        }
    }

    @SubscribeEvent
    fun onCropMilestoneUpdate(event: CropMilestoneUpdateEvent) {
        needsInventory = false
        bestCropTime.updateTimeTillNextCrop()
        update()
    }

    @SubscribeEvent
    fun onOwnInventoryItemUpdate(event: OwnInventorItemUpdateEvent) {
        if (!GardenAPI.inGarden()) return

        try {
            val item = event.itemStack
            val counter = GardenAPI.readCounter(item)
            if (counter == -1) return
            val crop = item.getCropType() ?: return
            if (cultivatingData.containsKey(crop)) {
                val old = cultivatingData[crop]!!
                val addedCounter = counter - old

                if (GardenCropMilestones.cropCounter.isEmpty()) {
                    for (innerCrop in CropType.values()) {
                        innerCrop.setCounter(0)
                    }
                }
                if (GardenAPI.isSpeedDataEmpty()) {
                    for (cropType in CropType.values()) {
                        cropType.setSpeed(-1)
                    }
                }

                crop.setCounter(crop.getCounter() + addedCounter)
                EliteFarmingWeight.addCrop(crop, addedCounter)
                if (currentCrop == crop) {
                    calculateSpeed(addedCounter)
                    update()
                }
            }
            cultivatingData[crop] = counter
        } catch (e: Throwable) {
            LorenzUtils.error("[SkyHanni] Error in OwnInventorItemUpdateEvent")
            e.printStackTrace()
        }
    }

    @SubscribeEvent
    fun onBlockClick(event: BlockClickEvent) {
        if (!isEnabled()) return
        if (event.clickType != ClickType.LEFT_CLICK) return

        val blockState = event.getBlockState

        val cropType = blockState.getCropType() ?: return
        val multiplier = cropType.multiplier
        if (multiplier == 1) {
            if (blockState.isBabyCrop()) return
        }
        blocksBroken += multiplier
    }

    private var currentSpeed = 0
    private var averageSpeedPerSecond = 0
    private var countInLastSecond = 0
    private val allCounters = mutableListOf<Int>()
    private var lastItemInHand: ItemStack? = null
    private var currentCrop: CropType? = null
    private var blocksBroken = 0
    private var lastBlocksPerSecond = 0

    private fun resetSpeed() {
        currentSpeed = 0
        averageSpeedPerSecond = 0
        countInLastSecond = 0
        allCounters.clear()
    }

    init {
        fixedRateTimer(name = "skyhanni-crop-milestone-speed", period = 1000L) {
            if (GardenAPI.inGarden() && GardenAPI.mushroomCowPet) {
                CropType.MUSHROOM.setCounter(CropType.MUSHROOM.getCounter() + blocksBroken)
                update()
            }
            if (isEnabled()) {
                checkSpeed()
            }
        }
    }

    private fun checkSpeed() {
        if (countInLastSecond > 8) {
            allCounters.add(currentSpeed)
            while (allCounters.size > 30) {
                allCounters.removeFirst()
            }
            averageSpeedPerSecond = allCounters.average().toInt()
        }
        countInLastSecond = 0
        currentSpeed = 0

        lastBlocksPerSecond = blocksBroken
        blocksBroken = 0
    }


    private fun calculateSpeed(addedCounter: Int) {
        currentSpeed += addedCounter
        countInLastSecond++
    }

    @SubscribeEvent
    fun onGardenToolChange(event: GardenToolChangeEvent) {
        lastItemInHand = event.toolItem
        currentCrop = event.crop

        if (isEnabled()) {
            resetSpeed()
            update()
        }
    }

    private fun update() {
        progressDisplay = emptyList()
        mushroomCowPerkDisplay = emptyList()
        bestCropTime.display = emptyList()
        currentCrop?.let {
            progressDisplay = drawProgressDisplay(it, it.getCounter())
            if (config.cropMilestoneBestDisplay) {
                bestCropTime.display = bestCropTime.drawBestDisplay(it)