Search in sources :

Example 1 with PositionedEmote

use of net.blay09.mods.chattweaks.chat.emotes.PositionedEmote in project ChatTweaks by blay09.

the class ChatView method addChatLine.

public ChatMessage addChatLine(ChatMessage chatLine) {
    chatLine = chatLine.copy();
    chatLines.add(chatLine);
    if (chatLines.size() > MAX_MESSAGES) {
        chatLines.remove(0);
    }
    try {
        if (chatLine.getSender() == null) {
            chatLine.setSender(subTextComponent(chatLine.getTextComponent(), lastMatcher.start("s"), lastMatcher.end("s")));
        }
        if (chatLine.getMessage() == null) {
            chatLine.setMessage(subTextComponent(chatLine.getTextComponent(), lastMatcher.start("m"), lastMatcher.end("m")));
        }
    } catch (IllegalArgumentException ignored) {
        if (chatLine.getMessage() == null) {
            chatLine.setMessage(chatLine.getTextComponent());
        }
    }
    ITextComponent source = chatLine.getTextComponent();
    ITextComponent textComponent = chatLine.getTextComponent();
    if (!builtOutputFormat.equals("$0")) {
        textComponent = new TextComponentString("");
        int last = 0;
        Matcher matcher = groupPattern.matcher(builtOutputFormat);
        while (matcher.find()) {
            if (matcher.start() > last) {
                textComponent.appendText(builtOutputFormat.substring(last, matcher.start()));
            }
            ITextComponent groupValue = null;
            String namedGroup = matcher.group(2);
            if (namedGroup != null) {
                if (namedGroup.equals("s") && chatLine.getSender() != null) {
                    groupValue = chatLine.getSender();
                } else if (namedGroup.equals("m") && chatLine.getMessage() != null) {
                    groupValue = chatLine.getMessage();
                } else if (namedGroup.equals("t")) {
                    groupValue = new TextComponentString(ChatTweaksConfig.timestampFormat.format(new Date(chatLine.getTimestamp())));
                    groupValue.getStyle().setColor(TextFormatting.GRAY);
                } else {
                    int groupStart = -1;
                    int groupEnd = -1;
                    try {
                        groupStart = lastMatcher.start(namedGroup);
                        groupEnd = lastMatcher.end(namedGroup);
                    } catch (IllegalArgumentException ignored) {
                    }
                    if (groupStart != -1 && groupEnd != -1) {
                        groupValue = subTextComponent(source, groupStart, groupEnd);
                    } else {
                        groupValue = chatLine.getOutputVar(namedGroup);
                    }
                }
            } else {
                int group = Integer.parseInt(matcher.group(1));
                if (group >= 0 && group <= lastMatcher.groupCount()) {
                    groupValue = subTextComponent(source, lastMatcher.start(group), lastMatcher.end(group));
                }
            }
            if (groupValue == null) {
                groupValue = new TextComponentString("missingno");
            }
            last = matcher.end();
            textComponent.appendSibling(groupValue);
        }
        if (last < builtOutputFormat.length()) {
            textComponent.appendText(builtOutputFormat.substring(last, builtOutputFormat.length()));
        }
    }
    ITextComponent newComponent = null;
    for (ITextComponent component : textComponent) {
        if (component instanceof TextComponentString) {
            String text = ((TextComponentString) component).getText();
            if (text.length() > 1) {
                int index = 0;
                StringBuilder sb = new StringBuilder();
                List<PositionedEmote> emotes = emoteScanner.scanForEmotes(text, null);
                for (PositionedEmote emoteData : emotes) {
                    if (index < emoteData.getStart()) {
                        sb.append(text.substring(index, emoteData.getStart()));
                    }
                    int imageIndex = sb.length() + 1;
                    sb.append("\u00a7*");
                    for (int i = 0; i < emoteData.getEmote().getWidthInSpaces(); i++) {
                        sb.append(' ');
                    }
                    chatLine.addImage(new ChatImageEmote(imageIndex, emoteData.getEmote()));
                    index = emoteData.getEnd() + 1;
                }
                if (index < text.length()) {
                    sb.append(text.substring(index));
                }
                ((TextComponentString) component).text = sb.toString();
            }
            if (text.length() > 0) {
                if (newComponent == null) {
                    newComponent = new TextComponentString("");
                    newComponent.setStyle(textComponent.getStyle().createDeepCopy());
                }
                TextComponentString copyComponent = new TextComponentString(((TextComponentString) component).text);
                copyComponent.setStyle(component.getStyle());
                newComponent.appendSibling(copyComponent);
            }
        }
    }
    if (newComponent == null) {
        newComponent = textComponent;
    }
    chatLine.setTextComponent(newComponent);
    return chatLine;
}
Also used : Matcher(java.util.regex.Matcher) ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString) PositionedEmote(net.blay09.mods.chattweaks.chat.emotes.PositionedEmote) ChatImageEmote(net.blay09.mods.chattweaks.image.ChatImageEmote) Date(java.util.Date) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 2 with PositionedEmote

use of net.blay09.mods.chattweaks.chat.emotes.PositionedEmote in project ChatTweaks by blay09.

the class EmoteScanner method scanEmotes.

public String scanEmotes(String text, ChatMessage message) {
    int index = 0;
    StringBuilder sb = new StringBuilder();
    List<PositionedEmote> emotes = scanForEmotes(text, null);
    for (PositionedEmote emoteData : emotes) {
        if (index < emoteData.getStart()) {
            sb.append(text.substring(index, emoteData.getStart()));
        }
        int imageIndex = sb.length() + 1;
        sb.append("\u00a7*");
        for (int i = 0; i < emoteData.getEmote().getWidthInSpaces(); i++) {
            sb.append(' ');
        }
        message.addImage(new ChatImageEmote(imageIndex, emoteData.getEmote()));
        index = emoteData.getEnd() + 1;
    }
    if (index < text.length()) {
        sb.append(text.substring(index));
    }
    text = sb.toString();
    return text;
}
Also used : ChatImageEmote(net.blay09.mods.chattweaks.image.ChatImageEmote)

Aggregations

ChatImageEmote (net.blay09.mods.chattweaks.image.ChatImageEmote)2 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 PositionedEmote (net.blay09.mods.chattweaks.chat.emotes.PositionedEmote)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 TextComponentString (net.minecraft.util.text.TextComponentString)1