How Does One Go About Reading YMLs?

Discussion in 'Plugin Development' started by Rellac, Oct 30, 2014.

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

    Rellac

    So I have been working on a plugin lately, my first to utilise YML files. Thus far, I've managed to handle writing of configs just fine and I'm rather confused as to how one would handle the reading aspect of the files

    This plugin is just a learning experience, it's just a very basic clone of a permissions plugin with a date checking system, these features can be very useful to a plugin I have in the works and I find that it is always much easier to learn new mechanics on an entirely new plugin, rather than chucking them into what I already have and hoping nothing conflicts.

    What I have so far is the following methods to write/save/load: http://pastie.org/9667849

    Then, within a command, the following lines are run:

    Code:
                Date now = new Date();
                getPlayers();
                if (!Players.contains("players."+args[0]))
                {
                    Players.createSection("players."+args[0]);
                    Players.createSection("players."+args[0]+".classes");
                }
                Players.createSection("players."+args[0]+".classes."+args[1]);
                Players.createSection("players."+args[0]+".classes."+args[1]+".date");
                //players.<player name>.classes.<class name>.date
             
                Players.set("players."+args[0]+".classes."+args[1]+".date", now);
     
                savePlayers();

    This all works just fine. I never seem to have any issues with overwriting/data loss. They form a list such as:

    Code:
        players:
            <player name>
                classes
                    <class name>
                        date
                            <date obtained>
                    <class name>
                        date
                            <date obtained>
            <player name>
                classes
                    <class name>
                        date
                            <date obtained>
                    <class name>
                        date
                            <date obtained>
    etc.

    I would like to go through each player whenever a specific command is ran and check the date obtained for each class. If the date of a specific class is over some predefined time, say, a week or a month or something, that class should then be removed from the listings for the player.

    I am very unsure of how to handle this and would very much appreciate a push in the right direction if no one minds.

    Thanks a lot for any assistance.
     
  2. Offline

    Abs0rbed

    You can use some nested for loops. ConfigurationSection provides getKeys(boolean deep) for a specific section. From there, all you have to do is use the keys (which should be the player names) to go. Granted, you'll need a few strings to do it.
    Code:java
    1. for(String player : YourConfigVar.getConfigurationSection("players").getKeys(false)){
    2.  
    3. for(String class : YourConfigVar.getConfigurationSection("players." + player + "classes").getKeys(false)){
    4.  
    5. long timeIn = YourConfigVar.getLong(class + ".date");
    6.  
    7. if(System.getCurrentTimeMillis() > timeIn + 604800000){//1 week
    8.  
    9. //Do whatever
    10. }
    11.  
    12. }
    13.  
    14. }


    Also, you should really be storing the current system time instead of a date object, makes it easier to calculate the time.
     
    Rellac likes this.
  3. Offline

    OffLuffy

    I don't think you need to create sections at all. When you set a child data node in a section (like setting the date), all sections required are created as well.

    Also, as mentioned above, it's a bit more common to store a UNIX-ish style timestamp, which is done with
    Code:java
    1. System.currentTimeMillis()
    Which gives you a long representing the number of milliseconds since a certain date. You can compare the age of that timestamp by fetching the currentTimeMillis() again whenever you need and subtracting the stored timestamp from it, which you can then divide by 1000L for age in seconds, another 60 for minutes, etc.

    To read those dates from file, you can use .getLong(pathToValue). Don't use getInt() or it'll likely throw errors. The timestamps tend to be larger than int's max value. The 'pathToValue' is the path (like a folder path, but periods instead of slashes) to the value in the file, i.e. "players.PLAYERNAME.classes.date"

    It may also be worth using the Player's UUID (.getUniqueId().toString()) instead of names in configs as name changing will be enabled very soon, and the plugin will no longer be able to identify players if using names.
     
    Rellac likes this.
  4. Offline

    Rellac

    Thanks a lot chaps, I'll get to implementation as soon as I can

    edit: pointing towards sections perfectly now, thanks a lot.
     
  5. Offline

    Rellac

    Abs0rbed OffLuffy

    Hi, sorry, if no one minds, I seem to have another issue here. I'm not sure how I should go about deleting a section.

    Within the directory players/<player name>/classes/<class name>, I am attempting to delete a specific <class name>, whilst leaving any other <class name> untouched.

    I attempted to use the line
    Code:
    Players.set("players." + player + ".classes." + pclass, null)
    Everything I can find online points to this being the perfect way for me to remove a section, but it simply doesn't respond. The section is still there, happy as larry and I can't find out how to remove it.

    pclass is the name of the specific class that I want to remove and my debugs tell me that it has the exact correct value and the line I am attempting to use is accessed just fine in run time. player is also printing the correct value.
     
  6. Offline

    fireblast709

    Rellac make sure you save the file after you set it to null :p.
     
    Rellac likes this.
  7. Offline

    Rellac

    ...

    Please excuse my brain, he likes to make me look like an idiot often
     
Thread Status:
Not open for further replies.

Share This Page