Config resetting to defaults after reload

Discussion in 'Plugin Development' started by migsthegod, Apr 23, 2012.

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

    migsthegod

    Hi im new to this,
    I have my plugin generate a config but everytime i make changes and reload the server, it reverts to defaults.

    This is what i have:
    Code:
        protected static FileConfiguration            strings = null;
        Logger log;
        File configFile;
        FileConfiguration config;
     
        public void onEnable(){
         
         
            log = this.getLogger();
            log.info("Misc has been enabled!");
         
            configFile = new File(getDataFolder(), "config.yml");
            config = new YamlConfiguration();
            loadYamls();
     
        }
     
        public void onDisable(){
            log.info("Misc has been disabled.");
            saveYamls();
        }
     
        public void saveYamls() {
            try {
                config.save(configFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void loadYamls() {
            try {
                config.load(configFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        private void firstRun() throws Exception {
            if(!configFile.exists()){
                configFile.getParentFile().mkdirs();
                config.options().header("VOTECAST PLUGIN BY DOMINIONSERVER");
                config.addDefault("vote-broadcast", "&c[DMN VOTES] &e@@@ has just voted for the server");
                saveYamls();
            }
        }
    
     
  2. Offline

    arnie231

    Here's what I do

    Values
    Code:java
    1.  
    2. public static File configFile;
    3.  
    4. public static FileConfiguration config;
    5.  


    onEnable
    Code:java
    1.  
    2. configFile = new File(this.directory + File.separator + "config.yml");
    3.  
    4. config = new YamlConfiguration();
    5.  
    6. Files.Load();
    7. // Plugin Enabled Blah Blah Blah
    8.  


    onDisable
    Code:java
    1.  
    2. // Plugin Disabled
    3.  


    Files
    Code:java
    1.  
    2. public class Files {
    3.  
    4. public static void Load() {
    5.  
    6.  
    7. if (!Main.configFile.exists()){
    8. Main.log.info("[Test] Config doesn't Exists!");
    9. createConfig();
    10. Main.log.info("[Test] Config Created!");
    11. }else{
    12. Main.log.info("[Test] Loading Config!");
    13. }
    14.  
    15.  
    16. private static void createConfig() {
    17. Main.config.options().header("Test.");
    18. Main.config.addDefault("Test WorldName", "world");
    19. Main.config.options().copyDefaults(true);
    20. saveconfig();
    21. }
    22. private static void saveconfig() {
    23. try{
    24. Main.config.save(Main.configFile);
    25. } catch (Exception e) {
    26. }
    27. }
    28.  


    If I then Edit the Config in any way I use the Save method after I've done editing it, hope this helps
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    migsthegod shut down the server first before you manually edit the yaml file, else when the server calls onDisable when it reloads it will write what is to memory to disk.
     
  4. Offline

    migsthegod

    I see, thanks man that works!
    I've been doing that too but it'll be harder to explain to other people so i was looking for another way :D

    Awesome, this works even with just a reload.
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    migsthegod
    coincidently arnie gave you a solution which does not call saveConfig() in on disable, that was the other option on your part ;)
     
  6. Offline

    Steffion

    PHP:
    if (args[0].equalsIgnoreCase("reload")) {
                        if (
    player == null) {
                            
    reloadConfig();
                            
    loadConfig();
                            
    log.info("Reload succesful.");
                            return 
    true;
                        } else if (
    player.hasPermission("permission.node")){
                            
    reloadConfig();
                            
    loadConfig();
                            
    player.sendMessage("§6Reload succesful.");
                            return 
    true;
                        } else {
                            
    player.sendMessage("§cYou have no permission to do that!");
                            return 
    true;
                        }

    Something Like this?
     
Thread Status:
Not open for further replies.

Share This Page