From 46399eb4ef9212935853bdd1c0af191cb6b76503 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:48:55 +0100 Subject: Removed message when crop milestones look different in the menu than stored SkyHanni data. --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 2 + .../hannibal2/skyhanni/config/commands/Commands.kt | 4 +- .../skyhanni/data/GardenCropMilestones.kt | 2 +- .../data/GardenCropMilestonesCommunityFix.kt | 179 +++++++++++++++++++++ .../skyhanni/data/GardenCropMilestonesFix.kt | 159 ------------------ .../skyhanni/utils/jsonobjects/GardenJson.java | 3 + 6 files changed, 187 insertions(+), 162 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesCommunityFix.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesFix.kt (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index f2d250226..a7c3584b6 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -16,6 +16,7 @@ import at.hannibal2.skyhanni.data.EntityMovementData import at.hannibal2.skyhanni.data.FriendAPI import at.hannibal2.skyhanni.data.GardenComposterUpgradesData import at.hannibal2.skyhanni.data.GardenCropMilestones +import at.hannibal2.skyhanni.data.GardenCropMilestonesCommunityFix import at.hannibal2.skyhanni.data.GardenCropUpgrades import at.hannibal2.skyhanni.data.GuiEditManager import at.hannibal2.skyhanni.data.GuildAPI @@ -362,6 +363,7 @@ class SkyHanniMod { loadModule(TabListData()) loadModule(RenderData()) loadModule(GardenCropMilestones) + loadModule(GardenCropMilestonesCommunityFix) loadModule(GardenCropUpgrades()) loadModule(VisitorListener()) loadModule(OwnInventoryData()) diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index 641dc912d..8e4791901 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -4,7 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigFileType import at.hannibal2.skyhanni.config.ConfigGuiManager import at.hannibal2.skyhanni.data.ChatManager -import at.hannibal2.skyhanni.data.GardenCropMilestonesFix +import at.hannibal2.skyhanni.data.GardenCropMilestonesCommunityFix import at.hannibal2.skyhanni.data.GuiEditManager import at.hannibal2.skyhanni.data.PartyAPI import at.hannibal2.skyhanni.features.bingo.BingoCardDisplay @@ -318,7 +318,7 @@ object Commands { registerCommand( "readcropmilestonefromclipboard", "Read crop milestone from clipboard. This helps fixing wrong crop milestone data" - ) { GardenCropMilestonesFix.readDataFromClipboard() } + ) { GardenCropMilestonesCommunityFix.readDataFromClipboard() } } private fun internalCommands() { diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt index d810121ed..610393906 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt @@ -41,7 +41,7 @@ object GardenCropMilestones { } } CropMilestoneUpdateEvent().postAndCatch() - GardenCropMilestonesFix.openInventory(event.inventoryItems) + GardenCropMilestonesCommunityFix.openInventory(event.inventoryItems) } var cropMilestoneData: Map> = emptyMap() diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesCommunityFix.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesCommunityFix.kt new file mode 100644 index 000000000..c97997262 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesCommunityFix.kt @@ -0,0 +1,179 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.events.RepositoryReloadEvent +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.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy +import at.hannibal2.skyhanni.utils.LorenzUtils.nextAfter +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber +import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded +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.jsonobjects.GardenJson +import kotlinx.coroutines.launch +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object GardenCropMilestonesCommunityFix { + private val pattern = ".*§e(?.*)§6/§e(?.*)".toPattern() + private var showWrongData = false + private var showWhenAllCorrect = false + + @SubscribeEvent + fun onRepoReload(event: RepositoryReloadEvent) { + val data = event.getConstant("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) { + if (!showWrongData) return + if (!SkyHanniMod.feature.garden.copyMilestoneData) return + fixForWrongData(inventoryItems) + } + + private fun fixForWrongData(inventoryItems: Map) { + val data = mutableListOf() + for ((_, stack) in inventoryItems) { + val crop = GardenCropMilestones.getCropTypeByLore(stack) ?: continue + checkForWrongData(stack, crop, data) + + CropMilestoneUpdateEvent().postAndCatch() + } + + if (data.isNotEmpty()) { + LorenzUtils.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) { + LorenzUtils.chat("No wrong crop milestone steps found!") + } + } + } + + private fun checkForWrongData( + stack: ItemStack, + crop: CropType, + wrongData: MutableList + ) { + val name = stack.name ?: return + val rawNumber = name.removeColor().replace(crop.cropName, "").trim() + val realTier = if (rawNumber == "") 0 else rawNumber.romanToDecimalIfNeeded() + + 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 = pattern.matchMatcher(next) { + group("max").formatNumber() + } ?: 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) +// println("guessTotalMax: ${guessTotalMax.addSeparators()}") + val totalMax = pattern.matchMatcher(total) { + group("max").formatNumber() + } ?: 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(rawCrop) + + if (tryFix(crop, tier.toInt(), amount.formatNumber().toInt())) { + fixed++ + } else { + alreadyCorrect++ + } + } + totalFixedValues += fixed + LorenzUtils.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>, tier: Int, amount: Int) { + map[crop] = map[crop]!!.editCopy { + this[tier] = amount + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesFix.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesFix.kt deleted file mode 100644 index cad8fdddf..000000000 --- a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestonesFix.kt +++ /dev/null @@ -1,159 +0,0 @@ -package at.hannibal2.skyhanni.data - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.config.ConfigManager -import at.hannibal2.skyhanni.events.CropMilestoneUpdateEvent -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.LorenzUtils -import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy -import at.hannibal2.skyhanni.utils.LorenzUtils.nextAfter -import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators -import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber -import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded -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 kotlinx.coroutines.launch -import net.minecraft.item.ItemStack - -object GardenCropMilestonesFix { - private val pattern = ".*§e(?.*)§6/§e(?.*)".toPattern() - - fun openInventory(inventoryItems: Map) { - if (SkyHanniMod.feature.garden.copyMilestoneData) { - fixForWrongData(inventoryItems) - } - } - - private fun fixForWrongData(inventoryItems: Map) { - val data = mutableListOf() - for ((_, stack) in inventoryItems) { - val crop = GardenCropMilestones.getCropTypeByLore(stack) ?: continue - checkForWrongData(stack, crop, data) - - CropMilestoneUpdateEvent().postAndCatch() - } - - if (data.isNotEmpty()) { - LorenzUtils.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 { -// LorenzUtils.chat("No wrong crop milestone steps found!") - } - } - - private fun checkForWrongData( - stack: ItemStack, - crop: CropType, - wrongData: MutableList - ) { - val name = stack.name ?: return - val rawNumber = name.removeColor().replace(crop.cropName, "").trim() - val realTier = if (rawNumber == "") 0 else rawNumber.romanToDecimalIfNeeded() - - 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 = pattern.matchMatcher(next) { - group("max").formatNumber() - } ?: 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) -// println("guessTotalMax: ${guessTotalMax.addSeparators()}") - val totalMax = pattern.matchMatcher(total) { - group("max").formatNumber() - } ?: 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(rawCrop) - - if (tryFix(crop, tier.toInt(), amount.formatNumber().toInt())) { - fixed++ - } else { - alreadyCorrect++ - } - } - totalFixedValues += fixed - LorenzUtils.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>, tier: Int, amount: Int) { - map[crop] = map[crop]!!.editCopy { - this[tier] = amount - } - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/jsonobjects/GardenJson.java b/src/main/java/at/hannibal2/skyhanni/utils/jsonobjects/GardenJson.java index cc8bcd51d..7bc9cf7fa 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/jsonobjects/GardenJson.java +++ b/src/main/java/at/hannibal2/skyhanni/utils/jsonobjects/GardenJson.java @@ -16,6 +16,9 @@ public class GardenJson { @Expose public Map> crop_milestones; + @Expose + public Map crop_milestone_community_help; + @Expose public Map visitors; -- cgit