Skip to content

Commit

Permalink
fix: cache regex instead of recreating every time
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 9, 2023
1 parent 6591a5f commit 4b61745
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import org.bukkit.Bukkit
import org.bukkit.entity.Player
import java.util.*

private val colorableRegex: Regex = "\\|(c|colorable)".toRegex()
private val bitmapIndexRegex: Regex = "\\|([0-9]+)".toRegex()
//TODO Tags like rainbow and gradient, which split the text into multiple children, will break replacement below
// Above is due to Adventure-issue, nothing on our end for once. https://github.com/KyoriPowered/adventure/issues/872
// Find out why this is called 3 times
fun Component.replaceEmoteIds(player: Player? = null, insert: Boolean = true): Component {
var msg = GlobalTranslator.render(this, player?.locale() ?: Locale.US)
val serialized = msg.serialize()

emojy.emotes.filter { ":${it.id}(\\|.*?)?:".toRegex() in serialized && it.checkPermission(player) }.forEach { emote ->
val matches = ":${emote.id}(\\|(c|colorable|\\d+))*:".toRegex().findAll(serialized)
emojy.emotes.filter { it.baseRegex in serialized && it.checkPermission(player) }.forEach { emote ->
val matches = emote.fullRegex.findAll(serialized)
matches.forEach { match ->
val colorable = "\\|(c|colorable)".toRegex() in match.value
val bitmapIndex = "\\|([0-9]+)".toRegex().find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1
val colorable = colorableRegex in match.value
val bitmapIndex = bitmapIndexRegex.find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1

val replacement = emote.formattedUnicode(insert = insert, colorable = colorable, bitmapIndex = bitmapIndex)
msg = msg.replaceText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ data class Emotes(val emotes: Set<Emote> = mutableSetOf()) {
@EncodeDefault(NEVER) val bitmapHeight: Int = template?.bitmapHeight ?: 1,
) {
val isMultiBitmap: Boolean get() = bitmapWidth > 1 || bitmapHeight > 1
@Transient val baseRegex = ":$id(\\|.*?)?:".toRegex()
@Transient val fullRegex = "::$id(\\|(c|colorable|\\d+))*:".toRegex()

// Beginning of Private Use Area \uE000 -> uF8FF
// Option: (Character.toCodePoint('\uE000', '\uFF8F')/37 + getIndex())
Expand Down

0 comments on commit 4b61745

Please sign in to comment.