From cc291932906c38943e20cee29ee591695a9875b5 Mon Sep 17 00:00:00 2001 From: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:13:52 +0200 Subject: Backend: Usage of TabWidget (#1240) Co-authored-by: Cal Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/data/HypixelData.kt | 22 +++++++------------ .../hannibal2/skyhanni/data/ProfileStorageData.kt | 17 +++++---------- .../hannibal2/skyhanni/events/WidgetUpdateEvent.kt | 2 ++ .../skyhanni/features/combat/FerocityDisplay.kt | 11 ++++++---- .../features/garden/GardenCropMilestoneFix.kt | 8 ++++--- .../features/garden/composter/ComposterDisplay.kt | 11 +++++----- .../features/garden/visitor/VisitorListener.kt | 12 ++++++----- .../features/nether/VolcanoExplosivityDisplay.kt | 18 +++++++++++----- .../dailyquest/DailyQuestHelper.kt | 15 +++++++------ .../reputationhelper/dailyquest/QuestLoader.kt | 3 ++- .../skyhanni/test/garden/VisitorListenerTest.kt | 25 +++++++++++++--------- 11 files changed, 78 insertions(+), 66 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt b/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt index 7599cc80f..82bd6d315 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt @@ -11,7 +11,6 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent import at.hannibal2.skyhanni.events.ScoreboardUpdateEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.events.minecraft.ClientDisconnectEvent import at.hannibal2.skyhanni.features.bingo.BingoAPI @@ -59,10 +58,6 @@ object HypixelData { "serverid.scoreboard", "§7\\d+/\\d+/\\d+ §8(?[mM])(?\\S+).*", ) - private val serverIdTablistPattern by patternGroup.pattern( - "serverid.tablist", - " Server: §r§8(?\\S+)", - ) private val lobbyTypePattern by patternGroup.pattern( "lobbytype", "(?.*lobby)\\d+", @@ -161,14 +156,14 @@ object HypixelData { if (LorenzUtils.lastWorldSwitch.passedSince() < 1.seconds) return if (!TabListData.fullyLoaded) return - ScoreboardData.sidebarLinesFormatted.matchFirst(serverIdScoreboardPattern) { - val serverType = if (group("servertype") == "M") "mega" else "mini" - serverId = "$serverType${group("serverid")}" + TabWidget.SERVER.matchMatcherFirstLine { + serverId = group("serverid") return } - TabListData.getTabList().matchFirst(serverIdTablistPattern) { - serverId = group("serverid") + ScoreboardData.sidebarLinesFormatted.matchFirst(serverIdScoreboardPattern) { + val serverType = if (group("servertype") == "M") "mega" else "mini" + serverId = "$serverType${group("serverid")}" return } @@ -301,11 +296,9 @@ object HypixelData { } } - @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { - event.tabList.matchFirst(UtilsPatterns.tabListProfilePattern) { + private fun checkProfile() { + TabWidget.PROFILE.matchMatcherFirstLine { 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 @@ -374,6 +367,7 @@ object HypixelData { fun onTabListUpdate(event: WidgetUpdateEvent) { when (event.widget) { TabWidget.AREA -> checkIsland(event) + TabWidget.PROFILE -> checkProfile() else -> Unit } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt index 3d9bdcf78..18211ccd8 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt @@ -4,21 +4,20 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.SackData import at.hannibal2.skyhanni.config.storage.PlayerSpecificStorage import at.hannibal2.skyhanni.config.storage.ProfileSpecificStorage +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.HypixelJoinEvent import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.DelayedRun import at.hannibal2.skyhanni.utils.HypixelCommands import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.TabListData -import at.hannibal2.skyhanni.utils.UtilsPatterns import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.time.Duration.Companion.seconds @@ -75,15 +74,9 @@ object ProfileStorageData { } @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { - if (!LorenzUtils.inSkyBlock) return - - event.tabList.matchFirst(UtilsPatterns.tabListProfilePattern) { - noTabListTime = SimpleTimeMark.farPast() - return - } - - noTabListTime = SimpleTimeMark.now() + fun onTabListUpdate(event: WidgetUpdateEvent) { + if (!event.isWidget(TabWidget.PROFILE)) return + noTabListTime = if (event.isClear()) SimpleTimeMark.now() else SimpleTimeMark.farPast() } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/events/WidgetUpdateEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/WidgetUpdateEvent.kt index 60300628f..a91e7dca3 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/WidgetUpdateEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/WidgetUpdateEvent.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.events import at.hannibal2.skyhanni.data.model.TabWidget +import at.hannibal2.skyhanni.utils.LorenzUtils.isAnyOf /** The events get send on change of the widget and on island switch */ open class WidgetUpdateEvent( @@ -9,6 +10,7 @@ open class WidgetUpdateEvent( ) : LorenzEvent() { fun isWidget(widgetType: TabWidget) = widget == widgetType + fun isWidget(vararg widgetType: TabWidget) = widget.isAnyOf(*widgetType) fun isClear() = lines.isEmpty() } diff --git a/src/main/java/at/hannibal2/skyhanni/features/combat/FerocityDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/combat/FerocityDisplay.kt index 31b85ddd0..c30299e4f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/combat/FerocityDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/combat/FerocityDisplay.kt @@ -1,8 +1,9 @@ package at.hannibal2.skyhanni.features.combat import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.GuiRenderEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst @@ -20,16 +21,18 @@ object FerocityDisplay { */ private val ferocityPattern by RepoPattern.pattern( "combat.ferocity.tab", - " Ferocity: §r§c⫽(?.*)" + " Ferocity: §r§c⫽(?.*)", ) private var display = "" @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { + fun onTabListUpdate(event: WidgetUpdateEvent) { if (!isEnabled()) return + if (!event.isWidget(TabWidget.STATS, TabWidget.DUNGEON_SKILLS_AND_STATS)) return display = "" - val stat = event.tabList.matchFirst(ferocityPattern) { + if (event.isClear()) return + val stat = event.lines.matchFirst(ferocityPattern) { group("stat") } ?: return 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 b154a72e0..127ee3f30 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneFix.kt @@ -4,8 +4,9 @@ import at.hannibal2.skyhanni.data.GardenCropMilestones import at.hannibal2.skyhanni.data.GardenCropMilestones.getCounter import at.hannibal2.skyhanni.data.GardenCropMilestones.setCounter import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.LorenzChatEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.features.garden.farming.GardenCropMilestoneDisplay import at.hannibal2.skyhanni.features.garden.pests.PestAPI import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -86,8 +87,9 @@ object GardenCropMilestoneFix { } @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { - event.tabList.matchFirst(tabListPattern) { + fun onTabListUpdate(event: WidgetUpdateEvent) { + if (!event.isWidget(TabWidget.CROP_MILESTONE)) return + event.lines.matchFirst(tabListPattern) { val tier = group("tier").toInt() val percentage = group("percentage").toDouble() val cropName = group("crop") 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 bd4c8274c..7c88acfca 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 @@ -3,8 +3,9 @@ package at.hannibal2.skyhanni.features.garden.composter import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.config.enums.OutsideSbFeature import at.hannibal2.skyhanni.data.IslandType +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.GuiRenderEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.features.fame.ReminderUtils import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -53,10 +54,11 @@ object ComposterDisplay { } @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { + fun onTabListUpdate(event: WidgetUpdateEvent) { if (!(config.displayEnabled && GardenAPI.inGarden())) return + if (!event.isWidget(TabWidget.COMPOSTER)) return - readData(event.tabList) + readData(event.lines) if (tabListData.isNotEmpty()) { composterEmptyTime = ComposterAPI.estimateEmptyTimeFromTab() @@ -137,8 +139,7 @@ object ComposterDisplay { storage.informedAboutLowMatter = 5.0.minutes.fromNow() } - if (ComposterAPI.getFuel() <= config.notifyLow.fuel && storage.informedAboutLowFuel.isInPast() - ) { + if (ComposterAPI.getFuel() <= config.notifyLow.fuel && storage.informedAboutLowFuel.isInPast()) { if (config.notifyLow.title) { LorenzUtils.sendTitle("§cYour Fuel is low", 4.seconds) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorListener.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorListener.kt index 2914aa1c3..66d03745f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorListener.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorListener.kt @@ -3,13 +3,14 @@ package at.hannibal2.skyhanni.features.garden.visitor import at.hannibal2.skyhanni.api.event.HandleEvent import at.hannibal2.skyhanni.config.features.garden.visitor.VisitorConfig import at.hannibal2.skyhanni.data.IslandType +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.CheckRenderEntityEvent import at.hannibal2.skyhanni.events.GuiKeyPressEvent import at.hannibal2.skyhanni.events.InventoryCloseEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.events.garden.visitor.VisitorOpenEvent import at.hannibal2.skyhanni.events.garden.visitor.VisitorRenderEvent import at.hannibal2.skyhanni.events.item.ItemHoverEvent @@ -42,7 +43,7 @@ import kotlin.time.Duration.Companion.seconds object VisitorListener { private val offersAcceptedPattern by RepoPattern.pattern( "garden.visitor.offersaccepted", - "§7Offers Accepted: §a(?\\d+)" + "§7Offers Accepted: §a(?\\d+)", ) private val config get() = VisitorAPI.config @@ -68,13 +69,14 @@ object VisitorListener { } @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { + fun onTabListUpdate(event: WidgetUpdateEvent) { if (!GardenAPI.inGarden()) return + if (!event.isWidget(TabWidget.VISITORS)) return - val hasVisitorInfo = event.tabList.any { VisitorAPI.visitorCountPattern.matches(it) } + val hasVisitorInfo = event.lines.any { VisitorAPI.visitorCountPattern.matches(it) } if (!hasVisitorInfo) return - val visitorsInTab = VisitorAPI.visitorsInTabList(event.tabList) + val visitorsInTab = VisitorAPI.visitorsInTabList(event.lines) if (LorenzUtils.lastWorldSwitch.passedSince() > 2.seconds) { for (visitor in VisitorAPI.getVisitors()) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/nether/VolcanoExplosivityDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/nether/VolcanoExplosivityDisplay.kt index 6cb7f3bf4..441dac11b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/nether/VolcanoExplosivityDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/nether/VolcanoExplosivityDisplay.kt @@ -2,11 +2,12 @@ package at.hannibal2.skyhanni.features.nether import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.data.IslandType +import at.hannibal2.skyhanni.data.model.TabWidget import at.hannibal2.skyhanni.events.GuiRenderEvent -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland -import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst +import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RenderUtils.renderString import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -22,14 +23,21 @@ object VolcanoExplosivityDisplay { */ private val statusPattern by patternGroup.pattern( "tablistline", - " *Volcano: (?(?:§.)*\\S+)" + " *Volcano: (?(?:§.)*\\S+)", ) private var display = "" @SubscribeEvent - fun onTabListUpdate(event: TabListUpdateEvent) { + fun onTabListUpdate(event: WidgetUpdateEvent) { if (!isEnabled()) return - event.tabList.matchFirst(statusPattern) { + if (!event.isWidget(TabWidget.VOLCANO)) return + + if (event.isClear()) { + display = "" + return + } + // TODO merge widget pattern with statusPattern + statusPattern.matchMatcher(event.lines.first()) { display = "§bVolcano Explosivity§7: ${group("status")}" } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/DailyQuestHelper.kt b/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/DailyQuestHelper.kt index d0b37ffcd..ef7423990 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/DailyQuestHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/DailyQuestHelper.kt @@ -65,7 +65,7 @@ class DailyQuestHelper(val reputationHelper: CrimsonIsleReputationHelper) { */ val minibossAmountPattern by RepoPattern.pattern( "crimson.reputationhelper.quest.minibossamount", - "(?:§7Kill the §c.+ §7|.*)miniboss §a(?\\d) §7times?!" + "(?:§7Kill the §c.+ §7|.*)miniboss §a(?\\d) §7times?!", ) private val config get() = SkyHanniMod.feature.crimsonIsle.reputationHelper @@ -87,11 +87,11 @@ class DailyQuestHelper(val reputationHelper: CrimsonIsleReputationHelper) { } @SubscribeEvent - fun onTabListWidgetUpdate(event: WidgetUpdateEvent) { - if (event.isWidget(TabWidget.FACTION_QUESTS)) { - if (!isEnabled()) return - questLoader.loadFromTabList() - } + fun onTabListUpdate(event: WidgetUpdateEvent) { + if (!event.isWidget(TabWidget.FACTION_QUESTS)) return + if (!isEnabled()) return + + questLoader.loadFromTabList() } @SubscribeEvent @@ -211,7 +211,8 @@ class DailyQuestHelper(val reputationHelper: CrimsonIsleReputationHelper) { } private fun Quest.needsTownBoardLocation(): Boolean = state.let { state -> - state == QuestState.READY_TO_COLLECT || state == QuestState.NOT_ACCEPTED || + state == QuestState.READY_TO_COLLECT || + state == QuestState.NOT_ACCEPTED || (this is RescueMissionQuest && state == QuestState.ACCEPTED) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/QuestLoader.kt b/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/QuestLoader.kt index 694ebed35..686f46827 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/QuestLoader.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/QuestLoader.kt @@ -214,7 +214,8 @@ class QuestLoader(private val dailyQuestHelper: DailyQuestHelper) { quest.haveAmount = haveAmount } catch (e: IndexOutOfBoundsException) { ErrorManager.logErrorWithData( - e, "Error loading Crimson Isle Quests from config.", + e, + "Error loading Crimson Isle Quests from config.", "text" to text, ) } diff --git a/src/test/java/at/hannibal2/skyhanni/test/garden/VisitorListenerTest.kt b/src/test/java/at/hannibal2/skyhanni/test/garden/VisitorListenerTest.kt index 78304f0ae..d9c31bbff 100644 --- a/src/test/java/at/hannibal2/skyhanni/test/garden/VisitorListenerTest.kt +++ b/src/test/java/at/hannibal2/skyhanni/test/garden/VisitorListenerTest.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.test.garden -import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.data.model.TabWidget +import at.hannibal2.skyhanni.events.WidgetUpdateEvent import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.features.garden.visitor.VisitorAPI import at.hannibal2.skyhanni.features.garden.visitor.VisitorListener @@ -33,7 +34,7 @@ class VisitorListenerTest { @Test fun `onTablistUpdate it should add new visitors to the list`() { listener.onTabListUpdate( - TabListUpdateEvent( + fakeTabWidget( mutableListOf( "§b§lVisitors: §r§f(3)", " §r§cSpaceman", @@ -41,8 +42,8 @@ class VisitorListenerTest { " §r§fJacob", "ThePlayerName", "", - ) - ) + ), + ), ) verify { VisitorAPI.addVisitor("§fJacob") } @@ -57,9 +58,9 @@ class VisitorListenerTest { ) listener.onTabListUpdate( - TabListUpdateEvent( - mutableListOf("§b§lVisitors: §r§f(0)", "") - ) + fakeTabWidget( + mutableListOf("§b§lVisitors: §r§f(0)", ""), + ), ) verify { VisitorAPI.removeVisitor("§fJacob") } @@ -74,11 +75,15 @@ class VisitorListenerTest { every { LorenzUtils.lastWorldSwitch } returns SimpleTimeMark.now() listener.onTabListUpdate( - TabListUpdateEvent( - mutableListOf("§b§lVisitors: §r§f(0)", "") - ) + fakeTabWidget( + mutableListOf("§b§lVisitors: §r§f(0)", ""), + ), ) verify(exactly = 0) { VisitorAPI.removeVisitor("§fJacob") } } + + private fun fakeTabWidget(lines: List): WidgetUpdateEvent { + return WidgetUpdateEvent(TabWidget.VISITORS, lines) + } } -- cgit