From e34960c5ef84807d7f610641e4ff6b9a58f4d714 Mon Sep 17 00:00:00 2001 From: Eric W <42985687+ericpretzel@users.noreply.github.com> Date: Tue, 29 Aug 2023 08:07:46 -0700 Subject: Sack change event (#403) Create SackChangeEvent #403 --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 1 + .../java/at/hannibal2/skyhanni/data/SackAPI.kt | 49 ++++++++++++++++++++++ .../hannibal2/skyhanni/events/SackChangeEvent.kt | 7 ++++ 3 files changed, 57 insertions(+) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/events/SackChangeEvent.kt diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index ee3244d4d..2c58985e8 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -179,6 +179,7 @@ class SkyHanniMod { loadModule(SlayerAPI) loadModule(PurseAPI()) loadModule(RiftAPI) + loadModule(SackAPI()) // features loadModule(BazaarOrderHelper()) diff --git a/src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt new file mode 100644 index 000000000..4efb30727 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt @@ -0,0 +1,49 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.SackChangeEvent +import at.hannibal2.skyhanni.utils.NEUInternalName +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + + +class SackAPI { + + data class SackChange(val delta: Int, val internalName: NEUInternalName, val sacks: List) + + private val sackChangeRegex = Regex("""([+-][\d,]+) (.+) \((.+)\)""") + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!event.message.removeColor().startsWith("[Sacks]")) return + + val sackChanges = ArrayList() + + val sackAddText = event.chatComponent.siblings.firstNotNullOfOrNull { sibling -> + sibling.chatStyle?.chatHoverEvent?.value?.formattedText?.removeColor()?.takeIf { + it.startsWith("Added") + } + } ?: "" + val sackRemoveText = event.chatComponent.siblings.firstNotNullOfOrNull { sibling -> + sibling.chatStyle?.chatHoverEvent?.value?.formattedText?.removeColor()?.takeIf { + it.startsWith("Removed") + } + } ?: "" + + val sackChangeText = sackAddText + sackRemoveText + if (sackChangeText.isEmpty()) return + + val otherItemsAdded = sackAddText.contains("other items") + val otherItemsRemoved = sackRemoveText.contains("other items") + + for (match in sackChangeRegex.findAll(sackChangeText)) { + val delta = match.groups[1]!!.value.replace(",", "").toInt() + val item = match.groups[2]!!.value + val sacks = match.groups[3]!!.value.split(", ") + + val internalName = NEUInternalName.fromItemName(item) + sackChanges.add(SackChange(delta, internalName, sacks)) + } + SackChangeEvent(sackChanges, otherItemsAdded, otherItemsRemoved).postAndCatch() + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/events/SackChangeEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/SackChangeEvent.kt new file mode 100644 index 000000000..797605e80 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/SackChangeEvent.kt @@ -0,0 +1,7 @@ +package at.hannibal2.skyhanni.events + +import at.hannibal2.skyhanni.data.SackAPI + +class SackChangeEvent(val sackChanges: List, + val otherItemsAdded: Boolean, + val otherItemsRemoved: Boolean) : LorenzEvent() -- cgit