1818
1919package me .duncte123 .skybot .utils ;
2020
21- import com .vdurmont .emoji .EmojiParser ;
21+ import net .fellbaum .jemoji .Emoji ;
22+ import net .fellbaum .jemoji .EmojiManager ;
2223
2324import java .util .ArrayList ;
2425import java .util .List ;
26+ import java .util .Locale ;
27+ import java .util .stream .Collectors ;
2528
2629/**
2730 * Adapted from https://gist.github.com/heyarny/71c246f2f7fa4d9d10904fb9d5b1fa1d
2831 */
29- public class TwemojiParser extends EmojiParser {
32+ public class TwemojiParser {
3033 private static final String BASE_URL = "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/" ;
3134
35+ private TwemojiParser () {}
36+
3237 public static String parseOne (String text ) {
33- final List <UnicodeCandidate > emojis = getUnicodeCandidates (stripVariants (text ));
38+ final List <Emoji > emojis = EmojiManager . extractEmojisInOrder (stripVariants (text ));
3439
3540 if (!emojis .isEmpty ()) {
36- final String iconId = grabTheRightIcon (emojis .get ( 0 ). getEmoji (). getUnicode ());
41+ final String iconId = grabTheRightIcon (emojis .getFirst ());
3742
3843 return BASE_URL + iconId + ".png" ;
3944 }
@@ -43,7 +48,7 @@ public static String parseOne(String text) {
4348
4449 // for future use
4550 public static List <String > parseAll (String text ) {
46- final List <UnicodeCandidate > emojis = getUnicodeCandidates (stripVariants (text ));
51+ final List <Emoji > emojis = EmojiManager . extractEmojisInOrder (stripVariants (text ));
4752
4853 if (emojis .isEmpty ()) {
4954 return null ;
@@ -52,8 +57,8 @@ public static List<String> parseAll(String text) {
5257 final List <String > urls = new ArrayList <>();
5358
5459 // Kinda copied from EmojiParser but it does not have the variants on it
55- for (final UnicodeCandidate emoji : emojis ) {
56- final String iconId = grabTheRightIcon (emoji . getEmoji (). getUnicode () );
60+ for (final Emoji emoji : emojis ) {
61+ final String iconId = grabTheRightIcon (emoji );
5762 final String iconUrl = BASE_URL + iconId + ".png" ;
5863
5964 urls .add (iconUrl );
@@ -62,39 +67,14 @@ public static List<String> parseAll(String text) {
6267 return urls ;
6368 }
6469
65- private static String toCodePoint (String unicodeSurrogates ) {
66- final List <String > codes = new ArrayList <>();
67-
68- int charAt ;
69- int someValue = 0 ; // what is for?
70- int index = 0 ;
71-
72- while (index < unicodeSurrogates .length ()) {
73- charAt = unicodeSurrogates .charAt (index ++);
74-
75- if (someValue == 0 ) {
76- if (0xD800 <= charAt && charAt <= 0xDBFF ) {
77- someValue = charAt ;
78- } else {
79- codes .add (Integer .toString (charAt , 16 ));
80- }
81- } else {
82- final int calculation = 0x10000 + ((someValue - 0xD800 ) << 10 ) + (charAt - 0xDC00 );
83-
84- codes .add (Integer .toString (calculation , 16 ));
85- someValue = 0 ;
86- }
87- }
88-
89- return String .join ("-" , codes );
90- }
91-
9270 public static String stripVariants (String rawText ) {
9371 // if variant is present as \uFE0F
9472 return rawText .indexOf ('\u200D' ) < 0 ? rawText .replace ("\uFE0F " , "" ) : rawText ;
9573 }
9674
97- private static String grabTheRightIcon (String rawText ) {
98- return toCodePoint (stripVariants (rawText ));
75+ private static String grabTheRightIcon (Emoji emoji ) {
76+ return emoji .getEmoji ().codePoints ().mapToObj (
77+ operand -> Integer .toHexString (operand ).toLowerCase (Locale .ROOT )
78+ ).collect (Collectors .joining ("-" ));
9979 }
10080}
0 commit comments