Solved Config resetting... have no clue why

Discussion in 'Plugin Development' started by HeyAwesomePeople, Jan 1, 2015.

Thread Status:
Not open for further replies.
  1. Hello there!

    I have a fairly noobish problem.... config resetting on reload. Here is my code;

    Main Class

    Code:
    package me.HeyAwesomePeople.performancemonitor;
    
    import java.io.File;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PerformanceMonitor extends JavaPlugin implements Listener {
        public static PerformanceMonitor instance;
       
        public Config config;
       
        @Override
        public void onEnable() {
            instance = this;
           
            if (!new File(getDataFolder(), "config.yml").exists()) {
                saveDefaultConfig();
            }
           
            config = new Config();
        }
    
        @Override
        public void onDisable() {
            saveConfig();
        }
    }
    
    Config.java class:
    Code:
    package me.HeyAwesomePeople.performancemonitor;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Config {
        public PerformanceMonitor plugin = PerformanceMonitor.instance;
    
        public Boolean timemonitor;
        public Boolean memorymonitor;
        public Boolean diskmonitor;
        public Boolean backupmonitor;
        public String backupsfolder;
        public Boolean worldmonitor;
        public List<String> monitoredworlds = new ArrayList<String>();
        public Boolean playermonitor;
        public Boolean servermonitor;
       
        public Config() {
            timemonitor = plugin.getConfig().getBoolean("monitors.timeMonitor.enabled");
            memorymonitor = plugin.getConfig().getBoolean("monitors.memoryMonitor.enabled");
            diskmonitor = plugin.getConfig().getBoolean("monitors.diskMonitor.enabled");
            backupmonitor = plugin.getConfig().getBoolean("monitors.backupMonitor.enabled");
            backupsfolder = plugin.getConfig().getString("monitors.backupMonitor.backupFolder");
            worldmonitor = plugin.getConfig().getBoolean("monitors.worldMonitor.enabled");
            monitoredworlds = plugin.getConfig().getStringList("monitors.worldMonitor.worlds");
            playermonitor = plugin.getConfig().getBoolean("monitors.playerMonitor.enabled");
            servermonitor = plugin.getConfig().getBoolean("monitors.serverMonitor.enabled");
        }
       
    }
    
    Default config:
    Code:
    monitors:
      timeMonitor:
        enabled: true
      memoryMonitor:
        enabled: true
      diskMonitor:
        enabled: true
      backupMonitor:
        enabled: true
        backupFolder: backups
      worldMonitor:
        enabled: true
        worlds:
        - world
        - world_nether
      playerMonitor:
        enabled: true
      serverMonitor:
        enabled: true

    Here is my issue: whenever I reload the server after editing that config, it reverts to what it was. For example, I start up the server with the default config. I edit the backups folder and change it to "backup". I reload the server and BOOM, it resets to backups. Now, the only reason why this is so hard for me to understand why this is happening is because if you note out "config = new Config();", and never create the Config() class instance, this problem never occurs. What is going on here?! I have tested with Craftbukkit 1.7.9 and 1.8 R0.1.

    Thanks,
    HeyAwesomePeople
     
  2. Offline

    mythbusterma

    @HeyAwesomePeople

    Why do you maintain an instance of the config yourself...you can just use JavaPlugin#saveDefaultConfig() when the pluign is enabled, and then JavaPlugin#saveConfig() when you're done editing. That is all you have to do.
     
  3. I don't. The Config class just stores variables from the config for quicker access. Yes, I know the speeds of getConfig and using hashed data are both very fast, but I like to make things run the smoothest and fastest I can.

    EDIT: That question also doesn't explain my issue at all.
     
  4. Offline

    Skionz

    Don't save it onDisable.
     
    HeyAwesomePeople likes this.
  5. Offline

    mythbusterma

    @HeyAwesomePeople

    Please stop, you have absolutely no idea what you're talking about at all, stop before you make yourself look like even more of an idiot.

    Yea, it actually does, don't be so presumptuous. You're just using the API incorrectly, that's why it's not working.
     
  6. Well damn. Guess I should quit coding because I don't understand something. Cya guys!

    Thank you for your help.
     
Thread Status:
Not open for further replies.

Share This Page