From 2e2282fb6d6b3d6d0e08ce9e8d09722759aec74e Mon Sep 17 00:00:00 2001 From: duncte123 Date: Sat, 2 Nov 2024 09:50:30 +0100 Subject: [PATCH] Switch emoji libs --- .../duncte123/skybot/utils/TwemojiParser.java | 52 ++++++------------- settings.gradle.kts | 5 +- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/bot/src/main/java/me/duncte123/skybot/utils/TwemojiParser.java b/bot/src/main/java/me/duncte123/skybot/utils/TwemojiParser.java index abedead26..1efbad3fe 100644 --- a/bot/src/main/java/me/duncte123/skybot/utils/TwemojiParser.java +++ b/bot/src/main/java/me/duncte123/skybot/utils/TwemojiParser.java @@ -18,22 +18,27 @@ package me.duncte123.skybot.utils; -import com.vdurmont.emoji.EmojiParser; +import net.fellbaum.jemoji.Emoji; +import net.fellbaum.jemoji.EmojiManager; import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; /** * Adapted from https://gist.github.com/heyarny/71c246f2f7fa4d9d10904fb9d5b1fa1d */ -public class TwemojiParser extends EmojiParser { +public class TwemojiParser { private static final String BASE_URL = "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/"; + private TwemojiParser() {} + public static String parseOne(String text) { - final List emojis = getUnicodeCandidates(stripVariants(text)); + final List emojis = EmojiManager.extractEmojisInOrder(stripVariants(text)); if (!emojis.isEmpty()) { - final String iconId = grabTheRightIcon(emojis.get(0).getEmoji().getUnicode()); + final String iconId = grabTheRightIcon(emojis.getFirst()); return BASE_URL + iconId + ".png"; } @@ -43,7 +48,7 @@ public static String parseOne(String text) { // for future use public static List parseAll(String text) { - final List emojis = getUnicodeCandidates(stripVariants(text)); + final List emojis = EmojiManager.extractEmojisInOrder(stripVariants(text)); if (emojis.isEmpty()) { return null; @@ -52,8 +57,8 @@ public static List parseAll(String text) { final List urls = new ArrayList<>(); // Kinda copied from EmojiParser but it does not have the variants on it - for (final UnicodeCandidate emoji : emojis) { - final String iconId = grabTheRightIcon(emoji.getEmoji().getUnicode()); + for (final Emoji emoji : emojis) { + final String iconId = grabTheRightIcon(emoji); final String iconUrl = BASE_URL + iconId + ".png"; urls.add(iconUrl); @@ -62,39 +67,14 @@ public static List parseAll(String text) { return urls; } - private static String toCodePoint(String unicodeSurrogates) { - final List codes = new ArrayList<>(); - - int charAt; - int someValue = 0; // what is for? - int index = 0; - - while (index < unicodeSurrogates.length()) { - charAt = unicodeSurrogates.charAt(index++); - - if (someValue == 0) { - if (0xD800 <= charAt && charAt <= 0xDBFF) { - someValue = charAt; - } else { - codes.add(Integer.toString(charAt, 16)); - } - } else { - final int calculation = 0x10000 + ((someValue - 0xD800) << 10) + (charAt - 0xDC00); - - codes.add(Integer.toString(calculation, 16)); - someValue = 0; - } - } - - return String.join("-", codes); - } - public static String stripVariants(String rawText) { // if variant is present as \uFE0F return rawText.indexOf('\u200D') < 0 ? rawText.replace("\uFE0F", "") : rawText; } - private static String grabTheRightIcon(String rawText) { - return toCodePoint(stripVariants(rawText)); + private static String grabTheRightIcon(Emoji emoji) { + return emoji.getEmoji().codePoints().mapToObj( + operand -> Integer.toHexString(operand).toLowerCase(Locale.ROOT) + ).collect(Collectors.joining("-")); } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 289b47867..e985b194f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -72,10 +72,11 @@ fun VersionCatalogBuilder.bot() { library("jagTag", "com.github.jagrosh", "JagTag").version("6dbe1ba") library("wolfram-alpha", "com.github.DuncteBot", "wolfram-alpha-java-binding").version("5c123ae") library("duration-parser", "me.duncte123", "durationParser").version("1.1.3") - library("emoji-java", "com.github.minndevelopment", "emoji-java").version("master-SNAPSHOT") +// library("emoji-java", "com.github.minndevelopment", "emoji-java").version("master-SNAPSHOT") + library("jemoji", "net.fellbaum", "jemoji").version("1.5.2") library("botCommons", "me.duncte123", "botCommons").version("3.1.18") - bundle("featureLibs", listOf("botCommons", "weebjava", "loadingBar", "jagTag", "wolfram-alpha", "duration-parser", "emoji-java")) + bundle("featureLibs", listOf("botCommons", "weebjava", "loadingBar", "jagTag", "wolfram-alpha", "duration-parser", "jemoji")) library("javaLyrics", "com.github.DuncteBot.java-timed-lyrics", "protocol").version("1.2.0") }