Config loading but not saving

Discussion in 'Plugin Development' started by kkirkfield, Jun 27, 2012.

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

    kkirkfield

    I'm storing defaults in code so if they are missing they get added to the config. Problem is they aren't saving.
    Code:
    boolean test = false;
    @Override
    public void onEnable() {
        test = getConfig().getBoolean("General.Test", test);
        test = !test;
        getConfig().set("General.Test", test);
        saveConfig();
    }
    config.yml
    This loads the test as true, but it gets saved as true, when it should be false. What am I doing wrong?
     
  2. Offline

    hammale

    hmmm not that i doubt you but ive never seen !BOOLEAN before. is test really false?
     
  3. It works just fine for me, I only pasted this:
    Code:
    boolean test = getConfig().getBoolean("General.Test", false);
    test = !test;
    getConfig().set("General.Test", test);
    saveConfig();
    In an empty plugin's onEnable(), started server, config was generated with value as true (mind that it's default false, this should happen), restarted server, value was false, restarted server again, value was true.

    hammale
    The "!" character means negation, it negates an expression, expressions return either true or false and so does a boolean.
     
  4. Offline

    hammale

    yeah i figured i just dont see it used that way very often :) thx for the explanation anyway
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    Thats because most people here are too inexperienced and prefer typing more code to express the same thing
    if (booleanVar == false) versus if (!booleanVar)
    also
    if (booleanVar == true) versus if(booleanVar)

    Pretty much any programmer that knows what they are doing will reduce the boolean expression down to the simplest form which is what I have written on the right of the versus.
     
    ferrybig likes this.
  6. Offline

    hammale

    yeah i normally use it like that in loops but never when defining a variable...thx for the tip :)
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Also be mindful of the word wrap i mean right of the versus
     
  8. Offline

    hammale

    yeah for all the people of the future u may wanna edit that post and make it a bit easier to understand :)
     
  9. Offline

    kkirkfield

    The actual toggle commands for booleans in the config are a bit longer, I just wanted to give a simplified example. I'll post a snippit later, have to go to work now.

    Ok here is a snippit including 2 boolean values that are stored in the config. There are a lot more, but this should be enough to figure out why this isn't working.
    Code:
    public class MyPlugin extends JavaPlugin {
        private boolean autosaveConfig = true;
        boolean debug = false;
     
        @Override
        public void onEnable() {
            try {
                final Date startTime = new Date();
                autosaveConfig = getConfig().getBoolean("General.AutosaveConfig", autosaveConfig);
                debug = getConfig().getBoolean("General.Debug", debug);
                getConfig().set("General.AutosaveConfig", autosaveConfig);
                getConfig().set("General.Debug", debug);
                saveConfig();
                if (debug) {
                    final Date endTime = new Date();
                    final long executionTime = endTime.getTime() - startTime.getTime();
                    getLogger().log(Level.INFO,
                            "onEnable took about " + executionTime + " milliseconds.");
                }
            } catch (final Exception e) {
                getLogger().log(Level.SEVERE, "Error!");
                getLogger().log(Level.SEVERE, "Caused by: " + e.getCause() + "  |  " + e.toString());
            }
        }
     
        @Override
        public boolean onCommand(final CommandSender sender, final Command command, final String label,
                final String[] args) {
            if (command.getName().equalsIgnoreCase("save-config")
                    && sender.hasPermission(command.getPermission())) {
                final Date startTime = new Date();
                if (args.length != 0) {
                    sender.sendMessage("Wrong number of arguments.");
                    return false;
                }
                getConfig().set("General.AutosaveConfig", autosaveConfig);
                getConfig().set("General.Debug", debug);
                saveConfig();
                getLogger().log(Level.INFO, "The config has been saved.");
                if (sender instanceof Player)
                    sender.sendMessage("The config has been saved.");
                if (debug) {
                    final Date endTime = new Date();
                    final long executionTime = endTime.getTime() - startTime.getTime();
                    getLogger().log(Level.INFO,
                            "save-config took about " + executionTime + " milliseconds.");
                }
                return true;
            }
            if (command.getName().equalsIgnoreCase("toggleAutosaveConfig")
                    && sender.hasPermission(command.getPermission())) {
                final Date startTime = new Date();
                if (args.length != 0) {
                    sender.sendMessage("Wrong number of arguments.");
                    return false;
                }
                if (autosaveConfig) {
                    autosaveConfig = false;
                    getLogger().log(Level.INFO, "Autosave of the config has been disabled.");
                    if (sender instanceof Player)
                        sender.sendMessage("Autosave of the config has been disabled.");
                } else {
                    autosaveConfig = true;
                    getLogger().log(Level.INFO, "Autosave of the config has been enabled.");
                    if (sender instanceof Player)
                        sender.sendMessage("Autosave of the config has been enabled.");
                    getConfig().set("General.AutosaveConfig", autosaveConfig);
                    saveConfig();
                }
                if (debug) {
                    final Date endTime = new Date();
                    final long executionTime = endTime.getTime() - startTime.getTime();
                    getLogger().log(Level.INFO,
                            "toggleAutosaveConfig took about " + executionTime + " milliseconds.");
                }
                return true;
            }
            if (command.getName().equalsIgnoreCase("toggleDebug")
                    && sender.hasPermission(command.getPermission())) {
                final Date startTime = new Date();
                if (args.length != 0) {
                    sender.sendMessage("Wrong number of arguments.");
                    return false;
                }
                if (debug) {
                    debug = false;
                    getLogger().log(Level.INFO, "Debug has been disabled.");
                    if (sender instanceof Player)
                        sender.sendMessage("Debug has been disabled.");
                } else {
                    debug = true;
                    getLogger().log(Level.INFO, "Debug has been enabled.");
                    if (sender instanceof Player)
                        sender.sendMessage("Debug has been enabled.");
                }
                if (autosaveConfig == true) {
                    getConfig().set("General.Debug", debug);
                    saveConfig();
                }
                if (debug) {
                    final Date endTime = new Date();
                    final long executionTime = endTime.getTime() - startTime.getTime();
                    getLogger().log(Level.INFO,
                            "toggleDebug took about " + executionTime + " milliseconds.");
                }
                return true;
            }
            return false;
        }
    }
    I still need help on this. Does anyone know why the config is loading the values but not saving them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  10. Because you're not reloading the config :)
    Use reloadConfig() before using getConfig() to get the latest version of it.
     
  11. Offline

    kkirkfield

    Why do i need to reload the config? Since that is the first getConfig() method, the config is stored in instance of JavaPlugin.configFile. All the getConfig methods after that just get the config that was loaded into memory. Since nothing was manually changed in the config.yml while running, there is no need to reload the config into memory.

    If I'm wrong can you please explain. I still don't understand why this isn't working.
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    You should not need to reload the config if you set values in the code. You only need to call reload if you anticipate changes made to the file on disk which you want to pick up, or destroy any unsaved runtime changes.
     
  13. Hmm, actually yes, you don't need reloadConfig() because you're using set() on it, nvm xD

    You should break down your code and debug some values to see what is getting whenever you use a command and stuff.
     
Thread Status:
Not open for further replies.

Share This Page