java.lang.Object
me.remigio07.chatplugin.api.common.util.text.ChatColor

public class ChatColor extends Object
Environment-indipendent (Bukkit, Sponge, BungeeCord and Velocity) chat color API with 1.16+ hex color codes support.

This class is a pseudo-Enum. It contains the following methods: name(), ordinal(), valueOf(String) and values().

  • Field Details

    • BLACK

      public static final ChatColor BLACK
      Black.

      Code: 0
      Type: color
      Hex: #000000

    • DARK_BLUE

      public static final ChatColor DARK_BLUE
      Dark blue.

      Code: 1
      Type: color
      Hex: #0000AA

    • DARK_GREEN

      public static final ChatColor DARK_GREEN
      Dark green.

      Code: 2
      Type: color
      Hex: #00AA00

    • DARK_AQUA

      public static final ChatColor DARK_AQUA
      Dark aqua.

      Code: 3
      Type: color
      Hex: #00AAAA

    • DARK_RED

      public static final ChatColor DARK_RED
      Dark red.

      Code: 4
      Type: color
      Hex: #AA0000

    • DARK_PURPLE

      public static final ChatColor DARK_PURPLE
      Dark purple.

      Code: 5
      Type: color
      Hex: #AA00AA

    • GOLD

      public static final ChatColor GOLD
      Gold.

      Code: 6
      Type: color
      Hex: #FFAA00

    • GRAY

      public static final ChatColor GRAY
      Gray.

      Code: 7
      Type: color
      Hex: #AAAAAA

    • DARK_GRAY

      public static final ChatColor DARK_GRAY
      Dark gray.

      Code: 8
      Type: color
      Hex: #555555

    • BLUE

      public static final ChatColor BLUE
      Blue.

      Code: 9
      Type: color
      Hex: #5555FF

    • GREEN

      public static final ChatColor GREEN
      Green.

      Code: a/A
      Type: color
      Hex: #55FF55

    • AQUA

      public static final ChatColor AQUA
      Aqua.

      Code: b/B
      Type: color
      Hex: #55FFFF

    • RED

      public static final ChatColor RED
      Red.

      Code: c/C
      Type: color
      Hex: #FF5555

    • LIGHT_PURPLE

      public static final ChatColor LIGHT_PURPLE
      Light purple.

      Code: d/D
      Type: color
      Hex: #FF55FF

    • YELLOW

      public static final ChatColor YELLOW
      Yellow.

      Code: e/E
      Type: color
      Hex: #FFFF55

    • WHITE

      public static final ChatColor WHITE
      White.

      Code: f/F
      Type: color
      Hex: #FFFFFF

    • OBFUSCATED

      public static final ChatColor OBFUSCATED
      Obfuscated/magic.

      Code: k/K
      Type: format

    • BOLD

      public static final ChatColor BOLD
      Bold.

      Code: l/L
      Type: format

    • STRIKETHROUGH

      public static final ChatColor STRIKETHROUGH
      Strikethrough.

      Code: m/M
      Type: format

    • UNDERLINE

      public static final ChatColor UNDERLINE
      Underline.

      Code: n/N
      Type: format

    • ITALIC

      public static final ChatColor ITALIC
      Italic.

      Code: o/O
      Type: format

    • RESET

      public static final ChatColor RESET
      Reset.

      Code: r/R
      Type: color

    • COLOR_CODES

      public static final String COLOR_CODES
      String containing all default color codes.

      Content: "0123456789AaBbCcDdEeFfRr"

      See Also:
    • FORMAT_CODES

      public static final String FORMAT_CODES
      String containing all default format codes.

      Content: "KkLlMmNnOo"

      See Also:
    • CODES

      public static final String CODES
      String containing all default color and format codes.

      Content: "0123456789AaBbCcDdEeFfRrKkLlMmNnOo"

      See Also:
    • STRIP_COLOR

      public static final Pattern STRIP_COLOR
      Pattern used to strip color from a string.

      Regex: "(?i)§[0-9A-FK-ORX]"

    • HEX_COLORS

      public static final Pattern[] HEX_COLORS
      Patterns used to identify hex colors.

      Contents: ["&#([A-Fa-f0-9]){6}", "#([A-Fa-f0-9]){6}"]

  • Method Details

    • toString

      public String toString()
      Gets the string representation of this color.

      Will return '§' + getCode() if isDefaultColor() and a hex string (example: "§x§f§f§5§5§f§f" - translated) otherwise.

      Overrides:
      toString in class Object
      See Also:
    • equals

      public boolean equals(Object obj)
      Checks if another object is an instance of ChatColor and if this color's toString() value is equal to the other object's one.
      Overrides:
      equals in class Object
      Parameters:
      obj - Object to compare
      Returns:
      Whether the two objects are equal
    • hashCode

      public int hashCode()
      Gets this color's hash code.

      Will return toString()'s hash code.

      Overrides:
      hashCode in class Object
      Returns:
      Color's hash code
    • bukkitValue

      public org.bukkit.ChatColor bukkitValue()
      Gets the chat color adapted for Bukkit environments.

      Will return ChatColor.RESET if !isDefaultColor().

      Returns:
      Bukkit-adapted chat color
      Throws:
      UnsupportedOperationException - If !Environment.isBukkit()
    • spongeValue

      public org.spongepowered.api.text.TextElement spongeValue()
      Gets the chat color adapted for Sponge environments.

      Will return TextColors.RESET if !isDefaultColor().

      Returns:
      Sponge-adapted chat color
      Throws:
      UnsupportedOperationException - If !Environment.isSponge()
    • bungeeValue

      public net.md_5.bungee.api.ChatColor bungeeValue()
      Gets the chat color adapted for BungeeCord environments.

      This method supports hex colors (!isDefaultColor()).

      Returns:
      BungeeCord-adapted chat color
      Throws:
      UnsupportedOperationException - If !Environment.isBungeeCord()
    • velocityValue

      public net.kyori.adventure.text.format.TextFormat velocityValue()
      Gets the chat color adapted for Velocity environments.

      This method supports hex colors (!isDefaultColor()).

      Returns:
      Velocity-adapted chat color
      Throws:
      UnsupportedOperationException - If !Environment.isVelocity()
    • name

      public String name()
      Equivalent of Enum.name().

      Will return a hex string (example: "#FF55FF") if !isDefaultColor().

      Returns:
      Constant's name
    • ordinal

      public int ordinal()
      Equivalent of Enum.ordinal().

      Will return -1 if !isDefaultColor().

      Returns:
      Constant's ordinal
    • getCode

      public Character getCode()
      Gets this color's code.

      Will return null if !isDefaultColor().

      Returns:
      Color's code
    • getColor

      public Color getColor()
      Gets this color's Color value.

      Will return null if isFormatCode().

      Note: returned color has an alpha value of 255.

      Returns:
      Color's value
    • isDefaultColor

      public boolean isDefaultColor()
      Checks if this is a default color.
      Returns:
      Whether this is a default color
    • isFormatCode

      public boolean isFormatCode()
      Checks if this is a format code.
      Returns:
      Whether this is a format code
    • getClosestDefaultColor

      public ChatColor getClosestDefaultColor()
      Gets the default color closest to this color.

      Will return this if isDefaultColor() || isFormatCode().

      Note: this method uses the same algorithm of ViaVersion.

      Returns:
      Closest default color
    • of

      public static ChatColor of(Color color)
      Gets a chat color from given Color.

      May return one of the 16 default colors.

      Parameters:
      color - Color to transform
      Returns:
      Chat color equivalent
    • of

      public static ChatColor of(String hex)
      Gets a chat color from given hex color code.

      May return one of the 16 default colors.

      The following formats are supported: "#rrggbb", "rrggbb".

      Parameters:
      hex - Color's hex representation, with or without '#'
      Returns:
      Chat color equivalent
      Throws:
      NumberFormatException - If the specified string's format is invalid
    • translate

      public static String translate(String string)
      Calls translate(String, boolean), specifying true as the second argument.
      Parameters:
      string - String to translate
      Returns:
      Translated string
    • translate

      public static String translate(String string, boolean retainNewLines)
      Translates given string applying default ("&x") and hex ("&#rrggbb", "#rrggbb") color codes.
      Parameters:
      string - String to translate
      retainNewLines - Whether to retain new lines or to replace them with spaces
      Returns:
      Translated string
    • translate

      public static List<String> translate(List<String> list)
      Translates given string list.
      Parameters:
      list - List to translate
      Returns:
      Translated string list
      See Also:
    • translate

      public static List<String> translate(List<String> list, boolean retainNewLines)
      Translates given string list.
      Parameters:
      list - List to translate
      retainNewLines - Whether to retain new lines or to replace them with spaces
      Returns:
      Translated string list
      See Also:
    • stripColor

      public static String stripColor(String string)
      Strips a string from any color and/or formatting codes.
      Parameters:
      string - String to strip
      Returns:
      Stripped string
    • getLastColors

      public static String getLastColors(String string)
      Gets the last colors contained in a string.

      Will return an empty string if there are no colors in the string.

      Parameters:
      string - String to check
      Returns:
      Last colors in string
    • isColorCode

      public static boolean isColorCode(char character)
      Checks if the specified character is a valid color code.
      Parameters:
      character - Character to check
      Returns:
      Whether the character is a color code
      See Also:
    • isFormatCode

      public static boolean isFormatCode(char character)
      Checks if the specified character is a valid format code.
      Parameters:
      character - Character to check
      Returns:
      Whether the character is a format code
      See Also:
    • isColorString

      public static boolean isColorString(String string)
      Checks if the specified string contains color codes.
      Parameters:
      string - String to check
      Returns:
      Whether the string contains colors
    • getRandomColor

      public static ChatColor getRandomColor()
      Gets a random color, format codes not included.

      Hex colors will be returned on 1.16+ environments.

      Returns:
      Random color
    • getByChar

      public static ChatColor getByChar(char code)
      Gets a default chat color by its code.

      Will return null if the code is invalid.

      Parameters:
      code - Color's code
      Returns:
      Corresponding color
    • valueOf

      public static ChatColor valueOf(String name)
      Equivalent of Enum#valueOf(String), with the only difference that instead of throwing IllegalArgumentException null is returned if the constant's name is invalid.

      This method recognizes Bukkit's, Sponge's BungeeCord's and Velocity's IDs.

      Parameters:
      name - Constant's name
      Returns:
      Enum constant
    • values

      public static ChatColor[] values()
      Equivalent of Enum#values().
      Returns:
      Enum constants