Solved Config not updating properly?

Discussion in 'Plugin Development' started by 9903286, Aug 12, 2013.

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

    9903286

    Hi, in my plugin i'm using a custom config that uses FileConfiguration to set, load and save stuff to a file.
    Now, the problem is:
    If i set something and then save it, the config file will be updated, but if i do it any more times it won't save to the file.
    If i get it to print the value from the file that it's supposed to save to, it gives me the correct value, but if i then were to reload it would go back to the value that it set the first time last session.
    I have no idea what's causing this, i get no errors or anything in the console.
    Tell me if you need any classes or methods and i'll send them, right now i do not know what to send.
     
  2. Offline

    deery50

    9903286
    Can you show us your config save method? Well...at least I hope you have a method for it. It would be something like this except changed to fit your config file name, etc.:
    Code:java
    1. public boolean save() {
    2. try {
    3. config.save(configFile);
    4. Messenger.debug("Saved Config");
    5. } catch (Exception e) {
    6. Messenger.severe("Config Failed to save, returned error:\n" + e.getMessage());
    7. }
    8. return true;
    9. }


    Then you could easily access it with save();
     
    9903286 likes this.
  3. Offline

    9903286


    Indeed i do, and here it is:
    Code:
        public static void save() {
            if (main.getDataFolder().exists()) {
                try {
                    main.getDataFolder().createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
               
                config.save(file);
               
            } catch (Exception e) {
                e.printStackTrace();
            }
           
            config = YamlConfiguration.loadConfiguration(file);
        }
     
  4. Offline

    deery50

    9903286
    I wouldn't recommend putting all of that in your save method. I would make a separate method that is called when the plugin loads like this:
    Code:java
    1. public boolean load() {
    2. try {
    3. if (!configFile.exists()) {
    4. configFile.createNewFile();
    5. getdefaults();
    6. }
    7. config.load(configFile);
    8. return true;
    9. }
    10. catch (Exception e) {
    11. Messenger.severe("Config Failed to load, returned error:\n" + e.getMessage());
    12. return false;
    13. }
    14. }


    Then you can have your save method like I had above and receive a message when it fails to save. As for your problem, can you post an example code snippet of the usage of the method?
     
    9903286 likes this.
  5. Offline

    9903286

    deery50
    The console says "Config Failed to load, returned error: null".
    And example code snippet: (I think it is)
    Code:
        public static void setKills(String playerName, String kills) {
            PlayerConfig.config.set(playerName+".kills", kills);
            PlayerConfig.save();
        }
     
  6. Offline

    deery50

    9903286
    That means that the config is null, make sure you identify the file correctly. This is an example of how I would load the config from my main plugin script when the plugin is enabled.
    Code:java
    1. public void loadconfig() {
    2. File dir = this.getDataFolder();
    3. if (!dir.exists()) dir.mkdir();
    4. File file = new File(this.getDataFolder(), "config.yml");
    5. config = new Config(file);
    6. if (!config.load()) {
    7. this.getServer().getPluginManager().disablePlugin(this);
    8. throw new IllegalStateException("The config-file was not loaded correctly!");
    9. }
    10. }

    This all relates to a separate script that deals with everything to do with configs, here is my full script you are welcome to build on: http://pastebin.com/Qzy2dDZX

    Then on the top of my Main plugin script I identified the config script with this: public static Config config = null;
    and on the very first line of the OnEnable simple call the loadconfig method with: loadconfig();

    Then as long as you pass that same config instance to all of the scripts needed to use it you can access it with config.getConfig().setString...etc.
     
    9903286 likes this.
  7. Offline

    9903286

    deery50
    Sorry for the wait, had to to some minor redecorating in my plugin to add your solution.
    Using the code from your Config.java file (http://pastebin.com/Qzy2dDZX) worked prefectly.
    Thanks for your help. :D
     
  8. Offline

    deery50

    9903286
    No problem Glad I could help! You can also add methods in your config file sort of like this one to make it easier for you:
    Code:java
    1. public String getstring(String path){
    2. return config.getString(path);
    3. }

    you can do the same to set strings, set booleans, set ints, get strings, get booleans, etc.
     
    9903286 likes this.
Thread Status:
Not open for further replies.

Share This Page