From 50dfb62bad66e9bdb04cca17a2dd32a69b108265 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Wed, 19 Apr 2023 23:44:52 +0200 Subject: Reading tab list to load or fix crop milestone data --- .../java/at/hannibal2/skyhanni/SkyHanniMod.java | 1 + .../features/garden/GardenCropMilestoneFix.kt | 68 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java index 76633751e..056a6f9e7 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java @@ -271,6 +271,7 @@ public class SkyHanniMod { loadModule(new AshfangMinisNametagHider()); loadModule(new GardenTeleportPadInventoryNumber()); loadModule(new ComposterOverlay()); + loadModule(new GardenCropMilestoneFix()); Commands.INSTANCE.init(); diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt new file mode 100644 index 000000000..33a605379 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt @@ -0,0 +1,68 @@ +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.TabListUpdateEvent +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class GardenCropMilestoneFix { + private val pattern = " Milestone: §r§a(?.*) (?.*): §r§3(?.*)%".toPattern() + + private val tabListCropProgress = mutableMapOf() + + @SubscribeEvent + fun onTabListUpdate(event: TabListUpdateEvent) { + for (line in event.tabList) { + val matcher = pattern.matcher(line) + if (!matcher.matches()) continue + + val tier = matcher.group("tier").toInt() + val percentage = matcher.group("percentage").toDouble() + + check(matcher.group("crop"), 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.getByName(cropName) + if (crop == null) { + LorenzUtils.debug("GardenCropMilestoneFix: crop is null: '$cropName'") + return + } + + val tabListValue = baseCrops + progress - smallestPercentage + + val long = tabListValue.toLong() + + if (tabListCropProgress[crop] == long) return + if (tabListCropProgress.containsKey(crop)) { + changedValue(crop, long) + } + + tabListCropProgress[crop] = long + } + + private fun changedValue(crop: CropType, tabListValue: Long) { + val calculated = crop.getCounter() + val diff = calculated - tabListValue + if (diff < -5_000) { + crop.setCounter(tabListValue) + LorenzUtils.chat("§e[SkyHanni] Loaded ${crop.cropName} milestone data from tab list!") + } + if (diff > 5_000) { + LorenzUtils.debug("Fixed wrong ${crop.cropName} milestone data from tab list: ${diff.addSeparators()}") + crop.setCounter(tabListValue) + } + } +} \ No newline at end of file -- cgit