Solved Config Issues - PLEASE help so I can release updated TreasureHunt

Discussion in 'Plugin Development' started by Taien, Feb 14, 2013.

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

    Taien

    So I've finally moved most of my plugins over to this new Config system that was introduced a while back, and I'm having some trouble, most notably with TreasureHunt. For some reason, I can't seem to get the plugin to load values from the config - it seems to ALWAYS load from the default config (within the jar). Maybe you guys who have more experience with this new API can shed some light on my problem?

    Here's the important bits of the loading code, which is run on command or on plugin load...

    Code:
    public void loadProcedure() {
     
    Config messages = new Config(this,"messages.yml");
    Config config = new Config(this,"config.yml");
    Config players = new Config(this,"players.yml");
     
    //then I load all the data stuffs into variables
    //which is like 300 lines, then...
     
    config.saveDefaultConfig();
    players.saveDefaultConfig();
    messages.saveDefaultConfig();
     
    }        
    My saving code is ok, because if I modify the values while the plugin is running (using commands) and then save, the config files reflect the changes just fine.

    Here's my Config class...pretty much copy and pasted from the example one with a few modifications I made since it wasn't working correctly, and still isn't....

    Code:
    public class Config {
     
        private final String fileName;
        private final JavaPlugin plugin;
       
        private File configFile;
        private FileConfiguration fileConfiguration;
     
        public Config(JavaPlugin plugin, String fileName) {
            if (plugin == null)
                throw new IllegalArgumentException("plugin cannot be null");
            if (!plugin.isInitialized())
                throw new IllegalArgumentException("plugin must be initialized");
            this.plugin = plugin;
            this.fileName = fileName;
        }
     
        public void loadConfig() {
            if (configFile == null) {
                File dataFolder = plugin.getDataFolder();
                if (dataFolder == null)
                    throw new IllegalStateException();
                configFile = new File(dataFolder, fileName);
            }
            if (configFile.exists()) fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
            else
            {
                InputStream defConfigStream = plugin.getResource(fileName);
                fileConfiguration = YamlConfiguration.loadConfiguration(defConfigStream);
            }
        }
     
        public FileConfiguration getConfig() {
            if (fileConfiguration == null) {
                this.loadConfig();
            }
            return fileConfiguration;
        }
     
        public void saveConfig() {
            if (fileConfiguration == null || configFile == null) {
                return;
            } else {
                try {
                    getConfig().save(configFile);
                } catch (IOException ex) {
                    plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
                }
            }
        }
       
        public void saveDefaultConfig() {
            if (!configFile.exists()) {           
                this.plugin.saveResource(fileName, false);
            }
        }
     
    }
    I could really use some help here, as my plugin's update is completely ready except for this bit of crap. I am completely lost as to why it's not loading the config files correctly...
     
  2. Offline

    keensta

    Code:
    public void loadProcedure() {
     
    Config messages = new Config(this,"messages.yml");
    Config config = new Config(this,"config.yml");
    Config players = new Config(this,"players.yml");
     
    //then I load all the data stuffs into variables
    //which is like 300 lines, then...
     
    config.saveDefaultConfig();
    players.saveDefaultConfig();
    messages.saveDefaultConfig();
     
    }        
    So with your code above Im taking it you then do something like

    FileConfigration PlyCfg = players.loadConfig();

    PlyCfg.set("bla", bla);

    PlyCrg.saveConfig();

    Because if you don't do .saveConfig(); that could be your problem its all great saving the default but isn't that just saving it back into the plugin and not acctully saving it into the FileLocation? Im not sure as I don't use saveResource(file, boolean); I write my own Method for config handling.
     
  3. Offline

    Taien

    Well essentially:

    What IS happening:
    On FIRST plugin load, plugin loads default values from the config.yml included in TreasureHunt.jar. Then the plugin saves config file if it doesn't already exist.
    On shutdown, plugin saves all current values to the configs and then saves the files. I can confirm that these changes are getting saved to the files.
    On FOLLOWING plugin loads, plugin loads the default values from the config.yml in the jar again for some reason, even though it should be loading from the saved files.

    What SHOULD be happening:
    On loads after the first one, it should load from the saved file instead of the default values included in the jar. According to my reading of the code, it SHOULD be doing this as it currently stands, and this config setup is almost exactly the same as the one on the Config YAML tutorial in the bukkit wiki.

    I'm utterly confused and quite dismayed at this, because it is all that is holding back a much improved and majorly upgraded version of TreasureHunt from release. :(

    Nevermind, I figured it out. And it had nothing to do with the code I had posted here. It was a collaboration between a typo on my part and the normal behavior of the plugin when it doesn't detect any world configurations. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page