Skip to content

Commit 4f9340c

Browse files
committed
fix shortcode emojis applying to server-sent unicode
Fixes #42
1 parent 221b81f commit 4f9340c

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/main/kotlin/org/polyfrost/polyplus/client/emoji/EmojiRegistry.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ object EmojiRegistry {
2121

2222
private val aliasesSorted: List<String> by lazy { shortcodes.keys.sorted() }
2323

24+
private val EMOJI_SHORTCODE: Pattern by lazy { Pattern.compile(SHORTCODE) }
25+
2426
private val EMOJI: Pattern by lazy {
2527
val alts = StringBuilder(SHORTCODE)
2628
for (seq in unicode.keys) alts.append('|').append(Pattern.quote(seq))
@@ -99,7 +101,7 @@ object EmojiRegistry {
99101
fun transform(component: Component): Component {
100102
val contents = component.contents
101103
val expandedSelf: MutableComponent? = when (contents) {
102-
is PlainTextContents -> expand(contents.text(), component.style)
104+
is PlainTextContents -> expand(contents.text(), component.style, EMOJI_SHORTCODE)
103105
is TranslatableContents -> transformTranslatable(contents, component.style)
104106
else -> null
105107
}
@@ -136,7 +138,7 @@ object EmojiRegistry {
136138
newArgs[i] = t
137139
}
138140
is String -> {
139-
val expanded = expand(arg, style)
141+
val expanded = expand(arg, style, EMOJI_SHORTCODE)
140142
if (expanded != null) { changed = true; newArgs[i] = expanded } else newArgs[i] = arg
141143
}
142144
else -> newArgs[i] = arg
@@ -148,8 +150,8 @@ object EmojiRegistry {
148150
return MutableComponent.create(rebuilt).setStyle(style)
149151
}
150152

151-
private fun expand(text: String, style: Style): MutableComponent? {
152-
val matcher = EMOJI.matcher(text)
153+
private fun expand(text: String, style: Style, pattern: Pattern): MutableComponent? {
154+
val matcher = pattern.matcher(text)
153155
var root: MutableComponent? = null
154156
var last = 0
155157
while (matcher.find()) {
@@ -168,7 +170,7 @@ object EmojiRegistry {
168170

169171
@JvmStatic
170172
fun styleInput(text: String, base: Style): net.minecraft.util.FormattedCharSequence? =
171-
expand(text, base)?.visualOrderText
173+
expand(text, base, EMOJI)?.visualOrderText
172174

173175
@JvmStatic
174176
fun toShortcodes(text: String): String {

0 commit comments

Comments
 (0)