Java Code Examples for org.bukkit.ChatColor#isColor()

The following examples show how to use org.bukkit.ChatColor#isColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VariableString.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static ChatColor getLastColor(final CharSequence s) {
	for (int i = s.length() - 2; i >= 0; i--) {
		if (s.charAt(i) == ChatColor.COLOR_CHAR) {
			final ChatColor c = ChatColor.getByChar(s.charAt(i + 1));
			if (c != null && (c.isColor() || c == ChatColor.RESET))
				return c;
		}
	}
	return null;
}
 
Example 2
Source File: JSONMessage.java    From JSONMessage with MIT License 5 votes vote down vote up
/**
 * @param color The color to set
 */
public void setColor(ChatColor color) {
    if (!color.isColor()) {
        throw new IllegalArgumentException(color.name() + " is not a color!");
    }
    this.color = color;
}
 
Example 3
Source File: UltimateFancy.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
private List<JSONObject> parseColors(String text) {
    List<JSONObject> jsonList = new ArrayList<>();
    for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + ")")) {
        JSONObject workingText = new JSONObject();

        //fix colors before
        filterColors(workingText);

        Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
        if (match.find()) {
            ChatColor color = ChatColor.getByChar(match.group(1).charAt(0));
            if (color.isColor()) {
                lastColor = color;
                last2Color = null;
            } else {
                // Set a second color if the first color is format
                if (lastColor.isColor()) last2Color = lastColor;
                lastColor = color;
            }
            //fix colors from latest
            filterColors(workingText);
            if (part.length() == 2) continue;
        }
        //continue if empty
        if (ChatColor.stripColor(part).isEmpty()) {
            continue;
        }

        workingText.put("text", ChatColor.stripColor(part));

        //fix colors after
        filterColors(workingText);

        if (!workingText.containsKey("color")) {
            workingText.put("color", "white");
        }
        jsonList.add(workingText);
    }
    return jsonList;
}
 
Example 4
Source File: UltimateFancy.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
private JSONArray addColorToArray(String text) {
    JSONArray extraArr = new JSONArray();
    ChatColor color = ChatColor.WHITE;
    for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + "[0-9a-fA-Fk-oK-ORr])")) {
        JSONObject objExtraTxt = new JSONObject();
        Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
        if (match.find()) {
            color = ChatColor.getByChar(match.group(1).charAt(0));
            if (part.length() == 2) continue;
        }
        objExtraTxt.put("text", ChatColor.stripColor(part));
        if (color.isColor()) {
            objExtraTxt.put("color", color.name().toLowerCase());
            objExtraTxt.remove("obfuscated");
            objExtraTxt.remove("underlined");
            objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
        }
        if (color.equals(ChatColor.RESET)) {
            objExtraTxt.put("color", "white");
            objExtraTxt.remove("obfuscated");
            objExtraTxt.remove("underlined");
            objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
        }
        if (color.isFormat()) {
            if (color.equals(ChatColor.MAGIC)) {
                objExtraTxt.put("obfuscated", true);
            } else if (color.equals(ChatColor.UNDERLINE)) {
                objExtraTxt.put("underlined", true);
            } else {
                objExtraTxt.put(color.name().toLowerCase(), true);
            }
        }
        extraArr.add(objExtraTxt);
    }
    return extraArr;
}
 
Example 5
Source File: UltimateFancy.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private List<JSONObject> parseColors(String text) {
    List<JSONObject> jsonList = new ArrayList<>();
    for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + ")")) {
        JSONObject workingText = new JSONObject();

        //fix colors before
        filterColors(workingText);

        Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
        if (match.find()) {
            ChatColor color = ChatColor.getByChar(match.group(1).charAt(0));
            if (color.isColor()) {
                lastColor = color;
                last2Color = null;
            } else {
                // Set a second color if the first color is format
                if (lastColor.isColor()) last2Color = lastColor;
                lastColor = color;
            }
            //fix colors from latest
            filterColors(workingText);
            if (part.length() == 2) continue;
        }
        //continue if empty
        if (ChatColor.stripColor(part).isEmpty()) {
            continue;
        }

        workingText.put("text", ChatColor.stripColor(part));

        //fix colors after
        filterColors(workingText);

        if (!workingText.containsKey("color")) {
            workingText.put("color", "white");
        }
        jsonList.add(workingText);
    }
    return jsonList;
}
 
Example 6
Source File: UltimateFancy.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private JSONArray addColorToArray(String text) {
    JSONArray extraArr = new JSONArray();
    ChatColor color = ChatColor.WHITE;
    for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + "[0-9a-fA-Fk-oK-ORr])")) {
        JSONObject objExtraTxt = new JSONObject();
        Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
        if (match.find()) {
            color = ChatColor.getByChar(match.group(1).charAt(0));
            if (part.length() == 2) continue;
        }
        objExtraTxt.put("text", ChatColor.stripColor(part));
        if (color.isColor()) {
            objExtraTxt.put("color", color.name().toLowerCase());
            objExtraTxt.remove("obfuscated");
            objExtraTxt.remove("underlined");
            objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
        }
        if (color.equals(ChatColor.RESET)) {
            objExtraTxt.put("color", "white");
            objExtraTxt.remove("obfuscated");
            objExtraTxt.remove("underlined");
            objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
        }
        if (color.isFormat()) {
            if (color.equals(ChatColor.MAGIC)) {
                objExtraTxt.put("obfuscated", true);
            } else if (color.equals(ChatColor.UNDERLINE)) {
                objExtraTxt.put("underlined", true);
            } else {
                objExtraTxt.put(color.name().toLowerCase(), true);
            }
        }
        extraArr.add(objExtraTxt);
    }
    return extraArr;
}
 
Example 7
Source File: LocalChatPaginator.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Takes a string and returns the last colors that can be copied to a new line
 */
public static String getLastColors(String input) {
    ChatColor lastColor = null;
    List<ChatColor> lastFormats = new ArrayList<>();

    int length = input.length();

    for (int index = length - 1; index > -1; --index) {
        char section = input.charAt(index);
        if (section == 167 && index < length - 1) {
            char c = input.charAt(index + 1);
            ChatColor color = ChatColor.getByChar(c);

            if (color != null) {
                if (color.equals(ChatColor.RESET)) {
                    break;
                }

                if (color.isColor() && lastColor == null) {
                    lastColor = color;
                    continue;
                }

                if (color.isFormat() && !lastFormats.contains(color)) {
                    lastFormats.add(color);
                }
            }
        }
    }

    String result = lastFormats.stream()
            .map(ChatColor::toString)
            .collect(Collectors.joining(""));

    if (lastColor != null) {
        result = lastColor.toString() + result;
    }
    return result;
}
 
Example 8
Source File: AbstractScoreboardTeam.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static boolean isStringFormatOverride(String string, ChatColor format) {
	if ((string.length() >= 2) && (string.charAt(0) == ChatColor.COLOR_CHAR)) {
		ChatColor formatStringColor = ChatColor.getByChar(string.charAt(1));
		if ((formatStringColor != null) && (formatStringColor.isColor() || (formatStringColor == format))) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: MiscUtil.java    From CardinalPGM with MIT License 4 votes vote down vote up
public static Color convertChatColorToColor(ChatColor chatColor) {
    if (chatColor.isColor()) {
        switch (chatColor) {
            case AQUA:
                return convertHexToRGB("55FFFF");
            case BLACK:
                return convertHexToRGB("000000");
            case BLUE:
                return convertHexToRGB("5555FF");
            case DARK_AQUA:
                return convertHexToRGB("00AAAA");
            case DARK_BLUE:
                return convertHexToRGB("0000AA");
            case DARK_GRAY:
                return convertHexToRGB("555555");
            case DARK_GREEN:
                return convertHexToRGB("00AA00");
            case DARK_PURPLE:
                return convertHexToRGB("AA00AA");
            case DARK_RED:
                return convertHexToRGB("AA0000");
            case GOLD:
                return convertHexToRGB("FFAA00");
            case GRAY:
                return convertHexToRGB("AAAAAA");
            case GREEN:
                return convertHexToRGB("55FF55");
            case LIGHT_PURPLE:
                return convertHexToRGB("FF55FF");
            case RED:
                return convertHexToRGB("FF5555");
            case WHITE:
                return convertHexToRGB("FFFFFF");
            case YELLOW:
                return convertHexToRGB("FFFF55");
            default:
                return convertHexToRGB("AAAAAA");
        }
    } else {
        return convertHexToRGB("AAAAAA");
    }
}