001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.Collections;
029import java.util.List;
030import java.util.regex.Matcher;
031
032/**
033 * Handles formatting Messages and managing colors
034 * @param <FT> The platform specific color object
035 */
036public abstract class MessageFormatter <FT> {
037
038    private final List<FT> colors = new ArrayList<>();
039
040    @SafeVarargs
041    public MessageFormatter(FT... colors) {
042        this.colors.addAll(Arrays.asList(colors));
043
044    }
045    public FT setColor(int index, FT color) {
046        if (index > 0) {
047            index--;
048        } else {
049            index = 0;
050        }
051        if (this.colors.size() <= index) {
052            int needed = index - this.colors.size();
053            if (needed > 0) {
054                this.colors.addAll(Collections.nCopies(needed, null));
055            }
056            colors.add(color);
057            return null;
058        } else {
059            return colors.set(index, color);
060        }
061    }
062
063    public FT getColor(int index) {
064        if (index > 0) {
065            index--;
066        } else {
067            index = 0;
068        }
069        FT color = colors.get(index);
070        if (color == null) {
071            color = getDefaultColor();
072        }
073        return color;
074    }
075
076    public FT getDefaultColor() {
077        return getColor(1);
078    }
079
080    abstract String format(FT color, String message);
081
082    public String format(int index, String message) {
083        return format(getColor(index), message);
084    }
085
086    public String format(String message) {
087        String def = format(1, "");
088        Matcher matcher = ACFPatterns.FORMATTER.matcher(message);
089        StringBuffer sb = new StringBuffer(message.length());
090        while (matcher.find()) {
091            Integer color = ACFUtil.parseInt(matcher.group("color"), 1);
092            String msg = format(color, matcher.group("msg")) + def;
093            matcher.appendReplacement(sb, Matcher.quoteReplacement(msg));
094        }
095        matcher.appendTail(sb);
096        return def + sb.toString();
097    }
098}