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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.data.jsonobjects.repo.GardenJson
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.CollectionUtils.editCopy
import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import kotlinx.coroutines.launch
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object GardenCropMilestonesCommunityFix {
    private val amountPattern by RepoPattern.pattern(
        "data.garden.milestonefix.amount",
        ".*§e(?<having>.*)§6/§e(?<max>.*)"
    )

    private var showWrongData = false
    private var showWhenAllCorrect = false

    @SubscribeEvent
    fun onRepoReload(event: RepositoryReloadEvent) {
        val data = event.getConstant<GardenJson>("Garden")
        val map = data.crop_milestone_community_help ?: return
        for ((key, value) in map) {
            if (key == "show_wrong_data") {
                showWrongData = value
            }
            if (key == "show_when_all_correct") {
                showWhenAllCorrect = value
            }
        }
    }

    fun openInventory(inventoryItems: Map<Int, ItemStack>) {
        if (!showWrongData) return
        if (!GardenAPI.config.copyMilestoneData) return
        fixForWrongData(inventoryItems)
    }

    private fun fixForWrongData(inventoryItems: Map<Int, ItemStack>) {
        val data = mutableListOf<String>()
        for ((_, stack) in inventoryItems) {
            val crop = GardenCropMilestones.getCropTypeByLore(stack) ?: continue
            checkForWrongData(stack, crop, data)
        }

        if (data.isNotEmpty()) {
            ChatUtils.chat(
                "Found §c${data.size} §ewrong crop milestone steps in the menu! " +
                    "Correct data got put into clipboard. " +
                    "Please share it on the §bSkyHanni Discord §ein the channel §b#share-data§e."
            )
            OSUtils.copyToClipboard("```${data.joinToString("\n")}```")
        } else {
            if (showWhenAllCorrect) {
                ChatUtils.chat("No wrong crop milestone steps found!")
            }
        }
    }

    private fun checkForWrongData(
        stack: ItemStack,
        crop: CropType,
        wrongData: MutableList<String>,
    ) {
        val rawNumber = stack.name.removeColor().replace(crop.cropName, "").trim()
        val realTier = if (rawNumber == "") 0 else rawNumber.romanToDecimalIfNecessary()

        val lore = stack.getLore()
        val next = lore.nextAfter({ GardenCropMilestones.totalPattern.matches(it) }, 3) ?: return
        val total = lore.nextAfter({ GardenCropMilestones.totalPattern.matches(it) }, 6) ?: return

//         debug(" ")
//         debug("crop: $crop")
//         debug("realTier: $realTier")

        val guessNextMax = GardenCropMilestones.getCropsForTier(realTier + 1, crop) - GardenCropMilestones.getCropsForTier(realTier, crop)
//         debug("guessNextMax: ${guessNextMax.addSeparators()}")
        val nextMax = amountPattern.matchMatcher(next) {
            group("max").formatLong()
        } ?: return
//         debug("nextMax real: ${nextMax.addSeparators()}")
        if (nextMax != guessNextMax) {
//             debug("wrong, add to list")
            wrongData.add("$crop:$realTier:${nextMax.addSeparators()}")
        }

        val guessTotalMax = GardenCropMilestones.getCropsForTier(46, crop) // no need to overflow here
//         println("guessTotalMax: ${guessTotalMax.addSeparators()}")
        val totalMax = amountPattern.matchMatcher(total) {
            group("max").formatLong()
        } ?: return
//         println("totalMax real: ${totalMax.addSeparators()}")
        val totalOffBy = guessTotalMax - totalMax
//         debug("$crop total offf by: ${totalOffBy.addSeparators()}")
    }

//     fun debug(message: String) {
//         if (SkyHanniMod.feature.dev.debug.enabled) {
//             println(message)
//         }
//     }

    /**
     * This helps to fix wrong crop milestone data
     * This command reads the clipboard content,
     * in the format of users sending crop milestone step data.
     *
     * The new data will be compared to the currently saved data,
     * differences are getting replaced, and the result gets put into the clipboard.
     * The clipboard context can be used to update the repo content.
     */
    fun readDataFromClipboard() {
        SkyHanniMod.coroutineScope.launch {
            OSUtils.readFromClipboard()?.let {
                handleInput(it)
            }
        }
    }

    private var totalFixedValues = 0

    private fun handleInput(input: String) {
        println(" ")
        var fixed = 0
        var alreadyCorrect = 0
        for (line in input.lines()) {
            val split = line.replace("```", "").replace(".", ",").split(":")
            if (split.size != 3) continue
            val (rawCrop, tier, amount) = split
            val crop = LorenzUtils.enumValueOf<CropType>(rawCrop)

            if (tryFix(crop, tier.toInt(), amount.formatInt())) {
                fixed++
            } else {
                alreadyCorrect++
            }
        }
        totalFixedValues += fixed
        ChatUtils.chat("Fixed: $fixed/$alreadyCorrect, total fixes: $totalFixedValues")
        val s = ConfigManager.gson.toJsonTree(GardenCropMilestones.cropMilestoneData).toString()
        OSUtils.copyToClipboard("\"crop_milestones\":$s,")
    }

    private fun tryFix(crop: CropType, tier: Int, amount: Int): Boolean {
        val guessNextMax = GardenCropMilestones.getCropsForTier(tier + 1, crop) - GardenCropMilestones.getCropsForTier(tier, crop)
        if (guessNextMax.toInt() == amount) return false
        GardenCropMilestones.cropMilestoneData = GardenCropMilestones.cropMilestoneData.editCopy {
            fix(crop, this, tier, amount)
        }
        return true
    }

    private fun fix(crop: CropType, map: MutableMap<CropType, List<Int>>, tier: Int, amount: Int) {
        map[crop] = map[crop]!!.editCopy {
            this[tier] = amount
        }
    }
}