Strange bug when using ChatColors

Discussion in 'Spigot Plugin Development' started by Without_Regret, May 14, 2015.

  1. Code:
    Code (Text):
    public String errorPrefix() {
        String prefix = " §4§l[§c§l!§4§l] ";
        return prefix;
    }
    Result:
    [​IMG]

    Note: I have tried replacing all the '§' characters with ChatColor.(COLOR), and keep getting the same result.

    If anyone has gotten this before, or knows a fix that would be great, because I cannot figure out why this is happening.
     
  2. ChatColor#translateAlternateColorCodes(..) ???
     
  3. Serializator

    Supporter

    Try to use the ChatColor enumeration, Bukkit implemented it so you don't need to do it the way you do it.
     
  4.  
  5. Does it work when you directly send a colored message to a player? like
    Code (Text):
    player.sendMessage("§6Test");
     
  6. You have character coding error. Try to save your files to UTF-8 character coding.
     
  7. Use the chatcolor enum, and use it correctly. If you have those characters still, then show how you did it.

    Encoding isn't an issue with the chatcolor enum, the unicode character is escaped. Additionally, you should not put unicode characters directly in your file, it will make your code much less portable
     
  8. Don't use unicode, and don't save in UTF-8. Place an empty string inbetween when concatenating chatcolor constants to each other:

    Code (Text):
    String example = ChatColor.RED + "" + ChatColor.BLUE;
    Java has left associativity (reads from left->right), so you need to specify that when you're concatenating the constants, that they are strings.
     
  9. ChatColor#toString()
     
  10. is used automagically by java in this case.
     
  11. Try writing
    Code (Java):
    ChatColor.RED + ChatColor.BLUE
    and see the syntax error. Then write
    Code (Java):
    ChatColor.RED.toString() + ChatColor.BLUE
    and see it work, with one less concatenation.
     
  12. Ah that's what you mean. I know the first one does not work.
     
  13. Either or. Compiler will usually resolve it to that anyhow, I'm lazy and like writing just a couple characters.
     
  14. Honestly I would just use a format method that translates to that from & how i looked at it is unless you are accessing it from another class the method is quite useless, better yet the translateAlternateColorCodes would translate from the config where you can use the config to access from all classes :D