summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-04-05 05:46:27 +1100
committerGitHub <noreply@github.com>2024-04-04 20:46:27 +0200
commit15db91ced38a8cc4c7c16565422fce5566c6057c (patch)
tree487804f98d4cad7316ea8b4f0f033fe530b2c4a5 /src/main/java/at/hannibal2/skyhanni/data
parent52afdbf62ef5c35403132696918b9cfcc8faffdb (diff)
downloadskyhanni-15db91ced38a8cc4c7c16565422fce5566c6057c.tar.gz
skyhanni-15db91ced38a8cc4c7c16565422fce5566c6057c.tar.bz2
skyhanni-15db91ced38a8cc4c7c16565422fce5566c6057c.zip
Backend: matchFirst function for lists of strings (#1353)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt15
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt20
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt75
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt10
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt14
5 files changed, 56 insertions, 78 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt
index 54131f4bf..72210ad63 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt
@@ -11,6 +11,7 @@ import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
+import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeResets
@@ -162,15 +163,11 @@ object BitsAPI {
return
}
- for (line in cookieStack.getLore()) {
- bitsAvailableMenuPattern.matchMatcher(line) {
- val amount = group("toClaim").formatInt()
- if (bitsToClaim != amount) {
- bitsToClaim = amount
- sendEvent()
- }
-
- return
+ cookieStack.getLore().matchFirst(bitsAvailableMenuPattern) {
+ val amount = group("toClaim").formatInt()
+ if (bitsToClaim != amount) {
+ bitsToClaim = amount
+ sendEvent()
}
}
return
diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt
index 5f61f0330..0038f4448 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt
@@ -7,8 +7,8 @@ import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
-import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
-import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
+import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
+import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@@ -25,11 +25,9 @@ object GardenCropMilestones {
)
fun getCropTypeByLore(itemStack: ItemStack): CropType? {
- for (line in itemStack.getLore()) {
- cropPattern.matchMatcher(line) {
- val name = group("name")
- return CropType.getByNameOrNull(name)
- }
+ itemStack.getLore().matchFirst(cropPattern) {
+ val name = group("name")
+ return CropType.getByNameOrNull(name)
}
return null
}
@@ -40,11 +38,9 @@ object GardenCropMilestones {
for ((_, stack) in event.inventoryItems) {
val crop = getCropTypeByLore(stack) ?: continue
- for (line in stack.getLore()) {
- totalPattern.matchMatcher(line) {
- val amount = group("name").formatNumber()
- crop.setCounter(amount)
- }
+ stack.getLore().matchFirst(totalPattern) {
+ val amount = group("name").formatLong()
+ crop.setCounter(amount)
}
}
CropMilestoneUpdateEvent().postAndCatch()
diff --git a/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt b/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt
index 7fc50447f..3ea3a9008 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt
@@ -16,6 +16,7 @@ import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzLogger
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
+import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
@@ -72,7 +73,7 @@ class HypixelData {
)
private val scoreboardVisitingAmoutPattern by patternGroup.pattern(
"scoreboard.visiting.amount",
- "\\s+§.✌ §.\\(§.(?<currentamount>\\d+)§.\\/(?<maxamount>\\d+)\\)"
+ "\\s+§.✌ §.\\(§.(?<currentamount>\\d+)§./(?<maxamount>\\d+)\\)"
)
private val guestPattern by patternGroup.pattern(
"guesting.scoreboard",
@@ -127,19 +128,15 @@ class HypixelData {
if (LorenzUtils.lastWorldSwitch.passedSince() < 1.seconds) return
if (!TabListData.fullyLoaded) return
- ScoreboardData.sidebarLinesFormatted.forEach {
- serverIdScoreboardPattern.matchMatcher(it) {
- val serverType = if (group("servertype") == "M") "mega" else "mini"
- serverId = "$serverType${group("serverid")}"
- return
- }
+ ScoreboardData.sidebarLinesFormatted.matchFirst(serverIdScoreboardPattern) {
+ val serverType = if (group("servertype") == "M") "mega" else "mini"
+ serverId = "$serverType${group("serverid")}"
+ return
}
- TabListData.getTabList().forEach {
- serverIdTablistPattern.matchMatcher(it) {
- serverId = group("serverid")
- return
- }
+ TabListData.getTabList().matchFirst(serverIdTablistPattern) {
+ serverId = group("serverid")
+ return
}
ErrorManager.logErrorWithData(
@@ -172,10 +169,8 @@ class HypixelData {
}
fun getMaxPlayersForCurrentServer(): Int {
- for (line in ScoreboardData.sidebarLinesFormatted) {
- scoreboardVisitingAmoutPattern.matchMatcher(line) {
- return group("maxamount").toInt()
- }
+ ScoreboardData.sidebarLinesFormatted.matchFirst(scoreboardVisitingAmoutPattern) {
+ return group("maxamount").toInt()
}
return if (serverId?.startsWith("mega") == true) 80 else 26
}
@@ -255,16 +250,14 @@ class HypixelData {
@SubscribeEvent
fun onTabListUpdate(event: TabListUpdateEvent) {
- for (line in event.tabList) {
- UtilsPatterns.tabListProfilePattern.matchMatcher(line) {
- var newProfile = group("profile").lowercase()
- // Hypixel shows the profile name reversed while in the Rift
- if (RiftAPI.inRift()) newProfile = newProfile.reversed()
- if (profileName == newProfile) return
- profileName = newProfile
- ProfileJoinEvent(newProfile).postAndCatch()
- return
- }
+ event.tabList.matchFirst(UtilsPatterns.tabListProfilePattern) {
+ var newProfile = group("profile").lowercase()
+
+ // Hypixel shows the profile name reversed while in the Rift
+ if (RiftAPI.inRift()) newProfile = newProfile.reversed()
+ if (profileName == newProfile) return
+ profileName = newProfile
+ ProfileJoinEvent(newProfile).postAndCatch()
}
}
@@ -320,15 +313,13 @@ class HypixelData {
skyBlock = inSkyBlock
}
- private fun checkProfileName(): Boolean {
- if (profileName.isEmpty()) {
- val text = TabListData.getTabList().firstOrNull { it.contains("Profile:") } ?: return true
- UtilsPatterns.tabListProfilePattern.matchMatcher(text) {
- profileName = group("profile").lowercase()
- ProfileJoinEvent(profileName).postAndCatch()
- }
+ private fun checkProfileName() {
+ if (profileName.isNotEmpty()) return
+
+ TabListData.getTabList().matchFirst(UtilsPatterns.tabListProfilePattern) {
+ profileName = group("profile").lowercase()
+ ProfileJoinEvent(profileName).postAndCatch()
}
- return false
}
private fun checkHypixel() {
@@ -364,24 +355,22 @@ class HypixelData {
}
private fun checkIsland() {
- var newIsland = ""
+ var foundIsland = ""
TabListData.fullyLoaded = false
- for (line in TabListData.getTabList()) {
- islandNamePattern.matchMatcher(line) {
- newIsland = group("island").removeColor()
- TabListData.fullyLoaded = true
- }
+ TabListData.getTabList().matchFirst(islandNamePattern) {
+ foundIsland = group("island").removeColor()
+ TabListData.fullyLoaded = true
}
// Can not use color coding, because of the color effect (§f§lSKYB§6§lL§e§lOCK§A§L GUEST)
val guesting = guestPattern.matches(ScoreboardData.objectiveTitle.removeColor())
- val islandType = getIslandType(newIsland, guesting)
+ val islandType = getIslandType(foundIsland, guesting)
if (skyBlockIsland != islandType) {
IslandChangeEvent(islandType, skyBlockIsland).postAndCatch()
if (islandType == IslandType.UNKNOWN) {
- ChatUtils.debug("Unknown island detected: '$newIsland'")
- loggerIslandChange.log("Unknown: '$newIsland'")
+ ChatUtils.debug("Unknown island detected: '$foundIsland'")
+ loggerIslandChange.log("Unknown: '$foundIsland'")
} else {
loggerIslandChange.log(islandType.name)
}
diff --git a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt
index 622683066..2e42bb002 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt
@@ -12,7 +12,7 @@ import at.hannibal2.skyhanni.events.TabListUpdateEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
-import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
+import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.TabListData
import at.hannibal2.skyhanni.utils.UtilsPatterns
import net.minecraftforge.fml.common.eventhandler.EventPriority
@@ -51,11 +51,9 @@ object ProfileStorageData {
fun onTabListUpdate(event: TabListUpdateEvent) {
if (!LorenzUtils.inSkyBlock) return
- for (line in event.tabList) {
- UtilsPatterns.tabListProfilePattern.matchMatcher(line) {
- noTabListTime = SimpleTimeMark.farPast()
- return
- }
+ event.tabList.matchFirst(UtilsPatterns.tabListProfilePattern) {
+ noTabListTime = SimpleTimeMark.farPast()
+ return
}
noTabListTime = SimpleTimeMark.now()
diff --git a/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
index f4266f800..ff8f4b5e4 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
@@ -1,12 +1,12 @@
package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.events.InventoryCloseEvent
-import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.PurseChangeCause
import at.hannibal2.skyhanni.events.PurseChangeEvent
+import at.hannibal2.skyhanni.events.ScoreboardChangeEvent
import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble
import at.hannibal2.skyhanni.utils.NumberUtil.million
-import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
+import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@@ -32,13 +32,11 @@ object PurseAPI {
}
@SubscribeEvent
- fun onTick(event: LorenzTickEvent) {
- for (line in ScoreboardData.sidebarLinesFormatted) {
- val newPurse = coinsPattern.matchMatcher(line) {
- group("coins").formatDouble()
- } ?: continue
+ fun onScoreboardChange(event: ScoreboardChangeEvent) {
+ event.newList.matchFirst(coinsPattern) {
+ val newPurse = group("coins").formatDouble()
val diff = newPurse - currentPurse
- if (diff == 0.0) continue
+ if (diff == 0.0) return
currentPurse = newPurse
PurseChangeEvent(diff, getCause(diff)).postAndCatch()