18
18
19
19
package me .duncte123 .skybot .utils ;
20
20
21
- import com .vdurmont .emoji .EmojiParser ;
21
+ import net .fellbaum .jemoji .Emoji ;
22
+ import net .fellbaum .jemoji .EmojiManager ;
22
23
23
24
import java .util .ArrayList ;
24
25
import java .util .List ;
26
+ import java .util .Locale ;
27
+ import java .util .stream .Collectors ;
25
28
26
29
/**
27
30
* Adapted from https://gist.github.com/heyarny/71c246f2f7fa4d9d10904fb9d5b1fa1d
28
31
*/
29
- public class TwemojiParser extends EmojiParser {
32
+ public class TwemojiParser {
30
33
private static final String BASE_URL = "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/" ;
31
34
35
+ private TwemojiParser () {}
36
+
32
37
public static String parseOne (String text ) {
33
- final List <UnicodeCandidate > emojis = getUnicodeCandidates (stripVariants (text ));
38
+ final List <Emoji > emojis = EmojiManager . extractEmojisInOrder (stripVariants (text ));
34
39
35
40
if (!emojis .isEmpty ()) {
36
- final String iconId = grabTheRightIcon (emojis .get ( 0 ). getEmoji (). getUnicode ());
41
+ final String iconId = grabTheRightIcon (emojis .getFirst ());
37
42
38
43
return BASE_URL + iconId + ".png" ;
39
44
}
@@ -43,7 +48,7 @@ public static String parseOne(String text) {
43
48
44
49
// for future use
45
50
public static List <String > parseAll (String text ) {
46
- final List <UnicodeCandidate > emojis = getUnicodeCandidates (stripVariants (text ));
51
+ final List <Emoji > emojis = EmojiManager . extractEmojisInOrder (stripVariants (text ));
47
52
48
53
if (emojis .isEmpty ()) {
49
54
return null ;
@@ -52,8 +57,8 @@ public static List<String> parseAll(String text) {
52
57
final List <String > urls = new ArrayList <>();
53
58
54
59
// 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 );
57
62
final String iconUrl = BASE_URL + iconId + ".png" ;
58
63
59
64
urls .add (iconUrl );
@@ -62,39 +67,14 @@ public static List<String> parseAll(String text) {
62
67
return urls ;
63
68
}
64
69
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
-
92
70
public static String stripVariants (String rawText ) {
93
71
// if variant is present as \uFE0F
94
72
return rawText .indexOf ('\u200D' ) < 0 ? rawText .replace ("\uFE0F " , "" ) : rawText ;
95
73
}
96
74
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 ("-" ));
99
79
}
100
80
}
0 commit comments