Config defaulting when edit

Discussion in 'Plugin Development' started by JarFile, Jan 2, 2015.

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

    JarFile

    When I go into the plugin files on my server and open the config.yml and change things its defaults back to the one in my code when I reload or restart. Anything that people change in there does not matter because it will always reset to default.
    Here is what I have making the config.yml:
    Code:
        public void Config()
        {
            try {
                File file  = new File(getDataFolder(), "config.yml");
                if(!getDataFolder().exists())
                    getDataFolder().mkdirs();
                if(!file.exists())
                {
                    getLogger().info("Config.yml not found, Creating");
                    getConfig().options().copyDefaults(true);
                    saveConfig();
                }
                else
                {
                    getLogger().info("Config.yml Found, Loading");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    And this gets called in console
    onEnable method:
    Code:
        @Override
        public void onEnable()
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            l.info("MiningCrates has been Enabled - Dev: TD6");
            EggPop = getConfig().getBoolean("sounds.eggPop");
            saveConfig();
        }
    
    onDisable Method:
    Code:
        @Override
        public void onDisable()
        {
            l.warning("MiningCrates has been Disabled - Dev: TD6");
            saveConfig();
        }
    
    I don't see the problem. When there isn't a config.yml it copies the defaults, but there is one when I editted it.
     
  2. Offline

    16davidjm6

    so when you enable the plugin, you copy the defaults and save that in your server's RAM.

    when you disable your plugin, you take those defaults in ram and save them over top of any changes you may have made.

    to fix your problem, I would just not save on disable.

    I honestly don't like the config methods and have my own class that can be copied and renamed for creating as saving multiple configs.

    Code:
    import java.io.File;
    import java.io.IOException;
     
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    public class Shop {
        private static YamlConfiguration config;
        private static File f;
      
        public Shop(JavaPlugin p){
            //create a new file
            f = new File(p.getDataFolder().toString() + "/config.yml");
            //if the file does not exist, save the default version of the file
            if(!f.exists()){
                p.saveResource("config.yml", false);
            }
            //load the configuration
            config = YamlConfiguration.loadConfiguration(f);
        }
      
        //allows user to get sections of the configuration
        public static ConfigurationSection get(String path){
            return config.getConfigurationSection(path);
        }
      
        //allws user to get the configuration itself
        public static YamlConfiguration getConfig(){
            return config;
        }
      
        public static void save() throws IOException{
            config.save(f);
            config = YamlConfiguration.loadConfiguration(f);
        }
    }
    
    Because the methods are static, your onEnable method simply needs:

    new Config(this);
     
  3. Offline

    mythbusterma

    @JarFile @16davidjm6

    What are you guys even on about? Honestly, none of that code above is needed.

    All you have to do is invoke JavaPlugin#saveDefaultConfig() in your onEnable() and invoke JavaPlugin#saveConfig() when you're done editing the file.

    You do not need any other code at all, especially code that makes awful static references to ConfigurationSections, where a reference already exists and is created for you.

    Whatever class you just posted is far more awful than the methods the API provides, I can't even fathom why you would think that is better in any way.
     
  4. Offline

    JarFile

    @mythbusterma How exactly would I do that? Sorry I'm a noob with configs...
     
  5. Offline

    mythbusterma

    @JarFile

    What is difficult to understand about that?
     
  6. Offline

    16davidjm6

    @mythbusterma using those methods I have there is actually what the bukkit tutorials suggest you use if using other configs other than the default one the API provides. The code I provided is actually much better than the code provided in the bukkit tutorials and the bukkit API actually also uses static refrences. If it didn't, you would end up with thousands of copies of the config saved in memory. Using it statically results in only 1 reference, unless you want to create the config and then pass it as an argument to every single method its used it.
     
  7. Offline

    _Cookie_

    @16davidjm6 Static is just yucky if it is not needed and in this case it really isn't.


    @JarFile All you really need to do is make a config reload command for your plugin, or just put reloadConfig(); before you saveConfig(); in your on disable, it should stop it from going back to default.
     
  8. if you're using "config.yml" just use the default config.... saveDefaultConfig(); getConfig()...
     
Thread Status:
Not open for further replies.

Share This Page