[&6] Color Support in Config.yml file

Discussion in 'Plugin Development' started by SeanyJo, Sep 19, 2013.

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

    SeanyJo

    i don't know what that means..
     
  2. Offline

    Janmm14

    SeanyJo
    If you post code (java code for example)

    do it like this:
    Code:
    [code]your code...
    your second code line...
    ...[/ code] <-- without space here
     
  3. Offline

    SeanyJo

    uhhh....

    EDIT: oh, i get it now. How would I fix the colors not working?
     
  4. Offline

    1Achmed1

    You should actually use
    Code:
    [syntax=java]Put java code here[/syntax]
    And end up with:
    Code:java
    1. Put java code here
     
  5. Offline

    MrDynamo

    Not trying to be rude, but if you can barely comprehend on how to create syntax boxes and follow the directions multiple people have layed out, then maybe you shouldn't be here.
     
  6. Offline

    Appljuze

    While that does come off as rude, I get where you're coming from and you're right. It's not meant in a mean way, but some people try to dive right into Bukkit without knowing basic Java. I understand how annoying it is, because you "just wanna make some plugins!".

    SeanyJo
    Maybe check out a new youtube series I'm making where I teach Java from the very beginning, but in a way that uses Bukkit. Might help you out without having you feel like you're learning something for nothing.

    SeanyJo
    So to answer your question, your event method should be as follows:
    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent e)
    3. {
    4. e.setMessage(ChatColor.translateAlternateColorCodes('&',e.getMessage()));
    5. }
    6.  
    7. }


    The .translateAlternateColorCodes('&', e.getMessage()) method basically just takes the string that you give it (in this case it's the message the player sent) and translates the "&" symbol in the message and the letter that follows it (one of the color codes like a,b,c,d,etc.) into a ChatColor.Color string.

    So if I have
    Code:java
    1. String myString = "&bHey man!"

    and I do
    Code:java
    1. String myNewString = ChatColor.translateAlternateColorCodes('&', myString)

    it will set myNewString to ChatColor.AQUA + "Hey man!"

    Hope this helps out a bit my friend.

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

    SeanyJo

    it did help, and how would I do it for the config, and on signs as well? For the signs, I know it contains something like:
    Code:java
    1. if lines[I] contains("&")//with i meaning any of the lines, like: for (int i = 0; i <= 3; i++)[/I]
    2. [I]ChatColor.translateAlternateColorCodes("&", lines[I]);[/I][/I]
    3. [I][/I]
    EDIT: I got signs to work, but i'm lost on the config.
     
  8. Offline

    Appljuze

    I'm not quite sure on what you're asking... are you trying to set actual color in a config.yml file?
     
  9. Offline

    SeanyJo

    I'm trying to make it so that when I have a string in the config called "motd", and when I put '&6&lWelcome!',
    it shows up as gold and bold in game, not &6&lWelcome!
     
  10. Offline

    Appljuze

    For the config you just get the string, then use the same method as I described above. I believe the same goes for signs.
     
  11. Offline

    SeanyJo

    The signs work, but I found a bug with the chat: Whenever I do a bold, italic, underline, or magic color code, it does the same to my name. So, if I do &k&lhi, my name is all magic as well as my message. I can't seem to get the config to work, here's my code for the color listener for the config:
    Code:java
    1. public void configColor()
    2. {
    3. if(plugin.getConfig().contains("&")) {
    4. ChatColor.translateAlternateColorCodes('&', "String");
    5. }
    6. }
    7. }

    EDIT: I found an error in the console, here it is:
    http://pastie.org/8350625
     
  12. Offline

    MrDynamo

    All it takes is a simple look at the error to see that '!' is not allowed.
     
  13. Offline

    SeanyJo

    I fixed that by putting the '&9Hi!' around it. But the colors still don't work.
     
  14. Offline

    Janmm14

    SeanyJo
    Change your last posted code to:
    Code:
    public String configColor(String path) {
        return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    }
    To get a colored string from the config, do:
    Code:
    String colored = configColor("thePathInTheConfig");
     
  15. Offline

    SeanyJo

    the String, colored, and configColor are undefined terms, and theres no import for c0nfigColor (in the getting path variable)
     
  16. Offline

    Janmm14

    SeanyJo
    ? Show your code please.
     
  17. Offline

    SeanyJo

    I changed it up a bit, and heres the new code.
    Code:java
    1. player.sendMessage(plugin.getConfig().getString(configColor("prefix")));

    The config is this: http://pastie.org/8353236

    EDIT: In-game, it shows up as a blank line, like the clearing chat plugins.
     
  18. Offline

    Janmm14

    SeanyJo
    Code:
    player.sendMessage(configColor("prefix"));
     
  19. Offline

    beastman3226

    SeanyJo
    Dude, start by learning basic syntax please. Then read the docs. It is annoying how lost you are and we can't explain what you're doing wrong because of your lack of knowledge.

    Make sure you have a config.yml packaged with your jar. Make sure you put apostrophes around the message:
    Code:
    motd: '&cMy message for the the message!'
    In the onEnable() make sure to save the default config.
    Lastly, do:
    Code:java
    1. //in the onCommand method
    2. //1 2 3 4 5 6 7 8
    3. sender.sendMessage(ChatColor.translateColorCodes('&', plugin.getConfig().getString("motd"));

    1: This is the person who sent the command
    2: This is sending a single line message
    3: An enum that contains colors
    4: A function that turns the default color character to 5
    5: The character you want to change to
    6: A method that returns the config.yml
    7: Gets text at the path 8
    8: The path to get from
     
    Janmm14 likes this.
  20. Offline

    CeramicTitan

    Your method, as well as bukkit's translateAlternateColorCodes method, both use regex. They are basically the same
     
  21. Offline

    beastman3226

    Code:java
    1. public String translateColorCodes(char code, String string) {
    2.  
    3. return string.replace('&', '§')
    4. }

    This is what the method looks like. There's no point in making redundant references.
     
  22. Offline

    SeanyJo

    Ive already done all of that.... i',m not THAT stupid, no offense.
     
  23. Offline

    MrDynamo

    If you're wanting colorized config files, then it's not possible afaik.
     
  24. Offline

    SeanyJo

    I didn't want colorized config files.... -_- I wanted them to show up in color in-game. Also, janmm solved it for me.
     
Thread Status:
Not open for further replies.

Share This Page