Search in sources :

Example 1 with Color

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

Aggregations

Color (io.github.darkkronicle.advancedchatcore.util.Color)1 Colors (io.github.darkkronicle.advancedchatcore.util.Colors)1 FluidText (io.github.darkkronicle.advancedchatcore.util.FluidText)1 RawText (io.github.darkkronicle.advancedchatcore.util.RawText)1 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 CommandSource (net.minecraft.command.CommandSource)1