diff options
| author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-04-28 20:43:18 +0200 |
|---|---|---|
| committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-04-28 20:43:29 +0200 |
| commit | cc73e7bf227c96e8f9608aa621e8f84c0128ec08 (patch) | |
| tree | be2eea214952108ae8acd658f4646403129a3e28 | |
| parent | 4e029de5980fb0eb534609bc1ea68c22d21e7507 (diff) | |
| download | SkyHanni-cc73e7bf227c96e8f9608aa621e8f84c0128ec08.tar.gz SkyHanni-cc73e7bf227c96e8f9608aa621e8f84c0128ec08.tar.bz2 SkyHanni-cc73e7bf227c96e8f9608aa621e8f84c0128ec08.zip | |
REGEX
39 files changed, 332 insertions, 361 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/api/CollectionAPI.kt b/src/main/java/at/hannibal2/skyhanni/api/CollectionAPI.kt index 074120d66..08010207a 100644 --- a/src/main/java/at/hannibal2/skyhanni/api/CollectionAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/api/CollectionAPI.kt @@ -9,12 +9,13 @@ 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.NEUItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class CollectionAPI { - private val counterPattern = "(?:.*) §e(.*)§6\\/(?:.*)".toPattern() - private val singleCounterPattern = "§7Total Collected: §e(.*)".toPattern() + private val counterPattern = "(?:.*) §e(?<amount>.*)§6\\/(?:.*)".toPattern() + private val singleCounterPattern = "§7Total Collected: §e(?<amount>.*)".toPattern() // private val hypixelApiHasWrongItems = listOf( // "WOOL", @@ -60,9 +61,8 @@ class CollectionAPI { if (inventoryName.endsWith(" Collection")) { val stack = event.inventoryItems[4] ?: return for (line in stack.getLore()) { - val matcher = singleCounterPattern.matcher(line) - if (matcher.matches()) { - val counter = matcher.group(1).replace(",", "").toLong() + singleCounterPattern.matchMatcher(line) { + val counter = group("amount").replace(",", "").toLong() val name = inventoryName.split(" ").dropLast(1).joinToString(" ") collectionValue[name] = counter } @@ -85,9 +85,8 @@ class CollectionAPI { } for (line in lore) { - val matcher = counterPattern.matcher(line) - if (matcher.matches()) { - val counter = matcher.group(1).replace(",", "").toLong() + counterPattern.matchMatcher(line) { + val counter = group("amount").replace(",", "").toLong() collectionValue[name] = counter } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt index f4c2802f2..d07172609 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt @@ -5,12 +5,13 @@ import at.hannibal2.skyhanni.events.CropMilestoneUpdateEvent import at.hannibal2.skyhanni.events.InventoryOpenEvent import at.hannibal2.skyhanni.features.garden.CropType import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.event.world.WorldEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class GardenCropMilestones { - private val cropPattern = "§7Harvest §f(.*) §7on .*".toPattern() - private val totalPattern = "§7Total: §a(.*)".toPattern() + private val cropPattern = "§7Harvest §f(?<name>.*) §7on .*".toPattern() + private val totalPattern = "§7Total: §a(?<name>.*)".toPattern() // Add when api support is there // @SubscribeEvent @@ -41,14 +42,12 @@ class GardenCropMilestones { for ((_, stack) in event.inventoryItems) { var crop: CropType? = null for (line in stack.getLore()) { - var matcher = cropPattern.matcher(line) - if (matcher.matches()) { - val name = matcher.group(1) - crop = CropType.getByNameOrNull(name) ?: continue + cropPattern.matchMatcher(line) { + val name = group("name") + crop = CropType.getByNameOrNull(name) } - matcher = totalPattern.matcher(line) - if (matcher.matches()) { - val amount = matcher.group(1).replace(",", "").toLong() + totalPattern.matchMatcher(line) { + val amount = group("name").replace(",", "").toLong() crop?.setCounter(amount) } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt b/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt index c6d540ecb..11435ae96 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt @@ -9,13 +9,13 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class SkillExperience { - - private val actionBarPattern = "(?:.*)§3\\+(?:.*) (.*) \\((.*)\\/(.*)\\)(?:.*)".toPattern() - private val inventoryPattern = "(?:.*) §e(.*)§6\\/(?:.*)".toPattern() + private val actionBarPattern = "(?:.*)§3\\+(?:.*) (?<skill>.*) \\((?<overflow>.*)\\/(?<needed>.*)\\)(?:.*)".toPattern() + private val inventoryPattern = "(?:.*) §e(?<number>.*)§6\\/(?:.*)".toPattern() @SubscribeEvent fun onProfileDataLoad(event: ProfileApiDataLoadedEvent) { @@ -38,15 +38,14 @@ class SkillExperience { fun onActionBar(event: LorenzActionBarEvent) { if (!LorenzUtils.inSkyBlock) return - val matcher = actionBarPattern.matcher(event.message) - if (!matcher.matches()) return - - val skill = matcher.group(1).lowercase() - val overflow = matcher.group(2).formatNumber() - val neededForNextLevel = matcher.group(3).formatNumber() - val nextLevel = getLevelForExpExactly(neededForNextLevel) - val baseExp = getExpForLevel(nextLevel - 1) - skillExp[skill] = baseExp + overflow + actionBarPattern.matchMatcher(event.message) { + val skill = group("skill").lowercase() + val overflow = group("overflow").formatNumber() + val neededForNextLevel = group("needed").formatNumber() + val nextLevel = getLevelForExpExactly(neededForNextLevel) + val baseExp = getExpForLevel(nextLevel - 1) + skillExp[skill] = baseExp + overflow + } } @SubscribeEvent @@ -70,9 +69,8 @@ class SkillExperience { val skillName = split[0].lowercase() val level = split[1].romanToDecimal() val baseExp = getExpForLevel(level) - val matcher = inventoryPattern.matcher(line) - if (matcher.matches()) { - val rawNumber = matcher.group(1) + inventoryPattern.matchMatcher(line) { + val rawNumber = group("number") val overflow = rawNumber.formatNumber() val experience = baseExp + overflow skillExp[skillName] = experience diff --git a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarCancelledBuyOrderClipboard.kt b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarCancelledBuyOrderClipboard.kt index e3f50ad0b..3fce3b4d3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarCancelledBuyOrderClipboard.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarCancelledBuyOrderClipboard.kt @@ -6,15 +6,16 @@ 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.OSUtils +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.event.entity.player.ItemTooltipEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Pattern class BazaarCancelledBuyOrderClipboard { - private val patternLastAmount = Pattern.compile("§a(.*)§7x") + private val patternLastAmount = Pattern.compile("§a(?<amount>.*)§7x") private val patternCancelledMessage = - "§6\\[Bazaar] §r§7§r§cCancelled! §r§7Refunded §r§6(.*) coins §r§7from cancelling Buy Order!".toPattern() + "§6\\[Bazaar] §r§7§r§cCancelled! §r§7Refunded §r§6(?<coins>.*) coins §r§7from cancelling Buy Order!".toPattern() private var latestAmount: String? = null @@ -29,7 +30,7 @@ class BazaarCancelledBuyOrderClipboard { for (line in stack.getLore()) { val matcher = patternLastAmount.matcher(line) if (matcher.find()) { - latestAmount = matcher.group(1) + latestAmount = matcher.group("amount") } } } @@ -38,13 +39,9 @@ class BazaarCancelledBuyOrderClipboard { fun onChat(event: LorenzChatEvent) { if (!isEnabled()) return - val message = event.message - - val matcher = patternCancelledMessage.matcher(message) - if (matcher.matches()) { + patternCancelledMessage.matchMatcher(event.message) { event.blockedReason = "bazaar cancelled buy order clipbaord" - val coins = matcher.group(1) - + val coins = group("coins") LorenzUtils.chat("§e[SkyHanni] Bazaar buy order cancelled. $latestAmount saved to clipboard. ($coins coins)") latestAmount?.let { OSUtils.copyToClipboard(it.replace(",", "")) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarOrderHelper.kt b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarOrderHelper.kt index 1e00d8c22..fa0dd8187 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarOrderHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarOrderHelper.kt @@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RenderUtils.highlight +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraft.client.gui.inventory.GuiChest import net.minecraft.inventory.ContainerChest import net.minecraft.inventory.Slot @@ -43,13 +44,12 @@ class BazaarOrderHelper { if (slot.stack == null) continue val itemName = slot.stack.name ?: continue - val matcher = bazaarItemNamePattern.matcher(itemName) - if (!matcher.matches()) continue + bazaarItemNamePattern.matchMatcher(itemName) { + val buyOrSell = group("type").let { (it == "BUY") to (it == "SELL") } + if (buyOrSell.let { !it.first && !it.second }) return - val buyOrSell = matcher.group("type").let { (it == "BUY") to (it == "SELL") } - if (buyOrSell.let { !it.first && !it.second }) continue - - highlightItem(matcher.group("name"), slot, buyOrSell) + highlightItem(group("name"), slot, buyOrSell) + } } } @@ -62,14 +62,13 @@ class BazaarOrderHelper { val itemLore = slot.stack.getLore() for (line in itemLore) { - if (filledPattern.matcher(line).matches()) { + filledPattern.matchMatcher(line) { slot highlight LorenzColor.GREEN return } - val matcher = pricePattern.matcher(line) - if (matcher.matches()) { - val price = matcher.group("number").replace(",", "").toDouble() + pricePattern.matchMatcher(line) { + val price = group("number").replace(",", "").toDouble() if (buyOrSell.first && price < data.sellPrice || buyOrSell.second && price > data.buyPrice) { slot highlight LorenzColor.GOLD return diff --git a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardDisplay.kt index cc4faeaeb..229c3fc69 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardDisplay.kt @@ -11,6 +11,7 @@ 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.RenderUtils.renderStrings +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiChat @@ -27,7 +28,7 @@ class BingoCardDisplay { private var tick = 0 private var display = listOf<String>() private val config get() = SkyHanniMod.feature.bingo - private val goalCompletePattern = "§6§lBINGO GOAL COMPLETE! §r§e(.*)".toPattern() + private val goalCompletePattern = "§6§lBINGO GOAL COMPLETE! §r§e(?<name>.*)".toPattern() init { update() @@ -179,9 +180,8 @@ class BingoCardDisplay { if (!LorenzUtils.isBingoProfile) return if (!config.cardDisplay) return - val matcher = goalCompletePattern.matcher(event.message) - if (matcher.matches()) { - val name = matcher.group(1) + goalCompletePattern.matchMatcher(event.message) { + val name = group("name") personalGoals.filter { it.displayName == name } .forEach { it.done = true diff --git a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoNextStepHelper.kt b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoNextStepHelper.kt index 28faaf877..472fe1a81 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoNextStepHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoNextStepHelper.kt @@ -10,6 +10,7 @@ import at.hannibal2.skyhanni.features.bingo.nextstep.* import at.hannibal2.skyhanni.utils.InventoryUtils import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.matchRegex import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -22,9 +23,9 @@ class BingoNextStepHelper { private val itemIslandRequired = mutableMapOf<String, IslandVisitStep>() private val itemRequired = mutableMapOf<String, NextStep>() private val islands = mutableMapOf<IslandType, IslandVisitStep>() - private val collectionPattern = "Reach ([0-9]+(?:,\\d+)*) (.*) Collection\\.".toPattern() - private val crystalPattern = "Obtain a (\\w+) Crystal in the Crystal Hollows\\.".toPattern() - private val skillPattern = "Obtain level (.*) in the (.*) Skill.".toPattern() + private val collectionPattern = "Reach (?<amount>[0-9]+(?:,\\d+)*) (?<name>.*) Collection\\.".toPattern() + private val crystalPattern = "Obtain a (?<name>\\w+) Crystal in the Crystal Hollows\\.".toPattern() + private val skillPattern = "Obtain level (?<level>.*) in the (?<skill>.*) Skill.".toPattern() companion object { private val finalSteps = mutableListOf<NextStep>() @@ -219,49 +220,52 @@ class BingoNextStepHelper { dirty = false for (goal in personalGoals) { - val description = goal.description.removeColor() + readDescription(goal.description.removeColor()) + } - val collectionMatcher = collectionPattern.matcher(description) - if (collectionMatcher.matches()) { - val amount = collectionMatcher.group(1).replace(",", "").toInt() - val name = collectionMatcher.group(2) + updateResult() + } - val collectionStep = CollectionStep(name, amount).apply { finalSteps.add(this) } - createItemIslandRequirement(name, collectionStep) - continue - } - if (description == "Craft an Emerald Ring.") { - CraftStep("Emerald Ring").apply { finalSteps.add(this) } requires ItemsStep( - "32x Enchanted Emerald", - "Emerald", - 160 * 32, - mapOf("Emerald" to 1, "Enchanted Emerald" to 160) - ).apply { this requires IslandType.DWARVEN_MINES.getStep() } - } - if (description == "Obtain a Mathematical Hoe Blueprint.") { - CraftStep("Mathematical Hoe Blueprint").apply { finalSteps.add(this) } requires ItemsStep( - "32x Jacob's Ticket", - "Jacob's Ticket", - 32, - mapOf("Jacob's Ticket" to 1) - ).apply { this requires IslandType.GARDEN.getStep() }.addItemRequirements() - } - val crystalMatcher = crystalPattern.matcher(description) - if (crystalMatcher.matches()) { - val crystal = crystalMatcher.group(1) - ChatMessageStep("Obtain a $crystal Crystal").apply { finalSteps.add(this) } requires IslandType.CRYSTAL_HOLLOWS.getStep() - } - val matcher = skillPattern.matcher(description) - if (matcher.matches()) { - val level = matcher.group(1).toInt() - val skillName = matcher.group(2) - SkillLevelStep(skillName, level).apply { finalSteps.add(this) } - } + private fun readDescription(description: String) { + collectionPattern.matchMatcher(description) { + val amount = group("amount").replace(",", "").toInt() + val name = group("name") + + val collectionStep = CollectionStep(name, amount).apply { finalSteps.add(this) } + createItemIslandRequirement(name, collectionStep) + return + } - println("No help for goal: '$description'") + if (description == "Craft an Emerald Ring.") { + CraftStep("Emerald Ring").apply { finalSteps.add(this) } requires ItemsStep( + "32x Enchanted Emerald", + "Emerald", + 160 * 32, + mapOf("Emerald" to 1, "Enchanted Emerald" to 160) + ).apply { this requires IslandType.DWARVEN_MINES.getStep() } } - updateResult() + if (description == "Obtain a Mathematical Hoe Blueprint.") { + CraftStep("Mathematical Hoe Blueprint").apply { finalSteps.add(this) } requires ItemsStep( + "32x Jacob's Ticket", + "Jacob's Ticket", + 32, + mapOf("Jacob's Ticket" to 1) + ).apply { this requires IslandType.GARDEN.getStep() }.addItemRequirements() + } + + crystalPattern.matchMatcher(description) { + val crystal = group("name") + ChatMessageStep("Obtain a $crystal Crystal").apply { finalSteps.add(this) } requires IslandType.CRYSTAL_HOLLOWS.getStep() + } + + skillPattern.matchMatcher(description) { + val level = group("level").toInt() + val skill = group("skill") + SkillLevelStep(skill, level).apply { finalSteps.add(this) } + } + + println("No help for goal: '$description'") } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt b/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt index 6fb885ff2..62077f08c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt @@ -11,6 +11,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import io.github.moulberry.notenoughupdates.NotEnoughUpdates import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe @@ -21,7 +22,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.TickEvent class MinionCraftHelper { - private var minionNamePattern = "(.*) Minion (.*)".toPattern() + private var minionNamePattern = "(?<name>.*) Minion (?<number>.*)".toPattern() private var tick = 0 private var display = listOf<String>() private var hasMinionInInventory = false @@ -75,11 +76,11 @@ class MinionCraftHelper { ): MutableList<String> { val newDisplay = mutableListOf<String>() for ((minionName, minionId) in minions) { - val matcher = minionNamePattern.matcher(minionName) - if (!matcher.matches()) continue - val cleanName = matcher.group(1).removeColor() - val number = matcher.group(2).romanToDecimalIfNeeded() - addMinion(cleanName, number, minionId, otherItems, newDisplay) + minionNamePattern.matchMatcher(minionName) { + val cleanName = group("name").removeColor() + val number = group("number").romanToDecimalIfNeeded() + addMinion(cleanName, number, minionId, otherItems, newDisplay) + } } return newDisplay } @@ -273,7 +274,7 @@ class MinionCraftHelper { if (event.inventoryName != "Crafted Minions") return for ((_, b) in event.inventoryItems) { - val name = b.name?: continue + val name = b.name ?: continue if (!name.startsWith("§e")) continue val internalName = NEUItems.getInternalName("$name I") diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt index 1de8f09a2..2bfb0c9cf 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.getLorenzVec import net.minecraft.client.Minecraft @@ -18,7 +19,7 @@ class PlayerDeathMessages { private val lastTimePlayerSeen = mutableMapOf<String, Long>() //§c ☠ §r§7§r§bZeroHazel§r§7 was killed by §r§8§lAshfang§r§7§r§7. - private val pattern = "§c ☠ §r§7§r§.(.+)§r§7 (.+)".toPattern() + private val deathMessagePattern = "§c ☠ §r§7§r§.(?<name>.+)§r§7 (?<reason>.+)".toPattern() @SubscribeEvent fun onTick(event: TickEvent.ClientTickEvent) { @@ -34,12 +35,11 @@ class PlayerDeathMessages { |
