Color codes

Discussion in 'Plugin Development' started by Scullyking, Jan 19, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Scullyking

    Yes it's me again...

    I was expecting to be able to concat() a ChatColor.COLOUR to a string but evidently not.
    What is the done thing for doing this?
     
  2. Offline

    Chinwe

    You can use .toString() on a ChatColor to get ยง[char] which you can then concatenate :)
     
  3. Offline

    Scullyking

    Chinwe Awesome! Thanks :)

    Chinwe

    I'm using this code, but its throwing a NullPointerException error. I'm iterating through all the chars in the char array (which was converted from a string). If theres a & then it looks for the next char and if its a correct number it concats the (ChatColor.COLOUR).toString to a new string. Otherwise it just concats the char.

    Code:java
    1. for (int i = 0; i < message.length; i++){
    2. if (message[i] == '&'){
    3. switch (message[i + 1]){
    4. case 'b': newMessage.concat((ChatColor.AQUA).toString());
    5. case '0': newMessage.concat((ChatColor.BLACK).toString());
    6. case '9': newMessage.concat((ChatColor.BLUE).toString());
    7. case '3': newMessage.concat((ChatColor.DARK_AQUA).toString());
    8. case '1': newMessage.concat((ChatColor.DARK_BLUE).toString());
    9. case '8': newMessage.concat((ChatColor.DARK_GRAY).toString());
    10. case '2': newMessage.concat((ChatColor.DARK_GREEN).toString());
    11. case '5': newMessage.concat((ChatColor.DARK_PURPLE).toString());
    12. case '4': newMessage.concat((ChatColor.DARK_RED).toString());
    13. case '6': newMessage.concat((ChatColor.GOLD).toString());
    14. case '7': newMessage.concat((ChatColor.GRAY).toString());
    15. case 'a': newMessage.concat((ChatColor.GREEN).toString());
    16. case 'd': newMessage.concat((ChatColor.LIGHT_PURPLE).toString());
    17. case 'c': newMessage.concat((ChatColor.RED).toString());
    18. case 'f': newMessage.concat((ChatColor.WHITE).toString());
    19. case 'e': newMessage.concat((ChatColor.YELLOW).toString());
    20. case 'm': newMessage.concat((ChatColor.STRIKETHROUGH).toString());
    21. case 'l': newMessage.concat((ChatColor.BOLD).toString());
    22. case 'k': newMessage.concat((ChatColor.MAGIC).toString());
    23. case 'n': newMessage.concat((ChatColor.UNDERLINE).toString());
    24. case 'o': newMessage.concat((ChatColor.ITALIC).toString());
    25. }
    26. } else {
    27. newMessage.concat(Character.toString(message[i]));
    28. }
    29. }
    30.  
    31. player.sendMessage(newMessage);[/i][/i]


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    Chinwe

    Eeeeh what are you trying to do? Convert all &[char] to a ColorCode? If so, use
    Code:
    String translatedMessage = ChatColor.translateAlternateColorCodes('&', originalMessage);
     
    Scullyking likes this.
  5. Offline

    Scullyking

    Chinwe Well that makes everything a f**kload easier
     
  6. Offline

    Developerjohn

    Scullyking likes this.
Thread Status:
Not open for further replies.

Share This Page