Search in sources :

Example 1 with SearchResult

use of io.github.darkkronicle.advancedchatcore.util.SearchResult 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)

Aggregations

FluidText (io.github.darkkronicle.advancedchatcore.util.FluidText)1 SearchResult (io.github.darkkronicle.advancedchatcore.util.SearchResult)1 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)1 Style (net.minecraft.text.Style)1 Formatting (net.minecraft.util.Formatting)1