Solved Custom Config

Discussion in 'Plugin Development' started by Failplease, Dec 31, 2013.

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

    Failplease

    How would I go about creating a custom configuration file that lets me add comments?
     
  2. Offline

    tommycake50

    Please expand on your problem, I'm not sure I understand.
    For instance, are you using a code-generated config or a config in the jar?
    Or what you are actually asking?
    Are you asking for a way to add comments any time or just on initial generation?
     
  3. Offline

    PolarCraft

    Failplease Can you please post your code? So that we can further assist you? tommycake50 130 Internets now :D
     
  4. Offline

    jusjus112

    Failplease
    Code:java
    1. File example;
    2. FileConfiguration examplez;
    3.  
    4. public void onEnable(){
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6.  
    7. example = new File(getDataFolder(), "YOURCUSTOMFILE.yml"); // set the file location
    8. examplez = YamlConfiguration.loadConfiguration(example); // this will give you all the functions such as .getInt, getString ect..
    9. saveNewConfig();
    10.  
    11. config.addDefault("hub.message", "Unknown command!. Type '/help' for help."); //You can add custom Booleans and Strings e.t.c
    12. }
    13. public void saveNewConfig(){
    14. try{
    15. examplez.save(example);
    16.  
    17. }catch(Exception e){
    18. e.printStackTrace();
    19. }
    20. }
     
  5. Offline

    Failplease

    In the code-generated config and the jar. I've used bits of jusjus112 's code, I now just need to know how to write in the comments.
     
  6. Offline

    jusjus112

    Failplease
    What do you mean with "comments"
    Do you mean an String? to the config or list?
     
  7. Offline

    Failplease

    jusjus112 I mean like if this was the config:
    Code:
    # If true, then potatoes will happen.
    potato_power: false
    # Setting to false will disable your mom.
    your_robot_mom: true
    The lines beginning with "#" are comments.
     
  8. Offline

    jusjus112

    Failplease
    Did you have tried to set the header for the config?
    Or create an own config with you text?
     
  9. Offline

    Failplease

    jusjus112
    I just want to make it so that you can have "headers anywhere in the config".

    * summons xTigerRebornx *

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

    xTigerRebornx

    Failplease You'd probably need to do something along the lines of your own saving method. I was actually thinking about this a few days ago. Something I thought of is to read what values are changed in the config, then saving the default config over (Which I believe saves the comments in it), and replacing the values with the changed ones.

    EDIT: That, or write your own YAML-Configuration API kinda deal that allows you to do this

    Should submit a ticket or something to Bukkit requesting Comment support in configs :p
     
  11. Offline

    Failplease

    Could you explain what I need to add/modify here in order to save the comments?
    Code:
    public class Config {
       
        File fconfig;
        FileConfiguration config;
     
        public void onEnable() {
     
            fconfig = new File("plugins/ITrollU/config.yml");
            config = YamlConfiguration.loadConfiguration(fconfig);
            saveNewConfig();
            config.addDefault("hub.message", "Unknown command!. Type '/help' for help.");
            config.options().copyDefaults(true);
        }
        public void saveNewConfig() {
            try {
                config.save(fconfig);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
     
  12. Offline

    Squawkers13

    This would be really nice...
     
  13. Offline

    AoH_Ruthless

    Failplease
    Well first of all, don't do this:
    Code:
     fconfig = new File("plugins/ITrollU/config.yml");
    I assume 'ITrollU' is your plugin. What you want to do is get the datafolder. Because the plugins folder is in the server folder (which could be named anything).
    Code:
     fconfig = new File(getDataFolder(), "config.yml")
    Also, config.yml is the default, built in Bukkit Configuration. So you can replace that whole thing (your saveNewConfig method and everything in the onEnable() with:
    saveDefaultConfig()
    Then add the defaults and save the config.
    Also, I see an onEnable but you are not extending JavaPlugin.
     
  14. Offline

    Failplease

    So could you supply me with the new code that you are talking about.
    And my original question still applies; how would I add comments?
     
  15. Offline

    jusjus112

  16. Offline

    BillyGalbreath

    To answer the Op, no. There is no current method provided by Bukkit to save comments in your YAML files. We have been asking them for years now, but they "dont see a point" in comments.
     
  17. Offline

    Failplease

    jusjus112
    Code:
    public class Config extends JavaPlugin {
     
        File fconfig;
        FileConfiguration config;
     
        public void onEnable() {
            fconfig = new File(getDataFolder(), "config.yml");
            config = YamlConfiguration.loadConfiguration(fconfig);
            config.addDefault("pie.is.good",true);
            config.options().copyDefaults(true);
            saveConf();
        }
     
        public void onDisable() {
            reloadConf();
            saveConf();
        }
     
        public void saveConf() {
            try {
                config.save(fconfig);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        public void reloadConf() {
            //reload method
        }
    }
    Another quick question, can someone tell me what to use to reload the config? I see config.load(fconfig), but is there something that can unload it first?

    BillyGalbreath
    I'm not talking about adding comments to Bukkit's config, I want to make my own so that I can add comments.
     
  18. Offline

    AoH_Ruthless

    Failplease
    The only comments you can add from the code is the header. You have to manually add all other comments.
     
  19. Offline

    Failplease

    AoH_Ruthless
    Ok, but will these comments get transferred to the config generated by code when the plugin is enabled?
     
  20. Offline

    AoH_Ruthless

    Failplease
    The header will; if you want other comments you will have to manually generate the config.
     
  21. Offline

    Failplease

    AoH_Ruthless
    But how do other plugins make it so that comments are added without having to manually do it?
     
  22. Offline

    BungeeTheCookie

    Why not just use a save default config method? It's on the bukkit wiki for configuration API section.
     
  23. Offline

    Failplease

    BungeeTheCookie
    So where do I use/put the save default config method? Should I put it here:
    Code:
    public void onEnable() {
            fconfig = new File(getDataFolder(), "config.yml");
            config = YamlConfiguration.loadConfiguration(fconfig);
            config.addDefault("pie.is.good",true);
            //saveDefaultConfig();
            config.options().copyDefaults(true);
            saveConf();
        }
     
  24. Offline

    BungeeTheCookie

    Failplease
    PHP:
    File customConfigFile null;
    File customConfig null;
     
    public 
    void saveDefaultCustomConfig() {
        if (
    customConfigFile == null) {
            
    customConfigFile = new File(getDataFolder(), "customConfig.yml");
        }
        if (!
    customConfigFile.exists()) {           
            
    plugin.saveResource("customConfig.yml"false);
        }
    }
    Main Class:
    Code:java
    1. public void onEnable(){
    2. saveDefaultCustomConfig();
    3. }
     
  25. Offline

    Failplease

    BungeeTheCookie likes this.
  26. Offline

    BungeeTheCookie

    Thanks :D
     
  27. Offline

    Failplease

    BungeeTheCookie
    So wait how would I use the normal config methods for the custom config?
     
  28. Offline

    BillyGalbreath

    Include a default config.yml in your plugin.jar just like you include the plugin.yml file. This config.yml will be copied to the server's hard drive with the saveDefaultConfig() method on first run (it will only copy the file if one doesnt already exist). The comments will get copied over too, exactly how you put them.

    Warning: The comments WILL get deleted if you edit the config using code. Thats why plugins usually have a config.yml thats read-only by the plugin, and other yml files for the plugin to save its data to. saveConfig() will delete any and all comments in the config its saving to.
     
  29. Offline

    Failplease

Thread Status:
Not open for further replies.

Share This Page