1.16.5 Use Hex Color Codes in Clickable Message?

Discussion in 'Spigot Plugin Development' started by Rawora, Dec 7, 2020.

  1. Hello,

    I use #Player.Spigot().sendMessage() to send a hover and clickable chat message.
    Now I also want to include hex color codes in that message which doesn't seem to work.

    This color code "#618a3d", for example, would be a dark green, but this is wrongly displayed as pink.

    When I use #Player.sendMessage(), the color code works fine. (Just has the disadvantage that I can't send a clickable chat that way.)

    I have this method for converting the hex color code:
    Code (Java):
        public final Pattern HEX_PATTERN = Pattern.compile("#([A-Fa-f0-9]{6})");
        public final char COLOR_CHAR = ChatColor.COLOR_CHAR;
        public String translateHexColorCodes(String message) {
            Matcher matcher = HEX_PATTERN.matcher(message);
            StringBuffer buffer = new StringBuffer(message.length() + 4 * 8);
            while (matcher.find()) {
                String group = matcher.group(1);
                matcher.appendReplacement(buffer, COLOR_CHAR + "x"
                        + COLOR_CHAR + group.charAt(0) + COLOR_CHAR + group.charAt(1)
                        + COLOR_CHAR + group.charAt(2) + COLOR_CHAR + group.charAt(3)
                        + COLOR_CHAR + group.charAt(4) + COLOR_CHAR + group.charAt(5)
            return matcher.appendTail(buffer).toString();

  2. Why not just use "+ group"?
  3. This is just a utility method I found when looking up how to convert the hex codes into colors. Didn't really pay attention to optimizing it

    Right now It works when I use Player.sendMessage but it does not work when I want to use Player.Spigot().sendMessage and include a clickable chat message. :/
    Do you have an idea why that is?
  4. Use TextComponent.fromLegacyText
  5. You are not showing us the code that actually sends them the message.
  6. Code (Java):
    String test = "#618a3dHello";

    // Works, correct color.

    // Does not work, wrong color
    TextComponent textComponent = new TextComponent(test + ", please click ");
    textComponent.addExtra(getClickableMessage("§6§l[here]", "§6[Click here]", "/help"));
    (This is just an example)
  7. Thank you very much for your input, could you please give me an code example how that would like with my example above?
    Could I still have the hover & click working with the color codes?
  8. SteelPhoenix


    Considering it's your own parsing that messes up, why not just match the hex color and replace it with a bungee ChatColor (which supports hex parsing)?
    • Agree Agree x 1
  9. Code (Java):
    private static final Pattern HEX_PATTERN = Pattern.compile("&(#\\w{6})");
        public static String colorize(String str)
            Matcher matcher = HEX_PATTERN.matcher(ChatColor.translateAlternateColorCodes('&', str));
            StringBuffer buffer = new StringBuffer();

            while (matcher.find())
                matcher.appendReplacement(buffer, ChatColor.of(matcher.group(1)).toString());

            return matcher.appendTail(buffer).toString();
    Here's what I use