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 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") val sackChanges = ArrayList() 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() } }