From 18e96e6c3b867da06aabd29bcd83e92ffe58a00e Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Fri, 17 Nov 2023 11:52:51 +0100 Subject: Created PetAPI and deprecated String.matchRegex() --- .../skyhanni/features/misc/CurrentPetDisplay.kt | 49 +++++++++++----------- .../features/misc/discordrpc/DiscordStatus.kt | 4 +- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/features/misc') diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/CurrentPetDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/CurrentPetDisplay.kt index fe9d55176..7855a94b9 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/CurrentPetDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/CurrentPetDisplay.kt @@ -2,53 +2,53 @@ package at.hannibal2.skyhanni.features.misc import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator -import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.data.PetAPI import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.features.rift.RiftAPI import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.LorenzUtils.between import at.hannibal2.skyhanni.utils.RenderUtils.renderString import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher -import at.hannibal2.skyhanni.utils.StringUtils.matchRegex +import at.hannibal2.skyhanni.utils.StringUtils.matches import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class CurrentPetDisplay { + private val config get() = SkyHanniMod.feature.misc.pets // TODO USE SH-REPO private val inventoryNamePattern = "(?:\\(\\d+/\\d+\\))? Pets".toPattern() + private val chatSpawnPattern = "§aYou summoned your §r(?.*)§r§a!".toPattern() + private val chatDespawnPattern = "§aYou despawned your §r.*§r§a!".toPattern() + private val chatPetRulePattern = "§cAutopet §eequipped your §7\\[Lvl .*] (?.*)! §a§lVIEW RULE".toPattern() @SubscribeEvent fun onChatMessage(event: LorenzChatEvent) { - val message = event.message - val config = ProfileStorageData.profileSpecific ?: return - var blocked = false - if (message.matchRegex("§cAutopet §eequipped your §7(.*)§e! §a§lVIEW RULE")) { - config.currentPet = message.between("] ", "§e!") - blocked = true + findPetInChat(event.message)?.let { + PetAPI.currentPet = it + if (config.display) { + event.blockedReason = "pets" + } } + } - if (!LorenzUtils.inSkyBlock) return - - if (message.matchRegex("§aYou summoned your §r(.*)§r§a!")) { - config.currentPet = message.between("your §r", "§r§a") - blocked = true + private fun findPetInChat(message: String): String? { + chatSpawnPattern.matchMatcher(message) { + return group("pet") } - if (message.matchRegex("§aYou despawned your §r(.*)§r§a!")) { - config.currentPet = "" - blocked = true + if (chatDespawnPattern.matches(message)) { + return "" } - - if (blocked && SkyHanniMod.feature.misc.pets.display) { - event.blockedReason = "pets" + chatPetRulePattern.matchMatcher(message) { + return group("pet") } + + return null } @SubscribeEvent fun onInventoryOpen(event: InventoryFullyOpenedEvent) { - val storage = ProfileStorageData.profileSpecific ?: return if (!inventoryNamePattern.matcher(event.inventoryName).matches()) return val lore = event.inventoryItems[4]?.getLore() ?: return @@ -56,7 +56,7 @@ class CurrentPetDisplay { for (line in lore) { selectedPetPattern.matchMatcher(line) { val newPet = group("pet") - storage.currentPet = if (newPet != "§cNone") newPet else "" + PetAPI.currentPet = if (newPet != "§cNone") newPet else "" } } } @@ -66,10 +66,9 @@ class CurrentPetDisplay { if (!LorenzUtils.inSkyBlock) return if (RiftAPI.inRift()) return - if (!SkyHanniMod.feature.misc.pets.display) return - val storage = ProfileStorageData.profileSpecific ?: return + if (!config.display) return - SkyHanniMod.feature.misc.pets.displayPos.renderString(storage.currentPet, posLabel = "Current Pet") + config.displayPos.renderString(PetAPI.currentPet, posLabel = "Current Pet") } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/discordrpc/DiscordStatus.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/discordrpc/DiscordStatus.kt index 9937ff288..e95132ff7 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/discordrpc/DiscordStatus.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/discordrpc/DiscordStatus.kt @@ -10,7 +10,7 @@ import at.hannibal2.skyhanni.data.GardenCropMilestones.isMaxed import at.hannibal2.skyhanni.data.GardenCropMilestones.progressToNextLevel import at.hannibal2.skyhanni.data.HypixelData import at.hannibal2.skyhanni.data.IslandType -import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.data.PetAPI import at.hannibal2.skyhanni.data.ScoreboardData import at.hannibal2.skyhanni.features.dungeon.DungeonAPI import at.hannibal2.skyhanni.features.garden.GardenAPI.getCropType @@ -289,7 +289,7 @@ enum class DiscordStatus(private val displayMessageSupplier: Supplier?) }), PETS({ - ProfileStorageData.profileSpecific?.currentPet?.let { + PetAPI.currentPet?.let { val colorCode = it.substring(1..2).first() val petName = it.substring(2) val petLevel = getCurrentPet()?.petLevel?.currentLevel ?: "?" -- cgit