Solved Broadcasting from Config with color codes

Discussion in 'Plugin Development' started by StaticE, Apr 6, 2013.

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

    StaticE

    Hi everyone,

    In part of my plugin I am working on I need to be able to broadcast a message that is customized in the config file, with support of color codes such as &4, etc. For example, an entry in the config file could look like this:

    Code:
    msg: &4 This is a colored message!
    I have tried this:
    Code:
    bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("msg"));
    but it does not work. (No error, just does not show up in color). Thanks:)
     
  2. Offline

    L33m4n123

    No idea if it works. But try §4 and remove the Chat.Color.translateAlternateColorCodes.. Since for me, whenever I save a yml file with & in it, it gets removed.. But § stays and then takes the color.. (if you get what I mean)
     
  3. Offline

    MrSparkzz

    SE Plugins
    For starters you need to wrap the message in quotes or else it'll return an error.
    Code:yaml
    1.  
    2. msg: '&4This is a colored message!'
    3.  

    And as for the color codes just do this.
    Code:java
    1.  
    2. bukkit.broadcast(this.getConfig().getString("msg").replaceAll("&", "\u00A7"));
    3.  
     
  4. Offline

    HelGod

    Just do something like this.
    msg = this.getConfig().replace("&", "§")
    I am on my iphone so this might not be the exact code
     
  5. Offline

    MrSparkzz

    L33m4n123

    To fix that & removal problem replace in your onEnable() method
    Code:java
    1.  
    2. getConfig().options().copyDefaults(true);
    3. saveConfig();
    4.  

    with
    Code:java
    1.  
    2. saveDefaultConfig();
    3.  


    It will fix that problem and it will allow you to add comments (#This is a comment for yaml)
     
  6. Offline

    L33m4n123

  7. Offline

    StaticE

    The code you posted above didn't work, I got an error in the console that pointed to the & symbol in the config.
    This actually worked, but before the message there is a weird A symbol that I can't get rid of...not sure what it means.
     
  8. Offline

    MrSparkzz

    SE Plugins
    The reason you got an error is because you can't start a string with "&" you need to put parenthesis around the string for example

    Code:yaml
    1.  
    2. msg: '&4This is a colored message!'
    3.  
     
  9. Offline

    StaticE

    It's this letter/symbol thing that shows up inf ront of the color message: Â

    Oh ok, I'll try that.

    You're right, forgot to put the quotes. Thanks! Solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    EvilKittyCat123

    Sorry for reviving this old thread, but I have the same problem. I get that weird A symbol. And if I try to do '&4This is a colored message' it will just appear as "This is a colored message" without any colors. Please help?
     
Thread Status:
Not open for further replies.

Share This Page