aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/GardenCropUpgrades.kt
blob: 07b796b334a31e93637f89b88a3949db8f9c1fd2 (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
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.CropUpgradeUpdateEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class GardenCropUpgrades {
    private val tierPattern = "§7Current Tier: §[0-9a-e](\\d)§7/§a9".toRegex()
    private val chatUpgradePattern = " {2}§r§6§lCROP UPGRADE §e§f([\\w ]+)§7 #(\\d)".toRegex()

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        chatUpgradePattern.matchEntire(event.message)?.groups?.let { matches ->
            val crop = CropType.getByItemName(matches[1]!!.value) ?: return
            val level = matches[2]!!.value.toInt()
            crop.setUpgradeLevel(level)
        }
        CropUpgradeUpdateEvent().postAndCatch()
    }

    @SubscribeEvent
    fun onInventoryOpen(event: InventoryOpenEvent) {
        if (event.inventoryName != "Crop Upgrades") return
        event.inventoryItems.forEach { (_, item) ->
            val crop = item.name?.removeColor()?.let {
                CropType.getByNameOrNull(it)
            } ?: return@forEach
            val level = item.getLore().firstNotNullOfOrNull {
                tierPattern.matchEntire(it)?.groups?.get(1)?.value?.toIntOrNull()
            } ?: return@forEach
            crop.setUpgradeLevel(level)
        }
        CropUpgradeUpdateEvent().postAndCatch()
    }

    companion object {
        private val cropUpgrades: MutableMap<CropType, Int> get() =
            SkyHanniMod.feature.hidden.gardenCropUpgrades

        fun CropType.getUpgradeLevel() = cropUpgrades[this]

        fun CropType.setUpgradeLevel(level: Int) {
            cropUpgrades[this] = level
        }

    }
}