Config won't update!

Discussion in 'Plugin Development' started by canemonster15, Aug 20, 2013.

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

    canemonster15

    I want to make it so when I type /knockoff create <name>, it will get my coordinates and write them to the config. Here is my code:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("knockoff")){
    2. if(args.length == 0){
    3. sender.sendMessage(prefix + "Please specify a command.");
    4. }else if(args.length == 1){
    5. sender.sendMessage(prefix + "Please specify a name!");
    6. }else if(args.length == 2){
    7. if(args[0].equalsIgnoreCase("create")){
    8. if(!sender.hasPermission("knockoff.create")){
    9. sender.sendMessage(prefix + "Insufficient Permission!");
    10. }else{
    11. double x = player.getLocation().getX();
    12. double y = player.getLocation().getY();
    13. double z = player.getLocation().getZ();
    14. World w = Bukkit.getServer().getPlayer(player.getName()).getWorld();
    15. this.getConfig().addDefault(args[1] + ".X", x);
    16. this.getConfig().addDefault(args[1] + ".Y", y);
    17. this.getConfig().addDefault(args[1] + ".Z", z);
    18. this.getConfig().addDefault(args[1] + ".World", w);
    19. this.saveConfig();
    20. this.reloadConfig();
    21. player.sendMessage(prefix + "Arena Created!");
    22. }

    Why isn't this working?
     
  2. Offline

    Hoolean

    canemonster15

    For a start you don't need:
    Code:
    this.reloadConfig();
    Secondly, to save the config if you have used addDefault, you have to do:
    Code:
    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    Thirdly, in this case use the set method instead of the addDefault method, as overwriting should be allowed :)
     
  3. Offline

    canemonster15

    I tried it without this.reloadConfig(), didn't work.
    I have the second piece of code in onEnable().
    And thirdly, what do you mean?
     
  4. Offline

    Hoolean

    canemonster15

    Well if you think about it:
    1. onEnable called
    2. More stuff added to config
    3. onDisable called
    So if you only call that code in onEnable, it's only going to save what you did to the config before you called it. So, only the stuff you did in the onEnable. You need to do it everytime you want to save the config (or put it in the onDisable to have it not actually save anything until the plugin is disabled; the changes will be stored in RAM until then).

    Thirdly, I mean using this.getConfig().set instead of this.getConfig().addDefault will mean that not just the initial data being saved but any modifications made in the future will be saved too. Set always sets the variable and makes it if necessary, whereas addDefault just creates it if it is not there.

    Any other questions? :)
     
  5. Offline

    Saposhiente

    Also, don't try to save the World object in the config; instead use the world's name.
     
    Hoolean likes this.
  6. Offline

    canemonster15

    That is the first time
    I am creating those locations. So I should be using this.getConfig().addDefualt()
     
  7. Offline

    Hoolean

    canemonster15

    Yeah sure but in that case you will need to handle what will happen if they use create and a name that is already taken.
     
Thread Status:
Not open for further replies.

Share This Page