Skip to content

Commit

Permalink
Internal Change: Remove String.matchRegex() function Usages (#641)
Browse files Browse the repository at this point in the history
Code cleanup and removed .matchRegex() #641
  • Loading branch information
walkerselby authored Dec 4, 2023
1 parent 66fc603 commit 1cf1f68
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand All @@ -38,6 +37,7 @@ class BingoNextStepHelper {
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()
private val crystalFoundPattern = " *§r§5§l✦ CRYSTAL FOUND §r§7\\(.§r§7/5§r§7\\)".toPattern()
private val rhysTaskName = "30x Enchanted Minerals (Redstone, Lapis Lazuli, Coal) (for Rhys)"

companion object {
Expand Down Expand Up @@ -136,7 +136,7 @@ class BingoNextStepHelper {

for (currentStep in currentSteps) {
if (currentStep is ObtainCrystalStep) {
if (event.message.matchRegex(" *§r§5§l✦ CRYSTAL FOUND §r§7\\(.§r§7/5§r§7\\)")) {
crystalFoundPattern.matchMatcher(event.message) {
nextMessageIsCrystal = true
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.baseMaxHealth
import at.hannibal2.skyhanni.utils.LorenzUtils.derpy
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.client.entity.EntityOtherPlayerMP
import net.minecraft.entity.Entity
Expand Down Expand Up @@ -71,6 +71,8 @@ class MobFinder {
//F5
private var floor5lividEntity: EntityOtherPlayerMP? = null
private var floor5lividEntitySpawnTime = 0L
private val correctLividPattern =
"§c\\[BOSS] (.*) Livid§r§f: Impossible! How did you figure out which one I was\\?!".toPattern()

//F6
private var floor6Giants = false
Expand Down Expand Up @@ -587,7 +589,7 @@ class MobFinder {
}
}

if (message.matchRegex("§c\\[BOSS] (.*) Livid§r§f: Impossible! How did you figure out which one I was\\?!")) {
correctLividPattern.matchMatcher(message) {
floor5lividEntity = null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.PlaySoundEvent
import at.hannibal2.skyhanni.events.ReceiveParticleEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import net.minecraft.client.Minecraft
import net.minecraft.client.entity.EntityOtherPlayerMP
import net.minecraft.entity.item.EntityArmorStand
Expand All @@ -20,6 +20,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
class DungeonCleanEnd {

private val config get() = SkyHanniMod.feature.dungeon.cleanEnd
private val catacombsPattern = "([ ]*)§r§c(The|Master Mode) Catacombs §r§8- §r§eFloor (.*)".toPattern()

private var bossDone = false
private var chestsSpawned = false
Expand All @@ -32,7 +33,7 @@ class DungeonCleanEnd {

val message = event.message

if (message.matchRegex("([ ]*)§r§c(The|Master Mode) Catacombs §r§8- §r§eFloor (.*)")) {
catacombsPattern.matchMatcher(message) {
chestsSpawned = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getBottleOfJyrreSeconds
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEdition
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object ItemDisplayOverlayFeatures {
// TODO USE SH-REPO
private val config get() = SkyHanniMod.feature.inventory

// TODO repo
private val rancherBootsSpeedCapPattern = "§7Current Speed Cap: §a(?<cap>.*)".toPattern()
private val petLevelPattern = "\\[Lvl (?<level>.*)] .*".toPattern()
private val masterSkullPattern = "(.*)Master Skull - Tier .".toPattern()
private val gardenVacuumPatterm = "§7Vacuum Bag: §6(?<amount>\\d*) Pests?".toPattern()
private val harvestPattern = "§7§7You may harvest §6(?<amount>.).*".toPattern()
private val dungeonPotionPattern = "Dungeon (?<level>.*) Potion".toPattern()
Expand All @@ -75,8 +76,10 @@ object ItemDisplayOverlayFeatures {
}
}

if (MASTER_SKULL_TIER.isSelected() && itemName.matchRegex("(.*)Master Skull - Tier .")) {
return itemName.substring(itemName.length - 1)
if (MASTER_SKULL_TIER.isSelected()) {
masterSkullPattern.matchMatcher(itemName) {
return itemName.substring(itemName.length - 1)
}
}

if (DUNGEON_HEAD_FLOOR_NUMBER.isSelected() && (itemName.contains("Golden ") || itemName.contains("Diamond "))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import at.hannibal2.skyhanni.utils.RenderUtils.drawString
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import at.hannibal2.skyhanni.utils.SpecialColour
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.TimeUtils
import at.hannibal2.skyhanni.utils.getLorenzVec
import at.hannibal2.skyhanni.utils.toLorenzVec
Expand Down Expand Up @@ -61,6 +61,7 @@ class MinionFeatures {
private var lastInventoryClosed = 0L
private var coinsPerDay = ""
private val minionUpgradePattern = "§aYou have upgraded your Minion to Tier (?<tier>.*)".toPattern()
private val minionCoinPattern = "§aYou received §r§6(.*) coins§r§a!".toPattern()

@SubscribeEvent
fun onPlayerInteract(event: PlayerInteractEvent) {
Expand Down Expand Up @@ -253,11 +254,10 @@ class MinionFeatures {
if (LorenzUtils.skyBlockIsland != IslandType.PRIVATE_ISLAND) return

val message = event.message
if (message.matchRegex("§aYou received §r§6(.*) coins§r§a!") && System.currentTimeMillis() - lastInventoryClosed < 2_000) {
if (minionCoinPattern.matches(message) && System.currentTimeMillis() - lastInventoryClosed < 2_000) {
minions?.get(lastMinion)?.let {
it.lastClicked = System.currentTimeMillis()
}

}
if (message.startsWith("§aYou picked up a minion!") && lastMinion != null) {
minions = minions?.editCopy { remove(lastMinion) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import at.hannibal2.skyhanni.features.combat.damageindicator.BossType
import at.hannibal2.skyhanni.features.combat.damageindicator.DamageIndicatorManager
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.TimeUtils
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class AshfangFreezeCooldown {
private val config get() = SkyHanniMod.feature.crimsonIsle.ashfang

// TODO USE SH-REPO
private val cryogenicBlastPattern = "§cAshfang Follower's Cryogenic Blast hit you for (.*) damage!".toPattern()

private var lastHit = 0L

@SubscribeEvent
fun onChatMessage(event: LorenzChatEvent) {
if (!isEnabled()) return

val message = event.message
if (message.matchRegex("§cAshfang Follower's Cryogenic Blast hit you for (.*) damage!")) {
cryogenicBlastPattern.matchMatcher(message) {
lastHit = System.currentTimeMillis()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.entity.EntityLivingBase
Expand All @@ -18,6 +18,7 @@ import kotlin.time.Duration.Companion.seconds

class SlayerQuestWarning {
private val config get() = SkyHanniMod.feature.slayer
private val talkToMaddoxPattern = " {3}§r§5§l» §r§7Talk to Maddox to claim your (.+) Slayer XP!".toPattern()
private var needSlayerQuest = false
private var lastWarning = 0L
private var currentReason = ""
Expand All @@ -43,7 +44,7 @@ class SlayerQuestWarning {
}

//no auto slayer
if (message.matchRegex(" {3}§r§5§l» §r§7Talk to Maddox to claim your (.+) Slayer XP!")) {
talkToMaddoxPattern.matchMatcher(message) {
needNewQuest("You have no Auto-Slayer active!")
}
if (message == " §r§a§lSLAYER QUEST COMPLETE!") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class BlazeSlayerDaggerHelper {
private val config get() = SkyHanniMod.feature.slayer.blazes.hellion
private val attunementPattern = "§cStrike using the §r(.+) §r§cattunement on your dagger!".toPattern()

private var clientSideClicked = false
private var textTop = ""
Expand All @@ -34,12 +35,10 @@ class BlazeSlayerDaggerHelper {
@SubscribeEvent
fun onChatMessage(event: LorenzChatEvent) {
if (!LorenzUtils.inSkyBlock) return
if (!SkyHanniMod.feature.slayer.blazes.hellion.hideDaggerWarning) return
if (!config.hideDaggerWarning) return

val message = event.message
if (message.matchRegex("§cStrike using the §r(.+) §r§cattunement on your dagger!") ||
message == "§cYour hit was reduced by Hellion Shield!"
) {
if (attunementPattern.matches(message) || message == "§cYour hit was reduced by Hellion Shield!") {
event.blockedReason = "blaze_slayer_dagger"
}
}
Expand All @@ -63,15 +62,15 @@ class BlazeSlayerDaggerHelper {
checkActiveDagger()
lastNearest = findNearest()

val first = Dagger.entries[SkyHanniMod.feature.slayer.blazes.hellion.firstDagger]
val first = Dagger.entries[config.firstDagger]
val second = first.other()

textTop = format(holding, true, first) + " " + format(holding, true, second)
textBottom = format(holding, false, first) + " " + format(holding, false, second)
}

private fun findNearest(): HellionShield? {
if (!SkyHanniMod.feature.slayer.blazes.hellion.markRightHellionShield) return null
if (!config.markRightHellionShield) return null

if (lastNearestCheck + 100 > System.currentTimeMillis()) return lastNearest
lastNearestCheck = System.currentTimeMillis()
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
Expand All @@ -25,6 +25,20 @@ import kotlin.time.Duration.Companion.seconds

object ItemUtils {

// TODO USE SH-REPO
private val patternInFront = "(?: *§8(\\\\w)?(?<amount>[\\d.km,]+)(x )?)?(?<name>.*)".toPattern()
private val patternBehind = "(?<name>(?:['\\w-]+ ?)+)(?:§8x(?<amount>[\\d,]+))?".toPattern()
private val petLevelPattern = "\\[Lvl (.*)] (.*)".toPattern()

private val ignoredPetStrings = listOf(
"Archer",
"Berserk",
"Mage",
"Tank",
"Healer",
"",
)

fun ItemStack.cleanName() = this.displayName.removeColor()

fun isSack(stack: ItemStack) = stack.getInternalName().endsWith("_SACK") && stack.cleanName().endsWith(" Sack")
Expand All @@ -51,14 +65,7 @@ object ItemUtils {

fun isRecombobulated(stack: ItemStack) = stack.isRecombobulated()

fun isPet(name: String): Boolean = name.matchRegex("\\[Lvl (.*)] (.*)") && !listOf(
"Archer",
"Berserk",
"Mage",
"Tank",
"Healer",
"",
).any { name.contains(it) }
fun isPet(name: String): Boolean = petLevelPattern.matches(name) && !ignoredPetStrings.any { name.contains(it) }

fun maxPetLevel(name: String) = if (name.contains("Golden Dragon")) 200 else 100

Expand Down Expand Up @@ -253,10 +260,6 @@ object ItemUtils {

fun isSkyBlockMenuItem(stack: ItemStack?): Boolean = stack?.getInternalName()?.equals("SKYBLOCK_MENU") ?: false

// TODO USE SH-REPO
private val patternInFront = "(?: *§8(\\+§[\\w])?(?<amount>[\\d.km,]+)(x )?)?(?<name>.*)".toPattern()
private val patternBehind = "(?<name>(?:['\\w-]+ ?)+)(?:§8x(?<amount>[\\d,]+))?".toPattern()

private val itemAmountCache = mutableMapOf<String, Pair<String, Int>>()

fun readItemAmount(originalInput: String): Pair<String, Int>? {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiUtilRenderComponents
import net.minecraft.util.ChatComponentText
import net.minecraft.util.IChatComponent
import org.intellij.lang.annotations.Language
import java.util.Base64
import java.util.NavigableMap
import java.util.UUID
Expand Down Expand Up @@ -77,9 +76,6 @@ object StringUtils {
return toString().replace("-", "")
}

@Deprecated("Do not create a regex pattern each time.", ReplaceWith("toPattern()"))
fun String.matchRegex(@Language("RegExp") regex: String): Boolean = regex.toRegex().matches(this)

private fun String.removeAtBeginning(text: String): String =
if (this.startsWith(text)) substring(text.length) else this

Expand Down

0 comments on commit 1cf1f68

Please sign in to comment.