Search in sources :

Example 1 with StringMatch

use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.

the class ColorCodeFormatter method format.

@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
    if (parse != null) {
        return Optional.empty();
    }
    String string = text.getString();
    if (!string.contains("&")) {
        return Optional.empty();
    }
    SearchResult search = SearchResult.searchOf(string, "(?i)&[0-9A-FK-OR]", FindType.REGEX);
    if (search.size() == 0) {
        return Optional.empty();
    }
    int index = 0;
    Style last = Style.EMPTY;
    FluidText formatted = new FluidText();
    for (StringMatch match : search.getMatches()) {
        formatted.append(text.truncate(new StringMatch("", index, match.start)).fillStyle(last));
        Formatting format = Formatting.byCode(match.match.charAt(1));
        last = last.withFormatting(format);
        index = match.start;
    }
    FluidText small = text.truncate(new StringMatch("", index, string.length()));
    if (small != null && !small.getString().isEmpty()) {
        formatted.append(small.fillStyle(last));
    }
    return Optional.of(formatted);
}
Also used : FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) Formatting(net.minecraft.util.Formatting) Style(net.minecraft.text.Style) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) SearchResult(io.github.darkkronicle.advancedchatcore.util.SearchResult)

Example 2 with StringMatch

use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.

the class SpellCheckSuggestor method getHover.

private static Text getHover(String message) {
    String text = ChatBoxConfigStorage.SpellChecker.HOVER_TEXT.config.getStringValue();
    text = text.replaceAll("&", "ยง");
    Optional<StringMatch> match = SearchUtils.getMatch(message, "<suggestion>(.+)</suggestion>", FindType.REGEX);
    if (match.isEmpty()) {
        text = text.replaceAll("\\$1", message).replaceAll("\\$2", "").replaceAll("\\$3", "");
        return StyleFormatter.formatText(new FluidText(new RawText(text, Style.EMPTY)));
    }
    StringMatch stringMatch = match.get();
    String start = message.substring(0, stringMatch.start);
    String end = message.substring(stringMatch.end);
    String middle = message.substring(stringMatch.start + 12, stringMatch.end - 13);
    text = text.replaceAll("\\$1", start).replaceAll("\\$2", middle).replaceAll("\\$3", end);
    return StyleFormatter.formatText(new FluidText(new RawText(text, Style.EMPTY)));
}
Also used : FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) RawText(io.github.darkkronicle.advancedchatcore.util.RawText)

Example 3 with StringMatch

use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.

the class ChatFormatter method format.

/**
 * Format's the chat box contents
 *
 * @param string Contents
 * @return Formatted FluidText. If nothing is changed it will be the contents with Style.EMPTY
 */
public FluidText format(String string) {
    FluidText text = new FluidText(new RawText(string, Style.EMPTY));
    if (string.length() == 0) {
        return text;
    }
    if (suggestor.getAllSuggestions() != null) {
        HashMap<StringMatch, FluidText.StringInsert> format = new HashMap<>();
        for (AdvancedSuggestions suggestions : suggestor.getAllSuggestions()) {
            if (suggestions.getSuggestions().isEmpty()) {
                // Don't want to format if there's nothing there...
                continue;
            }
            boolean atLeastOne = false;
            for (AdvancedSuggestion suggestion : suggestions.getSuggestions()) {
                if (suggestion.getText().length() > 0) {
                    atLeastOne = true;
                    break;
                }
            }
            if (!atLeastOne) {
                continue;
            }
            StringRange range = suggestions.getRange();
            int start = range.getStart();
            int end = range.getEnd();
            if (end > string.length()) {
                end = string.length();
            }
            if (start < 0) {
                start = 0;
            }
            String matchString = string.subSequence(start, end).toString();
            format.put(new StringMatch(matchString, start, end), (current, match) -> {
                Style style = Style.EMPTY;
                style = style.withFormatting(Formatting.UNDERLINE);
                TextColor textColor = TextColor.fromRgb(ChatBoxConfigStorage.General.AVAILABLE_SUGGESTION_COLOR.config.get().color());
                style = style.withColor(textColor);
                return new FluidText(new RawText(matchString, style));
            });
        }
        text.replaceStrings(format);
    }
    for (ChatFormatterRegistry.ChatFormatterOption option : ChatFormatterRegistry.getInstance().getAll()) {
        if (!option.isActive()) {
            continue;
        }
        Optional<FluidText> otext = option.getOption().format(text, suggestor.getParse());
        if (otext.isPresent()) {
            text = otext.get();
        }
    }
    return text;
}
Also used : HashMap(java.util.HashMap) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) RawText(io.github.darkkronicle.advancedchatcore.util.RawText) StringRange(com.mojang.brigadier.context.StringRange) FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) Style(net.minecraft.text.Style) ChatFormatterRegistry(io.github.darkkronicle.advancedchatbox.registry.ChatFormatterRegistry) TextColor(net.minecraft.text.TextColor)

Example 4 with StringMatch

use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.

the class CommandColorer method format.

@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
    if (parse == null) {
        if (text.getString().charAt(0) == '/') {
            return Optional.of(new FluidText(RawText.withColor(text.getString(), CommandColorerStorage.ERROR_COLOR.config.get())));
        }
        return Optional.empty();
    }
    CommandContextBuilder<CommandSource> commandContextBuilder = parse.getContext().getLastChild();
    HashMap<StringMatch, FluidText.StringInsert> replace = new HashMap<>();
    int lowest = -1;
    int max = 0;
    int index = 0;
    String string = text.getString();
    int length = string.length();
    Colors.Palette palette = Colors.getInstance().get(CommandColorerStorage.DEFAULT_PALETTE.config.getStringValue()).orElse(Colors.getInstance().getDefault());
    TreeSet<CommandSection<?>> sections = new TreeSet<>(compileObjects(parse, string));
    // Arguments
    for (CommandSection<?> section : sections) {
        int start = section.getMatch().start;
        int end = Math.min(section.getMatch().end, length);
        StringMatch match = new StringMatch(string.subSequence(start, end).toString(), start, end);
        if (lowest == -1 || start < lowest) {
            lowest = start;
        }
        if (end > max) {
            max = end;
        }
        Color color = palette.getColors().get(index % palette.getColors().size());
        replace.put(match, (current, match1) -> {
            if (current.getStyle().equals(Style.EMPTY)) {
                return new FluidText(RawText.withColor(match1.match, color));
            }
            return new FluidText(new RawText(match1.match, current.getStyle()));
        });
        index += 1;
    }
    if (lowest > -1) {
        replace.put(new StringMatch(text.getString().substring(0, lowest), 0, lowest), (current, match) -> {
            if (current.getStyle().equals(Style.EMPTY)) {
                return new FluidText(RawText.withColor(match.match, CommandColorerStorage.COMMAND_COLOR.config.get()));
            }
            return new FluidText(new RawText(match.match, current.getStyle()));
        });
    }
    if (max != string.length()) {
        replace.put(new StringMatch(text.getString().substring(max, string.length()), max, string.length()), (current, match) -> {
            if (current.getStyle().equals(Style.EMPTY)) {
                return new FluidText(RawText.withColor(match.match, CommandColorerStorage.ERROR_COLOR.config.get()));
            }
            return new FluidText(new RawText(match.match, current.getStyle()));
        });
    }
    text.replaceStrings(replace);
    return Optional.of(text);
}
Also used : HashMap(java.util.HashMap) Color(io.github.darkkronicle.advancedchatcore.util.Color) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) CommandSource(net.minecraft.command.CommandSource) RawText(io.github.darkkronicle.advancedchatcore.util.RawText) FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) Colors(io.github.darkkronicle.advancedchatcore.util.Colors) TreeSet(java.util.TreeSet)

Example 5 with StringMatch

use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.

the class JSONFormatter method format.

@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
    String content = text.getString();
    Optional<List<StringMatch>> omatches = SearchUtils.findMatches(content, "\\{.+\\}", FindType.REGEX);
    if (!omatches.isPresent()) {
        return Optional.empty();
    }
    List<StringMatch> matches = omatches.get();
    HashMap<StringMatch, FluidText.StringInsert> replace = new HashMap<>();
    for (StringMatch m : matches) {
        replace.put(m, (current, match) -> colorJson(match.match));
    }
    text.replaceStrings(replace);
    return Optional.of(text);
}
Also used : HashMap(java.util.HashMap) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)7 FluidText (io.github.darkkronicle.advancedchatcore.util.FluidText)4 RawText (io.github.darkkronicle.advancedchatcore.util.RawText)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 StringRange (com.mojang.brigadier.context.StringRange)2 Style (net.minecraft.text.Style)2 AdvancedSuggestion (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion)1 AdvancedSuggestions (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions)1 ChatFormatterRegistry (io.github.darkkronicle.advancedchatbox.registry.ChatFormatterRegistry)1 Color (io.github.darkkronicle.advancedchatcore.util.Color)1 Colors (io.github.darkkronicle.advancedchatcore.util.Colors)1 SearchResult (io.github.darkkronicle.advancedchatcore.util.SearchResult)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 CommandSource (net.minecraft.command.CommandSource)1 TextColor (net.minecraft.text.TextColor)1 Formatting (net.minecraft.util.Formatting)1 Expression (org.mariuszgromada.math.mxparser.Expression)1