YML Data File Confusion

Discussion in 'Plugin Development' started by nuclearmissile, Apr 18, 2014.

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

    nuclearmissile

    I have been looking at YML files for storing player data, and the various documents I've read haven't clarified a number of things.

    1. Do you create a separate YML file for every player? I didn't think you would, but it leads to the next question.

    2. When defining a path in a YML file, how can you use a player's name to find an identical set of data? For example, if I have the player name Bob142, how can I add that to the path? Am I going to have to build my own string by concatenating the existing player name string with the other path elements?

    3. How do you define the file path when loading the file so that it works with any server? I obviously can't just use my computer's file path if I'm uploading it to Bukkit. How can I do it so that, like the config.yml file, it just sort of finds it?

    I think I can handle the other issues I'm having, but these have irritated me. I'm just using a single string for each player's data, and I can interpret it separately with other code. Thanks in advance for any help you can offer!
     
  2. Offline

    agent6262

    1) No because if you have a thousand players then you will have a thousand files (not fun to deal with).
    2) For this answer I need to know which version of bukkit you are using
    3) Use the getDataFolder() method which belongs to the JavaPlugin class that you extended in your main class.
     
  3. Offline

    nuclearmissile

    For number 2, I'm using 1.7.2. As for number 3, could you elaborate a little more? I'll probably just be saving the data file in a small folder in the plugins folder, similar to where you find a config file. Will it still be able to find it? I'm just not sure what the limitations are to that method.
     
  4. Offline

    agent6262

    nuclearmissile P.S. This all done using a .yml file and i would highly suggest using one to store player related stuff.
    For number 3 try and use a .yml file. And as for number 2 an example of your file would look something like this (partial adaptation from my plugin)
    Code:
    agent6262:
      firstTimeLogin: false
      hasChosen: true
      chosenItems: 2
    And to grab the values
    Code:java
    1. //<filename>.get<int, boolean, so on and so forth>("<player name>.<key value>");
    2. config.getBoolean("agent6262.firstTimeLogin");//would return false
    3. config.getBoolean("agent6262.hasChosen");//would return true
    4. config.getInt("agent6262.chosenItems");//would return 2


    And for the method that i mention above it will return the dir that your plugin creates. So basically the plugin can get any file or dir in the dir returned from the getDataFolder() method. For example
    Code:java
    1. File configFile = new File(this.getDataFolder(), "config.yml");
    2. YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);


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

    nuclearmissile

    Thanks for the help, I will try this! BTW, it's nice of you to show me how to have multiple sub-variables, but I think I'm just going to use a single string to store all the player's data. I'll have my own little string handler, since this file I'm making is only to save player data when they log off. Instead of reading from the file every time a player does something that needs checked, I'm getting their full save data, which isn't very much honestly, and loading it when they log in onto an ArrayList that'll be used for all the necessary checks. Thanks for the help man! :)
     
  6. Offline

    agent6262

    nuclearmissile
    Glad i could help you and if you need anything else just let me know.
     
  7. Offline

    nuclearmissile

    Wow what timing, I literally just came on looking for a bit more help. :3 Now I'm confused as to how to check the presence of a player. In my playerDat.yml, I have players sorted like this:

    Code:
    PlayerName:
      Data1: 0
      Data2: 5
      Data3: 293
    PlayerName2:
      Data1:
    like that, etc. How do I check for the existence of a certain PlayerName, even though those data members themselves have null values. I need to know, if given player name Jim, whether the name Jim appears, and if so I can read the sub-data of Jim. Otherwise I create it.
     
  8. Offline

    agent6262

    If you are using a yml file you can just use
    Code:java
    1. config.contians("player name");

    And are you using a yml file? If not let me know.
     
  9. Offline

    nuclearmissile

    Yes, I'm using a yml file. The thread is named YML file confusion and all my questions were about yml files. Sorry if that wasn't clear enough! :D But seriously, I am indeed using yml file. Thanks for the help, though I can't guarantee I won't post more noob questions in the future. ;)

    EDIT: Actually, do you know if it's faster to take player data off the file and store it in an ArrayList, only loading from file when they log on and saving to it when they log off, or is it better to just constantly update the yml file with the player data.
     
  10. Offline

    agent6262

    nuclearmissile WOW i'm a retard, sorry may bad just answering a lot of peoples questions today.
     
  11. Offline

    nuclearmissile

    agent6262 No problem, so about that question I had edited into the last post? I just don't wanna code the whole plugin around one of those two methods before knowing how much it'll affect running speed.
     
  12. Offline

    agent6262

    I personally would add a player and remove a player from the ArrayList on a PlayerJoinEvent and PlayerQuitEvent respectively.
    And are you storing player names or the player object in the ArrayList?
     
  13. Offline

    nuclearmissile

    I'm storing a custom object that is tied to the player through their name and contains their multitude of plugin-related variables. :D Also thanks, I thought it'd be better that way but wanted to be sure.
     
  14. Offline

    agent6262

    Awesome. Just be sure to keep up with players joining and leaving.
     
  15. Just a tip: Don't store usernames in configs any more, store UUIDs and use the UUIDFetcher/NameFetcher to convert it.
     
  16. Offline

    nuclearmissile

    Thanks for the tip but I'm gonna hold off on that for a future update, when 1.7.9 is in at least the beta build phase.
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page