Solved .set("path.to.section", null) is not removing sections from config

Discussion in 'Plugin Development' started by RegalMachine, Jul 26, 2014.

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

    RegalMachine

    Here is a typical config for a plugin I am creating called Districts. (The districts are kept track of via UUIDs)

    Code:
    Districts:
      2c9a57cb-3d0a-4a1a-a196-811574157989:
        ownerUUID: c1533692-de50-43ee-b653-73b69b90f085
        worldUUID: 8e7f586b-9ca0-4a84-8f3d-e09c4f31b06e
        xyz:
          ==: Vector
          x: 37.43829390945743
          y: 69.0
          z: 326.47624173914505
        radius: 4
        trustedUUID: []
    
    Now I use the class for custom configuration files provided on the Bukkit Wiki found Here.

    I keep track of my various configs by keeping a static declaration of them in my main class extending JavaPlugin. and initializing them in my onEnable().

    There comes a time when I need to remove the 2c9a57cb-3d0a-4a1a-a196-811574157989 and all its under parts (ownerUUID, worldUUID, ect...) without removing Districts:.

    Over the course of much search, it looks like I should just be able to set 2c9a57cb-3d0a-4a1a-a196-811574157989 to null and get rid of everything, but this is not the case. Here are the various ways i have tried to delete that section:

    Code:java
    1.  
    2. Main.districts.getConfig().set("Districts." + id.toString() + ".ownerUUID", null);
    3. Main.districts.getConfig().set("Districts." + id.toString() + ".worldUUID", null);
    4. Main.districts.getConfig().set("Districts." + id.toString() + ".xyz", null);
    5. Main.districts.getConfig().set("Districts." + id.toString() + ".radius", null);
    6. Main.districts.getConfig().set("Districts." + id.toString() + ".trustedUUID", null);
    7.  
    8. Main.districts.getConfig().set("Districts." + id.toString(), null);
    9.  
    10. Main.districts.saveConfig();

    ('id' is a UUID variable in my District class. Those of you who are experienced should know how UUID's work and not need an explanation to understand their function as I've used them.)
    Code:java
    1.  
    2. Main.districts.getConfig().set("Districts." + id.toString(), null);
    3. Main.districts.saveConfig();


    Code:java
    1. Set<String> keys = Main.districts.getConfig().getConfigurationSection("Districts." + id.toString()).getKeys(true);
    2.  
    3. for(String path: keys){
    4.  
    5. Main.districts.getConfig().set(path, null);
    6.  
    7. }
    8.  
    9. Main.districts.getConfig().set("Districts." + id.toString(), null);
    10. Main.districts.saveConfig();


    Setting to null and saving isn't working! I've also used reloadConfig(); both before and after saveConfig(), and I haven't been able to get it to work! The .yml doesn't change!

    If you wish to see the rest of my code, let me know; The plugin won't work on the 2nd load unless this gets fixed!

    Remember to tagh me on replies. Thank you!

    Bump... Please, I need this plugin done ASAP, and I have been trying to build around this for 4 days now. It can't wait any longer.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    xTigerRebornx

    RegalMachine I can confirm that setting a ConfigurationSection to null deletes it. What is 'districts'? Can you provide that class if it is one you made?
     
  3. Offline

    RegalMachine

    xTigerRebornx

    districts is an instance of ConfigFile, using this class here, provided on the Bukkit Wiki.
     
  4. Offline

    xTigerRebornx

    RegalMachine What does the config look like when you save it after setting everything to null? And have you verified that the code that clears the section is actually being called?
     
  5. Offline

    RegalMachine

    xTigerRebornx

    The block of code IS called, verified by adding an bit that tells the console its attempting to remove the section. After it all executes without any errors, the config looks exactly the same.
     
  6. Offline

    xTigerRebornx

    RegalMachine The best I can say is that you need to debug your code. Setting it to null does work, the fault lies somewhere in your code.
    Providing the full code of the class where you call the setting could help us spot the problem, but as much as I can tell, either the setting isn't properly called, or the saving isn't properly called.
     
  7. Offline

    RegalMachine

    xTigerRebornx Just a minute, i will upload a GitHub of my entire code, and tell you where everything happens.

    @xTigerRebornx This is where you can find the git repository...

    The bit of code I am dealing with is in Districts/Commands/DistrictCommands.java line 276.

    That line gets a District object from the DistrictBag class, and calls the remove method for that object. District is located in Districts/Protection/

    xTigerRebornx

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    xTigerRebornx

    RegalMachine I noticed this:
    Code:
                if(Main.districts.getConfig().contains("Districts." + uuid)){
                    wizard.getPlayer().sendMessage(Lang.DISTRICTS + "District not removed from config!");
                }
    Do you ever receive this message?
     
  9. Offline

    RegalMachine

  10. Offline

    xTigerRebornx

    RegalMachine Can you debug what keys the Map in DistrictBag has?
     
  11. Offline

    RegalMachine

    xTigerRebornx I can put a check befor i remove them to see if the UUID i am using to remove exists in the DistrictBag... Give me a minute

    xTigerRebornx line 33 of DistrictBag changed from districts.remove(d); to districts.remove(d.getUUID()); ... so now things will actually be removed from the DistrictBag, but that still doesn't solve the initial issue of the plugin not deleting the config section. removal from the bag happens after it removes from the config, as it needs the reference to get to the config. Those two things happen seperatly.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Offline

    xTigerRebornx

    RegalMachine
    If you don't mind, can you print them out and post the results?
     
  13. Offline

    RegalMachine

    I just sent messages to myself using player.sendMessage(), they just state that the UUID that i am looking for is in the config, and is in the bag, and after the methods have been called, it tells me that it was removed from the bag, but not from the config. xTigerRebornx
     
  14. Offline

    xTigerRebornx

    RegalMachine
    Code:
     
        public void remove(){
            Main.districts.getConfig().set("Districts." + id.toString() + ".ownerUUID", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".worldUUID", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".xyz", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".radius", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".trustedUUID", null);
            Main.districts.getConfig().set("Districts." + id.toString(), null);
            Main.districts.saveConfig();
        }
    Print out the values of what is at the paths before and after each setting and post the values.
     
  15. Offline

    RegalMachine

    xTigerRebornx Befor the unclaim command:
    Code:
    Districts:
      300399d6-be25-4a84-8c44-da546cac2b9c:
        ownerUUID: c1533692-de50-43ee-b653-73b69b90f085
        worldUUID: 8e7f586b-9ca0-4a84-8f3d-e09c4f31b06e
        xyz:
          ==: Vector
          x: 37.51629073928001
          y: 69.0
          z: 326.54138018554625
        radius: 4
        trustedUUID: []
    
    after the unclaim command:

    Code:
    Districts:
      300399d6-be25-4a84-8c44-da546cac2b9c:
        ownerUUID: c1533692-de50-43ee-b653-73b69b90f085
        worldUUID: 8e7f586b-9ca0-4a84-8f3d-e09c4f31b06e
        xyz:
          ==: Vector
          x: 37.51629073928001
          y: 69.0
          z: 326.54138018554625
        radius: 4
        trustedUUID: []
    
    Nothing has changed...
     
  16. Offline

    xTigerRebornx

    RegalMachine I didn't ask for what the config file looks like, I already know that it doesn't change. Add in the specific debug messages I specified and post the debug results.
     
  17. Offline

    RegalMachine

    using this method
    Code:java
    1. public void printBag(){
    2. for(UUID id: DistrictBag.districts.keySet()){
    3. Bukkit.getLogger().info(id.toString());
    4. }
    5. }


    before and after the unclaim command was ran, it yeilded these results...

    Before:
    Code:
    [19:18:34] [Server thread/INFO]: Printing Bag
    [19:18:34] [Server thread/INFO]: e812f189-5532-4c07-b03d-ac8f8e78c1f7
    
    After:
    Code:
    [19:18:34] [Server thread/INFO]: Printing Bag
    So as you can see, the district is indeed removed from the bag. But not the config.
     
  18. Offline

    xTigerRebornx


    RegalMachine Was referring to this debug, not the debug determining if it was removed or not. I want to see if the change is made in memory.
     
  19. Offline

    RegalMachine

    xTigerRebornx I am not really understanding what you are asking me to do... This?
    Code:
    public void remove(){
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".ownerUUID"));
            Main.districts.getConfig().set("Districts." + id.toString() + ".ownerUUID", null);
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".ownerUUID"));
            Main.districts.getConfig().set("Districts." + id.toString() + ".worldUUID", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".xyz", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".radius", null);
            Main.districts.getConfig().set("Districts." + id.toString() + ".trustedUUID", null);
            Main.districts.getConfig().set("Districts." + id.toString(), null);
            Main.districts.saveConfig();
        }
    but after every line and with the correct path? or check before and after all 6 lines and print the results?
     
  20. Offline

    xTigerRebornx

    RegalMachine Yes, I want you to do that with all of the values and post the results of it.
     
  21. Offline

    RegalMachine

    xTigerRebornx

    check/print
    ...
    check/print

    set
    ...
    set
    save

    check/print
    ...
    check/print

    ?

    Or the first one?
     
  22. Offline

    xTigerRebornx

    RegalMachine
    for each path:
    print value
    change (set())
    print value again
     
  23. Offline

    RegalMachine

    Using this method
    Code:
    public void remove(){
         
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".ownerUUID"));
            Main.districts.getConfig().set("Districts." + id.toString() + ".ownerUUID", null);
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".ownerUUID"));
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".worldUUID"));
            Main.districts.getConfig().set("Districts." + id.toString() + ".worldUUID", null);
            Bukkit.getLogger().info(Main.districts.getConfig().getString("Districts." + id.toString() + ".worldUUID"));
            Bukkit.getLogger().info(Main.districts.getConfig().getVector("Districts." + id.toString() + ".xyz").toString());
            Main.districts.getConfig().set("Districts." + id.toString() + ".xyz", null);
            Bukkit.getLogger().info(Main.districts.getConfig().getVector("Districts." + id.toString() + ".xyz").toString());
            Bukkit.getLogger().info(Main.districts.getConfig().getInt("Districts." + id.toString() + ".radius") + "");
            Main.districts.getConfig().set("Districts." + id.toString() + ".radius", null);
            Bukkit.getLogger().info(Main.districts.getConfig().getInt("Districts." + id.toString() + ".radius") + "");
            for(String s: Main.districts.getConfig().getStringList("Districts." + id.toString() + ".trustedUUID")){
                Bukkit.getLogger().info(s);
            }
            Main.districts.getConfig().set("Districts." + id.toString() + ".trustedUUID", null);
            for(String s: Main.districts.getConfig().getStringList("Districts." + id.toString() + ".trustedUUID")){
                Bukkit.getLogger().info(s);
            }
            Main.districts.getConfig().set("Districts." + id.toString(), null);
         
            Main.districts.saveConfig();
        }
    The console spat out
    Code:
    [19:35:29] [Server thread/INFO]: c1533692-de50-43ee-b653-73b69b90f085        //OwnerUUID
    [19:35:29] [Server thread/INFO]: c1533692-de50-43ee-b653-73b69b90f085       
    [19:35:29] [Server thread/INFO]: 8e7f586b-9ca0-4a84-8f3d-e09c4f31b06e        //WorldUUID
    [19:35:29] [Server thread/INFO]: 8e7f586b-9ca0-4a84-8f3d-e09c4f31b06e
    [19:35:29] [Server thread/INFO]: 37.51629073928001,69.0,326.54138018554625    //Vector.toString()
    [19:35:29] [Server thread/INFO]: 37.51629073928001,69.0,326.54138018554625
    [19:35:29] [Server thread/INFO]: 4                //radius
    [19:35:29] [Server thread/INFO]: 4
    //No trustedUUID
    
    @xTigerRebornx

    xTigerRebornx I just tried saving the config after each set(), and it spat out the same thing

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  24. Offline

    xTigerRebornx

    RegalMachine The problem lies here:
    Code:
    //Do things to the Config
            Main.districts.getConfig().createSection("Districts." + id.toString());
            Main.districts.getConfig().addDefault("Districts." + id.toString() + ".ownerUUID", owner.getPlayer().getUniqueId().toString());
            Main.districts.getConfig().addDefault("Districts." + id.toString() + ".worldUUID", center.getWorld().getUID().toString());
            Main.districts.getConfig().addDefault("Districts." + id.toString() + ".xyz", center.toVector());
            Main.districts.getConfig().addDefault("Districts." + id.toString() + ".radius", radius);
            Main.districts.getConfig().addDefault("Districts." + id.toString() + ".trustedUUID", trusted);
    You are adding defaults. Below is the code for the implementation of getString() and what it calls
    Code:
     public String getString(String path) {
            Object def = getDefault(path);
            return getString(path, def != null ? def.toString() : null);
    }
    Code:
    public String getString(String path, String def) {
            Object val = get(path, def);
            return (val != null) ? val.toString() : def;
        }
    Notice, it will use the default if the value is null. That is why it is not properly saving, because you've written its defaults and it then uses those defaults if the value is null (you try to delete by setting it to null).
    So the solution would be to remove the code that sets the defaults, and use a set call instead (if that is the behavior you wanted)
     
  25. Offline

    RegalMachine

    xTigerRebornx so set() would create the section if it didnt already exist? and then allow me to remove it later?
     
  26. Offline

    xTigerRebornx

    RegalMachine Set will, as per what it does, set the value there. What were you trying to accomplish by adding the defaults?
     
  27. Offline

    RegalMachine

  28. Offline

    xTigerRebornx

    RegalMachine Use set for that, I've specified the problem with addDefault() and why it is stopping you from deleting the values.
     
    RegalMachine likes this.
  29. Offline

    RegalMachine

    xTigerRebornx Let me try this out...

    xTigerRebornx That was my issue! Thank you so much, you've been a tremendous help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page