Solved Config reload command help

Discussion in 'Plugin Development' started by xYourFreindx, May 22, 2014.

Thread Status:
Not open for further replies.
  1. Hey again...
    I've been at this for quite a while now, and I can't seem to get my reload command to work.

    This is my @onEnable
    (It works fine) ​
    Code:java
    1.  
    2. @Override
    3. public void onEnable(){
    4. getConfig().options().copyDefaults(true);
    5. saveConfig();
    6. getServer().getPluginManager().registerEvents(this, this);
    7. if (getConfig().getBoolean("CameraMode.enabled") == (false)) {
    8. getLogger().info("Plugin Disable Setting Detected...");
    9. getServer().getPluginManager().disablePlugin(this);
    10. }else{
    11. PluginDescriptionFile pdfFile = this.getDescription();
    12. getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled");
    13. }
    14. }


    When the plugin is starting up, all of the stuff in the config file stays as it was before the server was started up, but when my players edit the config file, then do /cameramode reload, it says "config reloaded" and they look at the config, and everything they just edited has returned to it's previous value. So the only way to edit the config file is to turn off the server, edit it, and turn it back on.

    I've tried....​
    Code:java
    1.  
    2. }else if (args.length == 1) {
    3. if (args[0].equalsIgnoreCase("reload")) {
    4. if (sender instanceof Player) {
    5. if (sender.hasPermission("cameramode.reload")) {
    6. this.saveConfig();
    7. this.reloadConfig();
    8. sender.sendMessage(ChatColor.GREEN + "Config Reloaded");
    9. }else{
    10. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    11. }
    12. }else{
    13. this.saveConfig();
    14. this.reloadConfig();
    15. getLogger().info("Config reloaded");
    16. if (getConfig().getBoolean("CameraMode.enabled") == (false)) {
    17. getLogger().info("Plugin Disable Setting Detected...");
    18. getServer().getPluginManager().disablePlugin(this);
    19. }
    20. [CENTER] }[/CENTER]


    This....​
    Code:java
    1.  
    2. }else if (args.length == 1) {
    3. if (args[0].equalsIgnoreCase("reload")) {
    4. if (sender instanceof Player) {
    5. if (sender.hasPermission("cameramode.reload")) {
    6. saveConfig();
    7. reloadConfig();
    8. sender.sendMessage(ChatColor.GREEN + "Config Reloaded");
    9. }else{
    10. sender.sendMessage(ChatColor.RED + "You do not have permission!");
    11. }
    12. }else{
    13. saveConfig();
    14. reloadConfig();
    15. getLogger().info("Config reloaded");
    16. if (getConfig().getBoolean("CameraMode.enabled") == (false)) {
    17. getLogger().info("Plugin Disable Setting Detected...");
    18. getServer().getPluginManager().disablePlugin(this);
    19. }
    20. }

    I've even tried copyDefaults(true) within the command function....
    But it always ends up with the same result.
    They edit the file, do the reload command, and the values are reverted.
    I'm probably missing something completely obvious here, and I've seen some really large, complex codes people have come up with that somehow work.... I just want to know how to do it the way it's meant to be done. Anyone who could point out where I'm screwing up would be a big help,​
    thank you.​
     
  2. Offline

    xTigerRebornx

    xYourFreindx Well, everytime you are calling reloadConfig(), you are calling saveConfig() right before it. saveConfig() will save the data that is in your JavaPlugin's FileConfiguration object, overriding the contents of the file. The solution is to just remove your saveConfig() call.
     
  3. xTigerRebornx SWEET
    Thank you!

    Would it be better to put the saveConfig() after my reloadConfig()? Or is there no reason in having saveConfig() at all?
    Obviously I had misunderstood what saveConfig() does, and I just want to be 100% sure that I know how it works now.
     
  4. Offline

    xTigerRebornx

    xYourFreindx So, an easy way of explaining this, FileConfiguration (generally, inherits the actual map from MemorySection) stores its values in a Map<String, Object> , where the String is the path to what you want, Object is what is stored there, and I believe this is used to stop repeated read/writes to a File. Calling saveConfig() will take that Map, save it to the file, overriding the Files contents. reloadConfig() will pull the values for that Map<String, Object> out of the file and put them into the Map, overriding the Map's contents.
    Therefor, why your code wouldn't work, its because you'd call saveConfig(), which would save that map to the file, and then you call reloadConfig(), which would just re-assign the map that just saved using saveConfig().
     
  5. So in what circumstance would saveConfig() actually be useful?! O_O
     
  6. Offline

    xTigerRebornx

    xYourFreindx If you are making changes to the FileConfiguration that is stored in memory, like setting values from a command or from your plugin, or any situation that you'd want the FileConfiguration's contents saved. If the values in memory aren't saved, they are lost, so something like a reload would caused unsaved things to disappear.
     
  7. xTigerRebornx
    Ahhh... I don't know why that was so difficult for me to understand. Thank you for your patience with my ignorance.
     
Thread Status:
Not open for further replies.

Share This Page