Per Player Config Files Help.

Discussion in 'Plugin Development' started by CytrixMC, Aug 29, 2014.

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

    CytrixMC

    I recently learned that having player data in one config file is very bad and can cause lots of time to load a player's data (Of coarse if there are tons of people in a single file). Now I want to learn how to do that. This could be very useful when I would hold statistics of players such as kills, coins, deaths, and etc. What is a way to make Per Player Config file and be able to read and write data into each individual file?
     
  2. Offline

    SmooshCakez

    You can use Bukkit's FileConfiguration on any file, not just getConfig(), which grants access to set(), get(), etc. Just create a file in the data folder with the name you want it to be (probably the player's UUID) and make a FileConfiguration instance. CytrixMC
     
  3. Offline

    CytrixMC

    How would i rename the file. and then create multiple files. SmooshCakez
     
  4. Offline

    jojo1541

    Code:java
    1. File file = new File("Your Path","Your Filename");

    to create a file,
    Code:java
    1. YamlConfiguration Conf = new YamlConfiguration();
    2.  
    3. Conf.loadConfiguration(file);

    to load your file as a config and

    Code:java
    1. Conf.getInt(String YamlNode)
    2. Conf.getBoolean(String YamlNode)
    3. Conf.getString(String YamlNode)
    4. Conf.getWhatever(String YamlNode)
    5.  
    6. Conf.set(String YamlNode, Object Value)

    to read/write values to it.
    Everythin else is described in the corresponding Javadocs.

    Always remember to catch the exceptions properly and check what values your config supports.

    Hope i could help. ;)
     
  5. Offline

    CytrixMC

    Thanks. But wouldn't I have to make an PlayerJoinEvent then check if the Data Folder Contains the players name. If not not it would make 1 file. Am I wrong or right?
     
  6. Offline

    Gnat008

    CytrixMC
    Using the above, on each PlayerJoinEvent, check if a file with their name (or UUID, if it does not absolutely need to be Human-readable, with changes to UUID to store data) exists. If it does not exist, create a new file with their name or UUID.
     
  7. Offline

    jojo1541

    yes, but it wont create a real file on its own. In my plugins i usually use the method 'exsists();' of File to determine if the file exists and if not i create one using 'createNewFile();'.
    If the file already exists though, you can directly use 'loadConfiguration();'

    One of my Configs:
    Code:java
    1. public class ConfigManager {
    2.  
    3. private File ICf;
    4. private Main M;
    5. private YamlConfiguration ICs;
    6.  
    7. public ConfigManager(Main m) {
    8. M = m;
    9. ICf = new File(M.getDataFolder(), "ICs.yml");
    10. }
    11.  
    12. public void loadICs() {
    13. //Here starts the code you would use on a PlayerJoinEvent
    14. if(ICf.exists()) {
    15. ICs = YamlConfiguration.loadConfiguration(ICf);
    16. } else {
    17. try {
    18. ICf.createNewFile();
    19. ICs = YamlConfiguration.loadConfiguration(ICf);
    20. return;
    21. } catch(IOException e) {
    22. e.printStackTrace();
    23. return;
    24. }
    25. }
    26.  
    27. //Nodes to load/save
    28.  
    29. }
    30.  
    31. }
     
  8. Offline

    CytrixMC

    I sort of understand. So on join should i check if a file with the player's name exist. Then if it doesnt create a new file with the players name. Then I can add data to each player's config by checking actions by the player such as kills and deaths. Everytime a player gets a kill I see if the player has kill data written already written in the file. If they do I get the Integer under Kills and +1. Otherwise I will give them 1 kill and save their Config. I think I have the idea now. But how would I save the player's own config. would I do the same as a normal one like saveConfig(); or is there another way to do it.
     
  9. Offline

    jojo1541

    Just use
    Code:java
    1. YourConf.save(File YourFile);


    One thing though. If the server isn't that big i would load the config into memory when the player joins and just save the values to config when he leaves. That should save some time/resources. (and it would be easier on the servers harddrive :p)
     
  10. Offline

    CytrixMC

    By isn't that big how many players do you think is fine for 1 file?
     
  11. Offline

    jojo1541

    oh ok, i thought you wanted one file per player.
    You can store an infinite Number of players in your config. but keep the loading time in mind. A big file takes long to load or to search through.
    I would still recommend to store the data for the players online in memory. just load them if a players joins, manipulate them as he plays and save them back to your file when he leaves.
    Should be faster too.
     
  12. Offline

    CytrixMC

    So we wouldn't flood this chat I made a convo with you.
     
Thread Status:
Not open for further replies.

Share This Page