diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt index 256be60..9864a11 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt @@ -16,7 +16,7 @@ import java.util.* val spaceRegex: Regex = "(? val colorable = colorableRegex in match.value - val bitmapIndex = bitmapIndexRegex.find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1 - - content = content.replaceFirst( - emote.baseRegex, emote.formattedComponent( - insert = insert, - colorable = colorable, - bitmapIndex = bitmapIndex - ).serialize() - ) + val bitmapMatch = bitmapIndexRegex.find(match.value) + val startIndex = bitmapMatch?.groupValues?.get(1)?.toIntOrNull() ?: -1 + val endIndex = bitmapMatch?.groupValues?.get(2)?.toIntOrNull()?.coerceAtLeast(startIndex) ?: startIndex + + if (startIndex != -1) content = content.replace(match.value, (startIndex..endIndex).joinToString(":space_-1:") { index -> + ":${emote.id}|$index${if (colorable) "|c" else ""}:" + }) + + for (bitmapIndex in startIndex..endIndex) { + content = content.replaceFirst( + emote.baseRegex, emote.formattedComponent( + insert = insert, + colorable = colorable, + bitmapIndex = bitmapIndex + ).serialize() + ) + } } for (gif in emojy.gifs) gif.baseRegex.findAll(this).forEach { _ -> @@ -89,9 +97,19 @@ fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false): for (emote in emojy.emotes) emote.baseRegex.findAll(serialized).forEach { match -> val colorable = colorableRegex in match.value - val bitmapIndex = bitmapIndexRegex.find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1 - - component = component.replaceText(emote.replacementConfig(false, insert, colorable, bitmapIndex)) + val bitmapMatch = bitmapIndexRegex.find(match.value) + val startIndex = bitmapMatch?.groupValues?.get(1)?.toIntOrNull() ?: -1 + val endIndex = bitmapMatch?.groupValues?.get(2)?.toIntOrNull()?.coerceAtLeast(startIndex) ?: startIndex + + if (startIndex != -1) component = component.replaceText { + it.matchLiteral(match.value).replacement((startIndex..endIndex).joinToString(":space_-1:") { index -> + ":${emote.id}|$index${if (colorable) "|c" else ""}:" + }) + } + + for (bitmapIndex in startIndex..endIndex) { + component = component.replaceText(emote.replacementConfig(false, insert, colorable, bitmapIndex)) + } } for (gif in emojy.gifs) gif.baseRegex.findAll(serialized).forEach { _ -> diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/config/Emote.kt b/core/src/main/kotlin/com/mineinabyss/emojy/config/Emote.kt index 7cc090a..f737dcb 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/config/Emote.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/config/Emote.kt @@ -40,7 +40,7 @@ data class Emote( @Transient val isMultiBitmap = bitmapWidth > 1 || bitmapHeight > 1 @Transient - val baseRegex = "(?