Writing to a YML

Discussion in 'Plugin Development' started by will181, Jul 12, 2014.

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

    will181

    Hello.

    I am currently creating a plugin that requires data to be written to a yml file onDisable(). I have created a function that loops through my HashMaps that I have created and then uses this to write data to the YML. However, when this function is called, what is actually written is some wierd thing, where the last section is just not filled-in, and js instead just written as "*id001". The top section, while the data is actually written to it, has "&id001" mysteriously added to it.

    Here is the function that I use:

    Code:java
    1. public void saveData() {
    2.  
    3. Main.getInstance().saveDefaultConfig();
    4.  
    5. ArrayList<Object> data = new ArrayList<Object>();
    6.  
    7.  
    8. Main.getInstance().getConfig().set("ids", data);
    9.  
    10. for(Location loc : Main.getInstance().idCoords.keySet()) {
    11.  
    12. //Clears the arraylist
    13. data.clear();
    14.  
    15. //Generating a new section in the yml
    16. Main.getInstance().getConfig().createSection("ids.id_" + Main.getInstance().idCoords.get(loc));
    17.  
    18. data.add(loc.getBlockX()); //X Cord
    19. data.add(loc.getBlockY()); //Y Cord
    20. data.add(loc.getBlockZ()); //Z Cord
    21. data.add(loc.getWorld().getName()); //World
    22. data.add(Main.getInstance().itemTypes.get(Main.getInstance().idCoords.get(loc)).toString()); //ItemType
    23. data.add(Main.getInstance().itemMeta.get(Main.getInstance().idCoords.get(loc))); //ItemMeta
    24. if(Main.getInstance().adminIDs.contains(loc)) //Admin id check
    25. data.add("true");
    26. else
    27. data.add("false");
    28.  
    29. //Storing the data
    30. Main.getInstance().getConfig().set("ids.id_" + Main.getInstance().idCoords.get(loc), data);
    31.  
    32. }
    33. }


    Extra information, adminIDs is an ArrayList. All the other data I am calling comes from HashMaps

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

    ChrystianSandu

    Thanks so much for this code! :)
     
  3. Offline

    EnderTroll68

    Try using a list instead of an arraylist
     
  4. Offline

    will181


    Thanks for the suggestion, but made absolutely no difference.

    What the config.yml ends up as.
    Code:
    stores:
      store_3: &id001
      - 516
      - 66
      - -138
      - world
      - SAND
      - ==: ItemMeta
        meta-type: UNSPECIFIC
      - 'true'
      store_2: *id001
      store_1: *id001
     
  5. Offline

    ZodiacTheories

  6. Offline

    minoneer

    Hey,

    I added some comments to your code where I have Suggestions/Questions about it:

    Code:java
    1. public void saveData()
    2. {
    3.  
    4. Main.getInstance().saveDefaultConfig();
    5.  
    6. ArrayList<Object> data = new ArrayList<Object>();
    7.  
    8. //What is this good for? You just created an empty array and then save it?
    9. Main.getInstance().getConfig().set("ids", data);
    10.  
    11. //You iterate over a keyset to then later retreive the value. I would suggest using the entry set for your iteration.
    12. for (Location loc : Main.getInstance().idCoords.keySet())
    13. {
    14.  
    15. //Now you are using the ArrayList you already saved. What's the point of that?
    16. //Clears the arraylist
    17. data.clear();
    18.  
    19. //Generating a new section in the yml
    20. //What is the value type of the idCoords map? Is it a String?
    21. Main.getInstance().getConfig().createSection("ids.id_" + Main.getInstance().idCoords.get(loc));
    22.  
    23. data.add(loc.getBlockX()); //X Cord
    24. data.add(loc.getBlockY()); //Y Cord
    25. data.add(loc.getBlockZ()); //Z Cord
    26. data.add(loc.getWorld().getName()); //World
    27. data.add(Main.getInstance().itemTypes.get(Main.getInstance().idCoords.get(loc)).toString()); //ItemType
    28. data.add(Main.getInstance().itemMeta.get(Main.getInstance().idCoords.get(loc))); //ItemMeta
    29. if (Main.getInstance().adminIDs.contains(loc)) //Admin id check
    30. {
    31. data.add("true");
    32. }
    33. else
    34. {
    35. data.add("false");
    36. }
    37.  
    38. //If you are just setting this as config, there is no need to create the sectio first. It is done automatically.
    39. //Storing the data
    40. Main.getInstance().getConfig().set("ids.id_" + Main.getInstance().idCoords.get(loc), data);
    41. }
    42. }


    While we could, generally speaking, get this to eventually work, it is way more complex than it needs to be. Bukkit provides us with an excellent serialization API for YAML files - so I woul suggest to make use of it.

    You can read about it here: http://wiki.bukkit.org/Configuration_API_Reference#Advanced_Topics

    I am also glad to help you getting started, if you describe you goal with this method :)

    regards, minoneer
     
  7. Offline

    will181

    minoneer In regards to your comments (in order):

    1. I set it to an empty array to clear it. Not sure if that is necessary, but I thought I'd add it to be sure
    2. I iterated over the keySet because I had to call the loc value several times. Again, you're right I could turn this around, but I don't really see the point
    3. I clear it because I am looping through the array several times so need to clear the data so that it is clean each time.
    4. Location, Integer
    5. Did not know that.
    I don't mean for this to seem like I am just giving blunt responses to your comments; I do sincerly appreciate the help.
    Regarding the API, I'll go read through that after posting this reply.
    The purpose of the function is simply to pull data out of a series of different hashMaps and store them in a YML onDisable().
     
  8. Offline

    ChrystianSandu

    ZodiacTheories Ah, I search on internet how to clear inventory, but this post make my work faster!
     
  9. Offline

    ZodiacTheories

Thread Status:
Not open for further replies.

Share This Page