Search in sources :

Example 1 with Emoticons

use of chatty.util.api.Emoticons in project chatty by chatty.

the class ChannelTextPane method addTwitchTagsEmoticons.

/**
 * Adds the emoticons from the Twitch IRCv3 tags.
 *
 * @param emoticons Map of emotes associated with Twitch emote id
 * @param text The message text
 * @param ranges The ranges for this message
 * @param rangesStyle The styles for this message
 * @param emotesDef The emotes definition from the IRCv3 tags
 */
private void addTwitchTagsEmoticons(User user, Map<Integer, Emoticon> emoticons, String text, Map<Integer, Integer> ranges, Map<Integer, MutableAttributeSet> rangesStyle, TagEmotes emotesDef) {
    if (emotesDef == null) {
        return;
    }
    Map<Integer, Emoticons.TagEmote> def = emotesDef.emotes;
    /**
     * Iterate over each character of the message and check if an emote starts
     * at the current position.
     *
     * The offset is used to handle supplemantary characters that consist
     * of two UTF-16 characters. Twitch Chat sees these as only one character
     * so that has to be corrected.
     *
     * https://discuss.dev.twitch.tv/t/jtv-2-receiving-messages/1635/10
     *
     * Example message: "Kappa 𠜎 Kappa"
     */
    int offset = 0;
    for (int i = 0; i < text.length(); ) {
        if (def.containsKey(i - offset)) {
            // An emote starts at the current position, so add it.
            Emoticons.TagEmote emoteData = def.get(i - offset);
            int id = emoteData.id;
            int start = i;
            int end = emoteData.end + offset;
            // Get and check emote
            Emoticon emoticon = null;
            Emoticon customEmote = main.emoticons.getCustomEmoteById(id);
            if (customEmote != null && customEmote.allowedForStream(user.getStream())) {
                emoticon = customEmote;
            } else {
                emoticon = emoticons.get(id);
            }
            if (end < text.length()) {
                if (emoticon == null) {
                    /**
                     * Add emote from message alone
                     */
                    String code = text.substring(start, end + 1);
                    String url = Emoticon.getTwitchEmoteUrlById(id, 1);
                    Emoticon.Builder b = new Emoticon.Builder(Emoticon.Type.TWITCH, code, url);
                    b.setNumericId(id);
                    Emoteset emotesetInfo = main.emoticons.getInfoByEmoteId(id);
                    if (emotesetInfo != null) {
                        b.setEmoteset(emotesetInfo.emoteset_id);
                        b.setStream(emotesetInfo.stream);
                        b.setEmotesetInfo(emotesetInfo.product);
                    } else {
                        b.setEmoteset(Emoticon.SET_UNKNOWN);
                    }
                    emoticon = b.build();
                    main.emoticons.addTempEmoticon(emoticon);
                    LOGGER.info("Added emote from message: " + emoticon);
                }
                if (!main.emoticons.isEmoteIgnored(emoticon)) {
                    addEmoticon(emoticon, start, end, ranges, rangesStyle);
                }
            }
        }
        /**
         * If the current position in the String consists of an character
         * thats more than one long (some Unicode characters), then add to
         * the offset and jump ahead accordingly.
         */
        offset += Character.charCount(text.codePointAt(i)) - 1;
        i += Character.charCount(text.codePointAt(i));
    }
}
Also used : CheerEmoticon(chatty.util.api.CheerEmoticon) Emoticon(chatty.util.api.Emoticon) Emoticons(chatty.util.api.Emoticons) Emoteset(chatty.util.TwitchEmotes.Emoteset)

Aggregations

Emoteset (chatty.util.TwitchEmotes.Emoteset)1 CheerEmoticon (chatty.util.api.CheerEmoticon)1 Emoticon (chatty.util.api.Emoticon)1 Emoticons (chatty.util.api.Emoticons)1