Config resetting

Discussion in 'Plugin Development' started by Thrylouis, Jun 21, 2018.

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

    Thrylouis

    Hey there!

    I've been working on this plugin with a config and everything works, but everytime I set a value to another value, the layout completely changes with just the keys under each other.
    CODE (open)
    Code:
                            String server = "BungeeCord is set to false";
                            this.getConfig().set("serverhub", server);
                            String world = p.getWorld().getName();
                            this.getConfig().set("worldhub", world);
                            double x = p.getLocation().getX();
                            this.getConfig().set("hubx", x);
                            double y = p.getLocation().getY();
                            this.getConfig().set("huby", y);
                            double z = p.getLocation().getZ();
                            this.getConfig().set("hubz", z);
                            double pitch = p.getLocation().getPitch();
                            this.getConfig().set("hubpitch", pitch);
                            double yaw = p.getLocation().getYaw();
                            this.getConfig().set("hubyaw", yaw);
                            this.saveConfig();
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("setHub")));


    Normal config (open)
    Code:
    #Use bungee or not. Set to true if you want to use it, else set to false.
    bungee: false
    
    #Teleport the players to the spawn when they join.
    teleportonjoin: true
    
    #Send the player a message when they go to spawn. You can set this message in the 'messages.yml'
    messagespawn: true
    
    #You can set a hub. You can also do this by typing: /sethub
    serverhub: false
    worldhub: false
    hubx: false
    huby: false
    hubz: false
    hubpitch: false
    hubyaw: false



    config resets to: (open)
    Code:
    bungee: false
    serverhub: BungeeCord is set to false
    worldhub: SoeSEetHenk
    hubx: 12.452447177811804
    huby: 163.0
    hubz: 12.787384304174566
    hubpitch: 23.849979400634766
    hubyaw: -171.30078125
     
  2. Offline

    CommonSenze

    @Thrylouis
    When you save a config in code after setting things to it, it deletes all your comments. One thing I THINK you can do is instead of setting values you can addDefaults with those values and copy the defaults onEnable. I don't know if that will work.
     
  3. Offline

    Thrylouis

    Maybe that's a good idea, but how? I don't really know what you mean haha
     
  4. Offline

    CommonSenze

    @Thrylouis
    Code:java
    1. public void onEnable() {
    2. String server = "BungeeCord is set to false";
    3. this.getConfig().addDefault("serverhub", server);
    4. String world = p.getWorld().getName();
    5. this.getConfig().addDefault("worldhub", world);
    6. double x = p.getLocation().getX();
    7. this.getConfig().addDefault("hubx", x);
    8. double y = p.getLocation().getY();
    9. this.getConfig().addDefault("huby", y);
    10. double z = p.getLocation().getZ();
    11. this.getConfig().addDefault("hubz", z);
    12. double pitch = p.getLocation().getPitch();
    13. this.getConfig().addDefault("hubpitch", pitch);
    14. double yaw = p.getLocation().getYaw();
    15. this.getConfig().addDefault("hubyaw", yaw);
    16.  
    17. this.getConfig().options().copyDefaults(true);
    18. this.saveConfig();
    19. }


    Instead of set you put addDefault and at the end you do options().copyDefaults(true) and save. What that does is add the default value for each section and copys it if it has no value in place I BELIEVE. I don't use this often and don't know much about it, however, I think this is worth a try. Just know you will have to manually edit the values through the YAML file.
     
  5. Offline

    Thrylouis

    I'll try that now!
     
    Last edited by a moderator: Jun 26, 2018
  6. Offline

    CommonSenze

    @Thrylouis
    I think I found a real solution that will work. If you make your own config.yml in the project its self with all the values set and the comments inside it and on enable copy defaults to true and save that config that might work.
     
  7. Offline

    Thrylouis

    Don't really know what you mean?
     
  8. Offline

    CommonSenze

    @Thrylouis
    Next to your plugin.yml file, make a config.yml and add your values and commands. Once finished you can go to your Main class and onEnable do the copyDefaults method and save the config.
     
Thread Status:
Not open for further replies.

Share This Page