Solved Multiple ChatColor tags for 1 string?

Discussion in 'Spigot Plugin Development' started by Creatos137, Apr 26, 2017.

  1. Hey,

    How do I apply multiple ChatColor tags to 1 string because when I do:
    ChatColor.BOLD + ChatColor.WHITE + "Test"
    it comes up as an error.

    Thank you for your time
    - Creatos
  2. Choco


    That's because you cannot concatenate two enum constants together. When concatenating an object to a String, the compiler assumes you want to invoke #toString() on the object, but when concatenating two enum constants it thinks you just want to add constants together much like two numerical values. To fix this, call #toString() on the first enum
    Code (Java):
    ChatColor.BOLD.toString() + ChatColor.WHITE + "Test"
  3. Also works for me when I do this, because you're essentially separating the two values.
    Code (Text):
    ChatColor.WHITE + (ChatColor.BOLD + "Test")
  4. Choco


    Correct because the result of the values in the bracket will be a String, and you'll end up concatenating a String and an enumeration constant anyways. Though the issue with this method is that it can get rather ugly when you have to concatenation multiple ChatColors and would end up in more brackets than you need. Could be confusing in various pieces of code. It does work, however!
  5. Putting here for reference in case someone finds this by searching.

    It's easiest to use translateAlternateColorCodes in some cases, especially when the colors are made configurable or provided in commands.

    Code (Text):
    ChatColor.translateAlternateColorCodes('&', "&l&fTest");
  6. Strange, this never worked for me.
    Couldn't we also just use § in order to create the colour. That seems to work for me.
  7. Choco


    Yes, but some systems do not support the encoding of the section symbol, and often results in white chat with missing characters in chat (white question mark boxes). It's discouraged to directly use the section symbol. If it were supported on all platforms, we wouldn't have the ChatColor API in the first place ;)
    • Agree Agree x 2
  8. Agreed.
    Though, I admit I'm lazy with this stuff, and I basically never use the ChatColor for anything.
    Yes, as I continuously switch between mac and windows, it always changes to those odd questionmark boxes, but all I had to do was change it to '\u00a7'. Now, that doesn't seem to change and works on both platforms.
    Is there a special case where even this won't work?
    Is there a chance the character used may change in the future; thus breaking such things?
  9. Choco


    I've see it break a few systems as well. I would imagine it's not super friendly with various Linux distributions. I highly doubt that Mojang would consider changing the symbol because it's been so standard in all versions of Minecraft.

    I dealt with a premium resource author recently (who will remain unnamed) who had countless people complaining about the colours not functioning properly, and he was in fact replacing all values with the unicode for the section symbol. If you really don't like the length of the ChatColor enum, you have the option to static import it as well
    Code (Java):
    import static org.bukkit.ChatColor;

    public class SomeClass {
        public void someMethod() {
            String value = RED + "Something red " + BLUE + " and something blue";
  10. No, it's not that, it's pure laziness and I could just start using them :) Thanks for clarifying!
    • Like Like x 2