blob: fb6453add8723c44554675ea112e0df5c535c243 (
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
|
package at.hannibal2.skyhanni.features.garden
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.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.TabListUpdateEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.regex.Pattern
class GardenCropMilestoneFix {
private val tabListPattern = " Milestone: §r§a(?<crop>.*) (?<tier>.*): §r§3(?<percentage>.*)%".toPattern()
private val levelUpPattern = Pattern.compile(" §r§b§lGARDEN MILESTONE §3(?<crop>.*) §8(?:.*)➜§3(?<tier>.*)")
private val tabListCropProgress = mutableMapOf<CropType, Long>()
@SubscribeEvent
fun onChatMessage(event: LorenzChatEvent) {
val matcher = levelUpPattern.matcher(event.message)
if (!matcher.matches()) return
val cropName = matcher.group("crop")
val crop = CropType.getByNameOrNull(cropName)
if (crop == null) {
LorenzUtils.debug("GardenCropMilestoneFix: crop is null: '$cropName'")
return
}
val tier = matcher.group("tier").romanToDecimalIfNeeded()
val crops = GardenCropMilestones.getCropsForTier(tier)
changedValue(crop, crops, "level up chat message")
}
@SubscribeEvent
fun onTabListUpdate(event: TabListUpdateEvent) {
for (line in event.tabList) {
val matcher = tabListPattern.matcher(line)
if (!matcher.matches()) continue
val tier = matcher.group("tier").toInt()
val percentage = matcher.group("percentage").toDouble()
val cropName = matcher.group("crop")
check(cropName, tier, percentage)
return
}
}
private fun check(cropName: String, tier: Int, percentage: Double) {
val baseCrops = GardenCropMilestones.getCropsForTier(tier)
val next = GardenCropMilestones.getCropsForTier(tier + 1)
val progressCrops = next - baseCrops
val progress = progressCrops * (percentage / 100)
val smallestPercentage = progressCrops * 0.0005
val crop = CropType.getByNameOrNull(cropName)
if (crop == null) {
LorenzUtils.debug("GardenCropMilestoneFix: crop is null: '$cropName'")
return
}
val tabListValue = baseCrops + progress - smallestPercentage
val newValue = tabListValue.toLong()
if (tabListCropProgress[crop] != newValue) {
if (tabListCropProgress.containsKey(crop)) {
changedValue(crop, newValue, "tab list")
}
}
tabListCropProgress[crop] = newValue
}
private val loadedCrops = mutableListOf<CropType>()
private fun changedValue(crop: CropType, tabListValue: Long, source: String) {
val calculated = crop.getCounter()
val diff = calculated - tabListValue
if (diff < -5_000) {
crop.setCounter(tabListValue)
if (!loadedCrops.contains(crop)) {
LorenzUtils.chat("§e[SkyHanni] Loaded ${crop.cropName} milestone data from $source!")
loadedCrops.add(crop)
}
}
if (diff > 5_000) {
LorenzUtils.debug("Fixed wrong ${crop.cropName} milestone data from $source: ${diff.addSeparators()}")
crop.setCounter(tabListValue)
}
}
}
|