Solved Chat color replacement

Discussion in 'Plugin Development' started by Pocketkid2, Mar 7, 2015.

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

    Pocketkid2

    Code:
    // Load messages from config
            messages = getConfig().getStringList("messages");
    
            // Replace colors
            for (String s : messages) {
                s.replaceAll("&", String.valueOf(ChatColor.COLOR_CHAR));
            }
    I have this in my plugin, but when I display the messages it doesn't show colors. It only shows the & color codes. Is there anything wrong here?
     
  2. Offline

    Skionz

    @Pocketkid2 Use ChatColor.translateAlternateColorCodes()
     
  3. Offline

    Pocketkid2

    Nope, that doesn't change anything.

    That shouldn't be the problem as I have another plugin that does colors using that same line I had before and it works.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Mar 7, 2015
  4. Offline

    Regablith

    Try
    Code:
    s.replace("&", "ยง");
     
  5. Offline

    Skionz

    @Pocketkid2 Don't replace anything. Use ChatColor.translateAlternateColorCodes(). I know for a fact that it works; you are just implementing it incorrectly.
     
  6. Offline

    Pocketkid2

    Yeah, I think so

    Because this
    Code:
    // Load messages from config
            messages = getConfig().getStringList("messages");
    
            // Replace colors
            for (String s : messages) {
                s = ChatColor.translateAlternateColorCodes('&', s);
            }
    doesn't appear to be working for me

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Mar 7, 2015
  7. Offline

    MexMaster

    Maybe the variable 's' is just a copy of the real string stored in the list?
    You could try creating a new list and then:

    1. for (String s : messages) {
    2. list.add(ChatColor.translateAlternateColorCodes('&', s));
    3. }
    but this is just a thought
     
  8. Offline

    RROD

    The code you have is fine, however the foreach loop does not store the variable. So you'll need to do it like so:

    Code:
    // Convert the List<String> into a String array
    String[] messageArray = messages.toArray(new String[messages.size()]);
    
    // For each over every array item by declaring the index
    for (int i=0; i<messageArray.length; i++) {
        messageArray[i] = ChatColor.translateAlternativeColorCodes('&', messageArray[i]);
    }
    
    // Convert back into a list if it makes life easier for you
    List<String> newMessages = Arrays.asList(messageArray);
    
     
    Last edited: Mar 7, 2015
  9. Offline

    Pocketkid2

    This worked! Thank you @RROD!
     
    RROD likes this.
Thread Status:
Not open for further replies.

Share This Page