saveDefaultConfig for custom YAML files

Discussion in 'Plugin Development' started by alex123099, Jul 24, 2013.

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

    alex123099

    When I do the saveDefaultConfig function, if the .yml file does not exist, it will take it from the jar and create it. This works perfectly for the files that are in the same location as plugin.yml, however, what if I have other .yml files which are in a folder inside the jar, how would I generate the folder with the files if it does not exist?

    this is my saveDefaultConfig function:
    Code:
     public void saveDefaultConfig() {
            if (!configFile.exists()) {
                vpc.saveResource(configFile.getName(), false);
            }
        }
    
     
  2. Offline

    AmShaegar

    saveResource creates the folder automatically. Just use:
    Code:
    vpc.saveResource("path/inside/jar/file.yml", false);
    And it will create the path and file at the exact subfolder of your plugin folder. /server/plugins/PLUGINNAME/path/inside/jar/file.yml
     
  3. Offline

    alex123099

    AmShaegar Thanks, that works, I am now facing a different problem however, whenever it copies the default files, only 1 line is being copied from the file. What I mean is:
    Inside the jar I have the following config:
    Code:
    #Toggle whether automatic mana regeneration is enabled
    mpregen: false
    #Toggle whether updating is allowed
    update: false
    #Set amount of points a player gets per vote
    votegain: 1
    
    But what is being copied to the disk from the jar is:
    Code:
    mpregen: false
    
     
  4. Offline

    AmShaegar

    I assume this to be the result of a configuration in your code that is saved without loading the default config before.
     
  5. Offline

    alex123099

    Here is my class:
    Code:
    public class ConfigHandler {
     
    public static ConfigHandler coreConfigHandler,
    playerDataConfigHandler,
    shopConfigHandler,
    shopItemConfigHandler;
     
    public static ArrayList<ConfigHandler> configHandlers = new ArrayList<ConfigHandler>(); 
     
    private VPCore vpc;
     
    private File configFile;
    private FileConfiguration config;
     
    private String defaultPath;
     
    public ConfigHandler(VPCore vpc, File configFile, String defaultPath){
    this.vpc = vpc;
    this.configFile = configFile;
    this.defaultPath = defaultPath;
    config = YamlConfiguration.loadConfiguration(configFile);
    configHandlers.add(this);
    }
     
    public void reloadConfig() { 
    config = YamlConfiguration.loadConfiguration(configFile);
     
            InputStream defConfigStream = vpc.getResource(configFile.getName());
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
    }
     
    public FileConfiguration getConfig() {
            if (config == null)
                reloadConfig();
            
            return config;
        }
     
    public void saveConfig() {
            if (config == null || configFile == null)
                return;
            else
                try {
                    getConfig().save(configFile);
                } catch (IOException ex) {
                    vpc.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
                }
        }
     
    public void saveDefaultConfig() {
            if (!configFile.exists()) {
                vpc.saveResource(defaultPath, false);
            }
        }
     
    public String getFileName(){
    return configFile.getName();
    }
    }
    
    As you see here:
    Code:
    config = YamlConfiguration.loadConfiguration(configFile);
    
    I am loading the config, unless this is not what you meant.
     
  6. Offline

    AmShaegar

    When you call saveDefaultConfig you ALWAYS have to reload the config.
     
  7. Offline

    alex123099

    I've modified the saveDefault function as follows:
    Code:
    public void saveDefaultConfig() {
            if (!configFile.exists()) {
           reloadConfig();
                vpc.saveResource(defaultPath, false);
            }
        }
    
    reloadConfig function:
    Code:
    public void reloadConfig() { 
    config = YamlConfiguration.loadConfiguration(configFile);
     
            InputStream defConfigStream = vpc.getResource(defaultPath);
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
    }
    
    yet the result is still the same.
     
  8. Offline

    AmShaegar

    Well, that's because you have to reload the config after you restored the defaults from your jar, not before.
     
  9. Offline

    alex123099

    Works now :D thank you for your time.
    Last thing -> It doesn't copy the comments, is there a fix for that?
     
  10. Offline

    AmShaegar

    Afaik, no. When you save the config, all values are saved in their appropriate nodes. The file is written from scratch.
     
Thread Status:
Not open for further replies.

Share This Page