Config Changes not Saving

Discussion in 'Plugin Development' started by TheNewTao, Dec 25, 2015.

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

    TheNewTao

    Hi,

    I am having a problem that when I change a value from my config, to be more specific I am changing "cooldown-time" to a different value. It does not change, it stays as the default, which is 20.

    In other words, the problem is that when I the value of cooldown-time in the config.yml and reload the server, it goes back to the default.

    Here is my onEnable()
    Code:
        @Override
        public void onEnable() {
            if (!new File(getDataFolder(), "config.yml").exists()) {
                saveDefaultConfig();
            }
    
            for (Player p : Bukkit.getOnlinePlayers()) {
                toggleFire.put(p.getUniqueId(), false);
                toggleStatus.put(p.getUniqueId(), false);
                currentAura.put(p.getUniqueId(), "none");
            }
            this.getCommand("fire").setExecutor(new AbilityCmd(this));
            this.getCommand("angel").setExecutor(new AbilityCmd(this));
            this.getCommand("evilcrown").setExecutor(new AbilityCmd(this));
            this.getCommand("crown").setExecutor(new AbilityCmd(this));
            this.getCommand("ender").setExecutor(new AbilityCmd(this));
            this.getCommand("cloud").setExecutor(new AbilityCmd(this));
            this.getCommand("smoke").setExecutor(new AbilityCmd(this));
            this.getCommand("spark").setExecutor(new AbilityCmd(this));
            this.getCommand("lava").setExecutor(new AbilityCmd(this));
            this.getCommand("love").setExecutor(new AbilityCmd(this));
            this.getCommand("leaf").setExecutor(new AbilityCmd(this));
            this.getCommand("water").setExecutor(new AbilityCmd(this));
            this.getCommand("magic").setExecutor(new AbilityCmd(this));
            this.getCommand("auramenu").setExecutor(new SelectorCmd(menu, this));
            this.getCommand("knowledge").setExecutor(new AbilityCmd(this));
            this.getCommand("aurastaff").setExecutor(new AdministrationCmd(this, new StaffMenu(this)));
            Bukkit.getPluginManager().registerEvents(new PlayerLoginEvents(this), this);
            Bukkit.getPluginManager().registerEvents(new ClickEvent(this, new RemoveCooldownMenu(), new AbilityMenu()),
                    this);
           
            List<String> cooldowns = getConfig().getStringList("cooldowns");
            for (String s : cooldowns) {
                String[] split = s.split(":");
                UUID player = UUID.fromString(split[0]);
                Long time = Long.parseLong(split[1]);
                cooldown.put(player, time);
            }
            cooldownTime = getConfig().getInt("cooldown-time");
       
        }
    I think my problem is here because when I remove this part of the code it does save the changes of cooldown-time:
    Code:
            List<String> cooldowns = getConfig().getStringList("cooldowns");
            for (String s : cooldowns) {
                String[] split = s.split(":");
                UUID player = UUID.fromString(split[0]);
                Long time = Long.parseLong(split[1]);
                cooldown.put(player, time);
            }
    //cooldownTime is an instance variable
            cooldownTime = getConfig().getInt("cooldown-time");
    This problem is driving me insane, would appreciate if anyone knows a solution.
    Thanks.
     
  2. Offline

    BaconStripzMan

    Get rid of this line:
    Code:
    if (!new File(getDataFolder(), "config.yml").exists()) {
    saveDefaultConfig(); does that for you.
    And make sure when you edit the config do saveConfig();
     
  3. Offline

    TheNewTao

    @BaconStripzMan

    Already tried that, still not working. And how would I saveConfig() if I am editing it from the FTP files.
     
  4. Offline

    Xerox262

    What are you talking about? You mean you uploaded the plugin with FTP? Or you're actually connecting to the ftp server with your plugin?

    Edit: Woops, silly me, if you're going to edit from the FTP then do it while the server is off, if you call saveConfig(); then it will overwrite any changes you make.

    @BaconStripzMan I hope you mean saveConfig(); in the disable method, because saving it everytime you change it is so inefficient.

    But yea, no need to check if the config file exists, saveDefaultConfig(); will only save the config if there isn't already one there.
     
  5. Offline

    TheNewTao

    @Xerox262

    What I mean is that I go to the config.yml through the FTP files and edit it like that. When I reload the server, it does not change. It goes back to what it was.

    So what happens is:

    I load the plugin.
    Go to the config.yml
    Change the cooldown value from 20 to 200, 20 is the default
    Reload the server
    The cooldown is 20, while it should be 200

    However, when I delete this part of the code, 200 stays.
    Code:
            List<String> cooldowns = getConfig().getStringList("cooldowns");
            for (String s : cooldowns) {
                String[] split = s.split(":");
                UUID player = UUID.fromString(split[0]);
                Long time = Long.parseLong(split[1]);
                cooldown.put(player, time);
            }
    //cooldownTime is an instance variable
            cooldownTime = getConfig().getInt("cooldown-time");
    Anyone any idea why?
     
  6. Offline

    Xerox262

    Because you're not supposed to edit it while the server is still running, you have to stop it, edit it then restart the server. Or you should remove the saveConfig(); from your onDisable, if you're not editing the config while in game then there's no need to have it there.
     
  7. Offline

    TheNewTao

    @Xerox262

    But I need the saveConfig() on my onDisable
    Code:
    //onDisable()
            List<String> times = getConfig().getStringList("times");
            if (!cooldown.isEmpty()) {
                for (UUID id : cooldown.keySet()) {
                    long currentTime = cooldown.get(id);
                    String playerId = id.toString();
                    String idTime = playerId + ":" + Long.toString(currentTime);
                    times.add(idTime);
                }
            }
            getConfig().set("times", times);
            saveConfig();
    I am saving the HashMap when disabled.
     
  8. Offline

    Xerox262

    And like I said, don't edit it while it's still enabled, it's just going to overwrite it when it disables.

    Stop the server, edit it, then start the server and see what happens.
     
  9. Offline

    TheNewTao

    @Xerox262

    That works, but how do people do it? like allowing people to modify the config.yml and just reloading to change the value, instead of having to restart the whole server
     
  10. Offline

    Xerox262

    Create a command, like group manager, they have commands like mansave and manload, mansave saves the file as it currently stands, and manload reloads all the data again.

    Like put all your config loading in a method of it's own, call it in onEnable, then call it if they do the command.
    Same for saving.
     
Thread Status:
Not open for further replies.

Share This Page