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 | bd6a0bb93ed6c0499cc3c5e044b7554481825bb5 (patch) | |
tree | be2eea214952108ae8acd658f4646403129a3e28 /src/main/java/at/hannibal2/skyhanni/features/garden | |
parent | 9df04ac5aaa5e9897324af306dc73b8b1e67f600 (diff) | |
download | skyhanni-bd6a0bb93ed6c0499cc3c5e044b7554481825bb5.tar.gz skyhanni-bd6a0bb93ed6c0499cc3c5e044b7554481825bb5.tar.bz2 skyhanni-bd6a0bb93ed6c0499cc3c5e044b7554481825bb5.zip |
REGEX
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/garden')
13 files changed, 145 insertions, 162 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt index fb6453add..3539e40af 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt @@ -8,6 +8,7 @@ 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 at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Pattern @@ -19,34 +20,28 @@ class GardenCropMilestoneFix { @SubscribeEvent fun onChatMessage(event: LorenzChatEvent) { - val matcher = levelUpPattern.matcher(event.message) - if (!matcher.matches()) return + levelUpPattern.matchMatcher(event.message) { + val cropName = group("crop") + val crop = CropType.getByNameOrNull(cropName) ?: return - val cropName = matcher.group("crop") - val crop = CropType.getByNameOrNull(cropName) - if (crop == null) { - LorenzUtils.debug("GardenCropMilestoneFix: crop is null: '$cropName'") - return - } + val tier = group("tier").romanToDecimalIfNeeded() - val tier = matcher.group("tier").romanToDecimalIfNeeded() - - val crops = GardenCropMilestones.getCropsForTier(tier) - changedValue(crop, crops, "level up chat message") + 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") + tabListPattern.matchMatcher(line) { + val tier = group("tier").toInt() + val percentage = group("percentage").toDouble() + val cropName = group("crop") - check(cropName, tier, percentage) - return + check(cropName, tier, percentage) + return + } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt index 6c4abadb8..38b31b57e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt @@ -10,22 +10,23 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded import at.hannibal2.skyhanni.utils.RenderUtils.renderString +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.math.roundToInt class GardenLevelDisplay { private val config get() = SkyHanniMod.feature.garden - private val expToNextLevelPattern = "(?:.*) §e(.*)§6\\/(?:.*)".toPattern() + private val expToNextLevelPattern = "(?:.*) §e(?<nextLevelExp>.*)§6\\/(?:.*)".toPattern() private val overflowPattern = ".*§r §6(?<overflow>.*) XP".toPattern() - private val namePattern = "Garden Level (.*)".toPattern() + private val namePattern = "Garden Level (?<currentLevel>.*)".toPattern() private var gardenExp get() = SkyHanniMod.feature.hidden.gardenExp set(value) { SkyHanniMod.feature.hidden.gardenExp = value } private var display = "" - private var visitorRewardPattern = " {4}§r§8\\+§r§2(.*) §r§7Garden Experience".toPattern() + private var visitorRewardPattern = " {4}§r§8\\+§r§2(?<exp>.*) §r§7Garden Experience".toPattern() @SubscribeEvent fun onProfileJoin(event: ProfileJoinEvent) { @@ -36,10 +37,8 @@ class GardenLevelDisplay { fun onChatMessage(event: LorenzChatEvent) { if (!isEnabled()) return - val matcher = visitorRewardPattern.matcher(event.message) - if (matcher.matches()) { - val moreExp = matcher.group(1).toInt() - gardenExp += moreExp + visitorRewardPattern.matchMatcher(event.message) { + gardenExp += group("exp").toInt() update() } } @@ -50,28 +49,24 @@ class GardenLevelDisplay { if (event.inventoryName != "Desk") return val item = event.inventoryItems[4]!! - val name = item.name!!.removeColor() - val nameMatcher = namePattern.matcher(name) - if (!nameMatcher.matches()) return - val currentLevel = nameMatcher.group(1).romanToDecimalIfNeeded() - var nextLevelExp = 0 - for (line in item.getLore()) { - var matcher = expToNextLevelPattern.matcher(line) - if (matcher.matches()) { - nextLevelExp = matcher.group(1).replace(",", "").toDouble().roundToInt() - break - } - matcher = overflowPattern.matcher(line) - if (matcher.matches()) { - val overflow = matcher.group("overflow").replace(",", "").toDouble().roundToInt() - gardenExp = overflow - update() - return + namePattern.matchMatcher(item.name!!.removeColor()) { + val currentLevel = group("currentLevel").romanToDecimalIfNeeded() + var nextLevelExp = 0 + for (line in item.getLore()) { + expToNextLevelPattern.matchMatcher(line) { + nextLevelExp = group("nextLevelExp").replace(",", "").toDouble().roundToInt() + } + overflowPattern.matchMatcher(line) { + val overflow = group("overflow").replace(",", "").toDouble().roundToInt() + gardenExp = overflow + update() + return + } } + val expForLevel = getExpForLevel(currentLevel).toInt() + gardenExp = expForLevel + nextLevelExp + update() } - val expForLevel = getExpForLevel(currentLevel).toInt() - gardenExp = expForLevel + nextLevelExp - update() } private fun update() { diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt index 7b9e52d14..c7ea644cb 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextJacobContest.kt @@ -10,10 +10,12 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RenderUtils.renderSingleLineWithItems import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.TimeUtils import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlinx.coroutines.launch +import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.TickEvent import org.lwjgl.opengl.Display @@ -31,9 +33,9 @@ class GardenNextJacobContest { private var tick = 0 private var contests = mutableMapOf<Long, FarmingContest>() private var inCalendar = false - private val patternDay = "§aDay (.*)".toPattern() - private val patternMonth = "(.*), Year (.*)".toPattern() - private val patternCrop = "§e○ §7(.*)".toPattern() + private val patternDay = "§aDay (?<day>.*)".toPattern() + private val patternMonth = "(?<month>.*), Year (?<year>.*)".toPattern() + private val patternCrop = "§e○ §7(?<crop>.*)".toPattern() private val maxContestsPerYear = 124 private val contestDuration = 1_000 * 60 * 20 @@ -92,17 +94,16 @@ class GardenNextJacobContest { if (lore[0] != "§7To Calendar and Events") return inCalendar = true - readCalendar(event) - } - private fun readCalendar(event: InventoryOpenEvent) { - val inventoryName = event.inventoryName + patternMonth.matchMatcher(event.inventoryName) { + val month = LorenzUtils.getSBMonthByName(group("month")) + val year = group("year").toInt() - val matcher = patternMonth.matcher(inventoryName) - if (!matcher.matches()) return - val month = LorenzUtils.getSBMonthByName(matcher.group(1)) - val year = matcher.group(2).toInt() + readCalendar(event.inventoryItems.values, year, month) + } + } + private fun readCalendar(items: MutableCollection<ItemStack>, year: Int, month: Int) { if (contests.isNotEmpty()) { val contest = contests.values.first() val endTime = contest.endTime @@ -113,7 +114,7 @@ class GardenNextJacobContest { } } - for (item in event.inventoryItems.values) { + for (item in items) { val lore = item.getLore() if (!lore.any { it.contains("§6§eJacob's Farming Contest") }) continue @@ -121,13 +122,13 @@ class GardenNextJacobContest { val matcherDay = patternDay.matcher(name) if (!matcherDay.matches()) continue - val day = matcherDay.group(1).toInt() + val day = matcherDay.group("day").toInt() val startTime = SkyBlockTime(year, month, day).toMillis() val crops = mutableListOf<CropType>() for (line in lore) { val matcherCrop = patternCrop.matcher(line) if (!matcherCrop.matches()) continue - crops.add(CropType.getByName(matcherCrop.group(1))) + crops.add(CropType.getByName(matcherCrop.group("crop"))) } val contest = FarmingContest(startTime + contestDuration, crops) contests[startTime] = contest diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenOptimalSpeed.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenOptimalSpeed.kt index d131aa91a..8653502d1 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenOptimalSpeed.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenOptimalSpeed.kt @@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.events.TabListUpdateEvent import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RenderUtils.renderString import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.renderables.Renderable import io.github.moulberry.notenoughupdates.mixins.AccessorGuiEditSign @@ -22,7 +23,7 @@ class GardenOptimalSpeed { private val configCustomSpeed get() = config.optimalSpeedCustom private var currentSpeed = 100 private var optimalSpeed = -1 - private val currentSpeedPattern = " Speed: §r§f✦(.*)".toPattern() + private val currentSpeedPattern = " Speed: §r§f✦(?<speed>.*)".toPattern() private var lastWarnTime = 0L private var cropInHand: CropType? = null private var rancherOverlayList: List<List<Any?>> = emptyList() @@ -30,9 +31,8 @@ class GardenOptimalSpeed { @SubscribeEvent fun onTabListUpdate(event: TabListUpdateEvent) { for (line in event.tabList) { - val matcher = currentSpeedPattern.matcher(line) - if (matcher.matches()) { - currentSpeed = matcher.group(1).toInt() + currentSpeedPattern.matchMatcher(line) { + currentSpeed = group("speed").toInt() } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenTeleportPadCompactName.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenTeleportPadCompactName.kt index 4dcd73745..a23177f39 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenTeleportPadCompactName.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenTeleportPadCompactName.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.features.garden import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.item.EntityArmorStand import net.minecraftforge.client.event.RenderLivingEvent @@ -8,7 +9,7 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class GardenTeleportPadCompactName { - private val patternName = "§.✦ §aWarp To (.*)".toPattern() + private val patternName = "§.✦ §aWarp To (?<name>.*)".toPattern() private val patternNoName = "§.✦ §cNo Destination".toPattern() @SubscribeEvent(priority = EventPriority.HIGH) @@ -19,13 +20,12 @@ class GardenTeleportPadCompactName { val name = entity.name - if (patternNoName.matcher(name).matches()) { + patternNoName.matchMatcher(name) { event.isCanceled = true } - val matcher = patternName.matcher(name) - if (matcher.matches()) { - entity.customNameTag = matcher.group(1) + patternName.matchMatcher(name) { + entity.customNameTag = group("name") } } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt index ba9cbe9a1..763186a03 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterDisplay.kt @@ -9,6 +9,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.TimeUtils import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.time.Duration @@ -115,9 +116,8 @@ class ComposterDisplay { if (next) { if (line == "") break for (type in DataType.values()) { - val matcher = type.pattern.matcher(line) - if (matcher.matches()) { - newData[type] = matcher.group(1) + type.pattern.matchMatcher(line) { + newData[type] = group(1) } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterInventoryNumbers.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterInventoryNumbers.kt index aaccea8bd..b8a7a4fc3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterInventoryNumbers.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterInventoryNumbers.kt @@ -6,12 +6,13 @@ import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class ComposterInventoryNumbers { - private val valuePattern = "(?:.*) §e(.*)§6\\/(.*)".toPattern() - private val compostsPattern = "§7§7Compost Available: §a(.*)".toPattern() + private val valuePattern = "(?:.*) §e(?<having>.*)§6\\/(?<total>.*)".toPattern() + private val compostsPattern = "§7§7Compost Available: §a(?<amount>.*)".toPattern() @SubscribeEvent fun onRenderItemTip(event: RenderInventoryItemTipEvent) { @@ -27,45 +28,41 @@ class ComposterInventoryNumbers { // Composts Available if (slotNumber == 22) { for (line in stack.getLore()) { - val matcher = compostsPattern.matcher(line) - if (!matcher.matches()) continue - - val total = matcher.group(1).replace(",", "").toInt() - if (total <= 64) continue - - event.offsetY = -2 - event.offsetX = -20 - event.stackTip = "§6${total.addSeparators()}" - return + compostsPattern.matchMatcher(line) { + val total = group("amount").replace(",", "").toInt() + event.offsetY = -2 + event.offsetX = -20 + event.stackTip = "§6${total.addSeparators()}" + return + } } } // Organic Matter or Fuel if (slotNumber == 46 || slotNumber == 52) { for (line in stack.getLore()) { - val matcher = valuePattern.matcher(line) - if (!matcher.matches()) continue + valuePattern.matchMatcher(line) { + val having = group("having").removeColor().replace(",", "").toDouble().toInt() + val havingFormat = NumberUtil.format(having) + val total = group("total").removeColor() - val having = matcher.group(1).removeColor().replace(",", "").toDouble().toInt() - val havingFormat = NumberUtil.format(having) - val total = matcher.group(2).removeColor() + val color = if (slotNumber == 46) { + // Organic Matter + event.offsetY = -95 + event.offsetX = 5 + event.alignLeft = false + "§e" + } else { + // Fuel + event.offsetY = -76 + event.offsetX = -20 + "§a" + } - val color = if (slotNumber == 46) { - // Organic Matter - event.offsetY = -95 - event.offsetX = 5 - event.alignLeft = false - "§e" - } else { - // Fuel - event.offsetY = -76 - event.offsetX = -20 - "§a" + event.stackTip = "$color$havingFormat/$total" + return } - - event.stackTip = "$color$havingFormat/$total" - return } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt index 3e98858d8..3cdc0b699 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt @@ -13,6 +13,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.jsonobjects.GardenJson import at.hannibal2.skyhanni.utils.renderables.Renderable @@ -120,10 +121,9 @@ class ComposterOverlay { for (upgrade in ComposterUpgrade.values()) { event.itemStack?.name?.let { if (it.contains(upgrade.displayName)) { - val matcher = ComposterUpgrade.regex.matcher(it) - matcher.matches() - val level = matcher.group("level")?.romanToDecimalIfNeeded() ?: 0 - maxLevel = level == 25 + maxLevel = ComposterUpgrade.regex.matchMatcher(it) { + group("level")?.romanToDecimalIfNeeded() ?: 0 + } == 25 extraComposterUpgrade = upgrade update() return diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenInventoryNumbers.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenInventoryNumbers.kt index 0bf7be4da..c62b0c84d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenInventoryNumbers.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenInventoryNumbers.kt @@ -8,13 +8,14 @@ import at.hannibal2.skyhanni.utils.InventoryUtils import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class GardenInventoryNumbers { private val config get() = SkyHanniMod.feature.garden - private var patternTierProgress = "§7Progress to Tier (.*): §e(?:.*)".toPattern() - private var patternUpgradeTier = "§7Current Tier: §[ea](.*)§7/§a.*".toPattern() + private var patternTierProgress = "§7Progress to Tier (?<tier>.*): §e(?:.*)".toPattern() + private var patternUpgradeTier = "§7Current Tier: §[ea](?<tier>.*)§7/§a.*".toPattern() @SubscribeEvent fun onRenderItemTip(event: RenderItemTipEvent) { @@ -26,7 +27,7 @@ class GardenInventoryNumbers { event.stack.getLore() .map { patternTierProgress.matcher(it) } .filter { it.matches() } - .map { it.group(1).romanToDecimalIfNeeded() - 1 } + .map { it.group("tier").romanToDecimalIfNeeded() - 1 } .forEach { event.stackTip = "" + it } } @@ -36,7 +37,7 @@ class GardenInventoryNumbers { event.stack.getLore() .map { patternUpgradeTier.matcher(it) } .filter { it.matches() } - .map { it.group(1) } + .map { it.group("tier") } .forEach { event.stackTip = "" + it } } @@ -44,9 +45,8 @@ class GardenInventoryNumbers { if (!config.numberComposterUpgrades) return event.stack.name?.let { - val matcher = ComposterUpgrade.regex.matcher(it) - if (matcher.matches()) { - val level = matcher.group("level")?.romanToDecimalIfNeeded() ?: 0 + ComposterUpgrade.regex.matchMatcher(it) { + val level = group("level")?.romanToDecimalIfNeeded() ?: 0 event.stackTip = "$level" } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenTeleportPadInventoryNumber.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenTeleportPadInventoryNumber.kt index dcb018404..5dc226327 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenTeleportPadInventoryNumber.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenTeleportPadInventoryNumber.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.events.InventoryOpenEvent import at.hannibal2.skyhanni.events.RenderInventoryItemTipEvent import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class GardenTeleportPadInventoryNumber { @@ -57,7 +58,7 @@ class GardenTeleportPadInventoryNumber { } private var inTeleportPad = false - private val pattern = "§.(.*) teleport pad".toPattern() + private val pattern = "§.(?<number>.*) teleport pad".toPattern() @SubscribeEvent fun onInventoryOpen(event: InventoryOpenEvent) { @@ -71,13 +72,12 @@ class GardenTeleportPadInventoryNumber { if (!inTeleportPad) return val name = event.stack.name?.lowercase() ?: return - val matcher = pattern.matcher(name) - if (!matcher.matches()) return - val text = matcher.group(1) - numbers[text]?.let { - event.stackTip = "$it" - return + pattern.matchMatcher(name) { + val text = group("number") + numbers[text]?.let { + event.stackTip = "$it" + } } } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/SkyMartCopperPrice.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/SkyMartCopperPrice.kt index 3a83ff92b..f6138e079 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/SkyMartCopperPrice.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/SkyMartCopperPrice.kt @@ -13,10 +13,11 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class SkyMartCopperPrice { - private val pattern = "§c(.*) Copper".toPattern() + private val pattern = "§c(?<amount>.*) Copper".toPattern() private var display = listOf<List<Any>>() private val config get() = SkyHanniMod.feature.garden @@ -33,25 +34,23 @@ class SkyMartCopperPrice { val table = mutableMapOf<Pair<String, String>, Pair<Double, String>>() for (stack in event.inventoryItems.values) { for (line in stack.getLore()) { - val matcher = pattern.matcher(line) - if (!matcher.matches()) continue - val internalName = stack.getInternalName() val lowestBin = NEUItems.getPrice(internalName) if (lowestBin == -1.0) continue + pattern.matchMatcher(line) { + val amount = group("amount").replace(",", "").toInt() + val factor = lowestBin / amount + val perFormat = NumberUtil.format(factor) + val priceFormat = NumberUtil.format(lowestBin) + val amountFormat = NumberUtil.format(amount) - val amount = matcher.group(1).replace(",", "").toInt() - val factor = lowestBin / amount - val perFormat = NumberUtil.format(factor) - val priceFormat = NumberUtil.format(lowestBin) - val amountFormat = NumberUtil.format(amount) - - val name = stack.nameWithEnchantment!! - val advancedStats = if (config.skyMartCopperPriceAdvancedStats) { - " §7(§6$priceFormat §7/ §c$amountFormat Copper§7)" - } else "" - val pair = Pair("$name§f:", "§6§l$perFormat$advancedStats") - table[pair] = Pair(factor, internalName) + val name = stack.nameWithEnchantment!! + val advancedStats = if (config.skyMartCopperPriceAdvancedStats) { + " §7(§6$priceFormat §7/ §c$amountFormat Copper§7)" + } else "" + val pair = Pair("$name§f:", "§6§l$perFormat$advancedStats") + table[pair] = Pair(factor, internalName) + } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt index 3823d80b8..6066608ec 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt @@ -17,6 +17,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.renderables.Renderable import io.github.moulberry.notenoughupdates.events.SlotClickEvent @@ -41,8 +42,8 @@ class GardenVisitorFeatures { private var lastClickedNpc = 0 private var tick = 0 private val newVisitorArrivedMessage = ".* §r§ehas arrived on your §r§bGarden§r§e!".toPattern() - private val copperPattern = " §8\\+§c(.*) Copper".toPattern() - private val gardenExperiencePattern = " §8\\+§2(.*) §7Garden Experience".toPattern() + private val copperPattern = " §8\\+§c(?<amount>.*) Copper".toPattern() + private val gardenExperiencePattern = " §8\\+§2(?<amount>.*) §7Garden Experience".toPattern() private val visitorChatMessagePattern = "§e\\[NPC] (§.)?(?<name>.*)§f: §r§f.*".toPattern() private val config get() = SkyHanniMod.feature.garden private val logger = LorenzLogger("garden/visitors") @@ -335,17 +336,15 @@ class GardenVisitorFeatures { } if (config.visitorCopperPrice) { - val matcher = copperPattern.matcher(line) - if (matcher.matches()) { - val coppers = matcher.group(1).replace(",", "").toInt() + copperPattern.matchMatcher(line) { + val coppers = group("amount").replace(",", "").toInt() val pricePerCopper = NumberUtil.format((totalPrice / coppers).toInt()) list[i + itemsWithSpeedCounter] = "$line §7(price per §6$pricePerCopper§7)" } } if (config.visitorExperiencePrice) { - val matcher = gardenExperiencePattern.matcher(line) - if (matcher.matches()) { - val gardenExp = matcher.group(1).replace(",", "").toInt() + gardenExperiencePattern.matchMatcher(line) { + val gardenExp = group("amount").replace(",", "").toInt() val pricePerCopper = NumberUtil.format((totalPrice / gardenExp).toInt()) list[i + itemsWithSpeedCounter] = "$line §7(price per §6$pricePerCopper§7)" } @@ -480,16 +479,13 @@ class GardenVisitorFeatures { } } - private fun hideVisitorMessage(message: String): Boolean { - val matcher = visitorChatMessagePattern.matcher(message) - if (!matcher.matches()) return false + private fun hideVisitorMessage(message: String) = visitorChatMessagePattern.matchMatcher(message) { + val name = group("name") + if (name == "Spaceman") return false + if (name == "Beth") return false - val name = matcher.group("name") - if (name == "Spaceman") return false - if (name == "Beth") return false - - return visitors.keys.any { it.removeColor() == name } - } + return visitors.keys.any { it.removeColor() == name } + } ?: false private fun update() { checkVisitorsReady() diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt index 3e5ffa6bb..3535c31cd 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorTimer.kt @@ -8,14 +8,15 @@ import at.hannibal2.skyhanni.events.VisitorArrivalEvent import at.hannibal2.skyhanni.features.garden.CropType.Companion.getCropType import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.utils.RenderUtils.renderString +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.TimeUtils import net.minecraftforge.event.world.WorldEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.math.roundToLong class GardenVisitorTimer { - private val patternNextVisitor = " Next Visitor: §r§b(.*)".toPattern() - private val patternVisitors = "§b§lVisitors: §r§f\\((\\d)\\)".toPattern() + private val patternNextVisitor = " Next Visitor: §r§b(?<time>.*)".toPattern() + private val patternVisitors = "§b§lVisitors: §r§f\\((?<amount>\\d)\\)".toPattern() private var render = "" private var lastMillis = 0L private var lastVisitors: Int = -1 @@ -38,9 +39,9 @@ class GardenVisitorTimer { var millis = visitorInterval var queueFull = false for (line in event.tabList) { - var matcher = patternNextVisitor.matcher(line) + val matcher = patternNextVisitor.matcher(line) if (matcher.matches()) { - val rawTime = matcher.group(1) + val rawTime = matcher.group("time") millis = TimeUtils.getMillis(rawTime) } else if (line == " Next Visitor: §r§c§lQueue Full!") { queueFull = true @@ -49,9 +50,8 @@ class GardenVisitorTimer { return } - matcher = patternVisitors.matcher(line) - if (matcher.matches()) { - visitorsAmount = matcher.group(1).toInt() + patternVisitors.matchMatcher(line) { + visitorsAmount = group("amount").toInt() } } |