Solved How to make a config file change ChatColor of text.

Discussion in 'Plugin Development' started by OptimalBread, May 2, 2014.

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


    So I am new to configuration files and pulling data from them. I want people in the config file to type a color like white, black, blue and it will add it to the chatcolor. Here is my code for the generating the config on the onEnable(){}
    1. File file = new File(getDataFolder() + File.separator + "config.yml");
    2. if (!file.exists()){
    3. this.getConfig().addDefault("ChatColor", "White");
    4. this.getConfig().options().copyDefaults(true);
    5. this.saveConfig();
    6. }

    Ok we got that but now here is my ChatColor code \
    1. (ChatColor. + this.getConfig().getString("ChatColor"))

    Any help would be great thanks.
  2. Offline


    First off, your onEnable() method can be much simpler! Simply add:
    1. this.saveDefaultConfig();

    to it, and it will do the folder and file loading for you, with a single line!

    Also, one possible way is to do the following:
    1. (ChatColor. + this.getConfig().getString("ChatColor"));

    You could put this into a message like this:
    1. p.sendMessage(ChatColor. + this.getConfig().getString("ChatColor") + "Hello, I'm a message!");

    It's EXTREMELY important that you configure your config.yml correctly if you do this, however. You MUST use a VALID color code, and it must be all caps. Like this example:
    ChatColor: 'LIGHT_BLUE'
    This would set it to ChatColor.LIGHT_BLUE.
  3. Offline


    I tried that but on .getConfig() it says method is undefined and for ChatColor. + this. says No enclosing instance of the type ChatColor is accessible in scope
  4. Offline


    You need to add a "" to make it a string.
    1. (ChatColor. + "" + this.getConfig().getString("ChatColor"));

    Sorry I forgot that.
    This also goes for in a message:
    1. (ChatColor. + "" + this.getConfig().getString("ChatColor") + "Hello!");

    Note there's an alternate, easier way to do this with .translateAlternateColorCodes(), although I am not experienced in using this method.
  5. Offline


    The . after ChatColor. is underlined in red and won't run the event properly.
    Sorry for being a pain

    I got it

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


    Nice, how'd you get it? I'm actually quite interested.
  7. Offline


    ChatColor. is not a valid enum value, you have to do ChatColor.valueOf
Thread Status:
Not open for further replies.

Share This Page