How do you keep a config.yml with comments?

Discussion in 'Plugin Development' started by OTF Catastrophe, Jul 1, 2016.

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

    OTF Catastrophe

    I've been searching all over Google trying to find a way to make my config.yml file stay exactly how it is from when it copies itself over.

    The only type of answer I've found was creating like a FileWriter or using a different type of API to keep all the comments and everything the same inside the file. It's just all confusing to me, would anybody be able to attempt to help me out on how to keep comments and all the spacing the same?
     
  2. Offline

    I Al Istannen

    @OTF Catastrophe
    This is written from what I believe to be true:

    The bukkit yaml implentation only respects the header. That's it. Nothing for you to do.
    You would need to use a different yaml implementation or some String vodoo with replacing only the changed parts, which I would guess would be a nightmare to do.

    I have often wondered the same, and this is what I read everywhere and found to be true.

    If anybody has a solution however, I would be extremly glad if you post it!
     
  3. Offline

    OTF Catastrophe

    A ton of people would be glad to have that answered aha. I see plugins like Essentials and like Clearlag with these nice configuration files all spaced out with comments and I feel like I need it aha.

    @timtower any clue on how to do something like this? Or do you possibly have any references or could tag anyone who knows how to do so?
     
  4. Offline

    Jakeeeee

    Define the file in eclipse, create a method that gets the "resource-file" (bukkit has already implemented one #saveDefaultConfig(), for other files look at this page), and it will load in the file that you created in eclipse exactly how you written it.
     
  5. Offline

    OTF Catastrophe

    I've tried using #saveDefaultConfig() and I currently still have it in the code, but when something alters the config.yml file like an updated string or added anything, the file reverts back to its messed up state. I'm looking for the way to create it, and being able to edit it so it stays the same formatting no matter what. Thank you for your help though :)

    Also I'm having some more issues if anyone doesn't mind helping. I'm trying to use ArrayList and getStringList to get a String list under the name Arenas(This being just arena names) and arenaData(This being all the info the plugin needs to set the player in the arena.)

    What the part in config is supposed to look like is this:

    Code:
    //This is by default in the internal config,
    Arenas:
    arenaData:
    
    //When a player runs commands that alter it, such as /command create (Arena). I want it to be:
    Arenas:
    - Arena
    Arena:
        x:
        y:
        z::
        pitch:
        yaw:
        world:
        resetLevel:(This being accessed by another command,)
    But when I try to use:
    Code:
        public static ArrayList<String> arenas = new ArrayList<String>();
    Code:
     arenas = (ArrayList)getConfig().getStringList("arenaData");
    
                     if (tpcmd.equalsIgnoreCase("create"))
                     {
                      
                         List<String> arenaNames = getConfig().getStringList("Arenas");
                      
                         if ((player.hasPermission("pk.create")) || (player.hasPermission("pk.*")))
                         {
                          
                             if (args.length == 2)
                             {
                              
                                 if (!arenaNames.contains(args[1]))
                                 {
                                  
                                     arenaNames.add(args[1]);
                                  
                                     Location tmp = player.getLocation();
                                  
                                     arenas.add(args[1]);
                                                                   
                                     config.set("Arena." + args[1] + ".x", Double.valueOf(tmp.getX()));
                                     config.set("Arena." + args[1] + ".y", Double.valueOf(tmp.getY()));
                                     config.set("Arena." + args[1] + ".z", Double.valueOf(tmp.getZ()));
                                     config.set("Arena." + args[1] + ".yaw", Float.valueOf(tmp.getYaw()));
                                     config.set("Arena." + args[1] + ".pitch", Float.valueOf(tmp.getPitch()));
                                     config.set("Arena." + args[1] + ".world", tmp.getWorld().getName());
                              
                                     saveDefaultConfig();
                              
                                     player.sendMessage(prefix.replace("&", "§") + "§aArena " + args[1] + " has been created!");
                                  
                                 }
                              
                                 else
                                 {
                                  
                                     player.sendMessage(prefix.replace("&", "§") + "§cArena " + args[0] + " has already been created!");
                                  
                                 }
                              
                             }
                          
                             else
                             {
                              
                                 player.sendMessage(prefix.replace("&", "§") + "§cMissing arguments! Correct usage /pk create {mapName}");
                              
                             }
                          
                         }
                      
                         else
                         {
                          
                             player.sendMessage(prefix.replace("&", "§") + config.getString("Messages.noPermissionMessage").replace("&", "§"));
                          
                         }
                      
                         return true;
                      
                     }
    It removed EVERYTHING from config except for the Arenas and arenaData String parents and I literally have no clue why. Does anyone understand and or could help with this?
     
    Last edited: Jul 1, 2016
  6. Offline

    Jakeeeee

    Comments in the config are not // they are #.
     
  7. Offline

    OTF Catastrophe

    I'm aware of this. My comment issues and code issues are separate. I was just using // in the code because it looks better and that's how you would put random text in your code that was meant to be read regularly.
     
  8. Online

    timtower Moderator Moderator

    @OTF Catastrophe I use a very different method for my configs, I have no idea.
    I generally just add a readme file.
     
  9. Offline

    mythbusterma

    @OTF Catastrophe

    Basically, if you have a config that your plugin never writes to, this is quite simple, you just write the file to disk and use it like you normally would for your plugin.

    If your plugin actually writes to this file (which, in general, you shouldn't have one that is meant for user configuration and that the plugin writes to), then you'll need to do something else.
     
  10. Offline

    OTF Catastrophe

    So would a definition of "Never writes to" be something along the lines of changes being changed from in the plugin itself? In that case using commands that would add and remove or even get strings would be fine right? Sorry if I sound a little confused, I understand most of what's being discussed I just dont fully understand configuration files yet.
     
  11. Offline

    I Al Istannen

    @OTF Catastrophe
    If you only read from a file (get), all is well. If you ever call the FileConfiguration#save() method, the comments will be lost.

    Mythbusterma said you should have one config only edited by the user (config, you know ;)) and another file where you read and write to. This other file won't have comments, only a header if you wish.
     
Thread Status:
Not open for further replies.

Share This Page