diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt | 49 |
1 files changed, 49 insertions, 0 deletions
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<String>) + + private val sackChangeRegex = Regex("""([+-][\d,]+) (.+) \((.+)\)""") + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!event.message.removeColor().startsWith("[Sacks]")) return + + val sackChanges = ArrayList<SackChange>() + + 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() + } +} |