Solved Player-Specific YAML Files

Discussion in 'Plugin Development' started by Quidam, Jan 25, 2013.

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

    Quidam

    So, if you've seen inside Essentials you know what's I'm talking about.
    I need a YAML for each player (ex: "jeb_.yml"), so I can store each StorageMinecart he/she places (by UUID).
    Yes, I'm aware SQL is easier, but it's not available to the server I am developing this for.

    I'd greatly appreciate it if you gave me the code as well as explained it, rather than doing only one of the two. Thanks!
     
  2. Offline

    RainoBoy97

    SQLite?
     
  3. Offline

    Quidam

    Read this:
     
  4. Offline

    RainoBoy97

    SQLite is a flatfile
     
  5. Offline

    Quidam

    Oh, sorry for the confusion.
    In the server folder? I need the .yml saved in /plugins/CoalMiner, not in an online database.
     
  6. Offline

    drtshock

    Quidam
    Do you know how to make configs / custom configs? Because if you do then it's as easy as just creating a new one if it doesn't exist whenever you need to. But since you're posting this, I assume you aren't sure how to so here is one way that I do it. You could just modify the name to be a string of the player's name.

    Code:java
    1. public void loadVaults() {
    2. datafolder = getDataFolder();
    3. datafile = new File(this.datafolder, "vaults.yml");
    4. vaults = new YamlConfiguration();
    5.  
    6. if (!datafolder.exists()) {
    7. try {
    8. getServer().getLogger().info(
    9. "[PlayerVaults] Creating vaults.yml");
    10. datafolder.mkdir();
    11. datafile.createNewFile();
    12. } catch (Exception e) {
    13. getServer().getLogger().severe(
    14. "[PlayerVaults] Could not create vaults.yml: " + e);
    15. }
    16. } else if (!datafile.exists())
    17. try {
    18. getServer().getLogger().info(
    19. "[PlayerVaults] Creating vaults.yml");
    20. datafile.createNewFile();
    21. } catch (Exception e) {
    22. getServer().getLogger().severe(
    23. "[PlayerVaults] Could not create vaults.yml: " + e);
    24. }
    25. try {
    26. vaults.load(datafile);
    27. } catch (Exception e) {
    28. getServer().getLogger().severe(
    29. "[PlayerVaults] Could not load vaults.yml: " + e);
    30. }
    31.  
    32. saveData();
    33. }
     
    justcool393 and Quidam like this.
  7. Offline

    RainoBoy97

    yes, you can have the file in the plugin datafolder :) im not really good with SQL stuff, but i think the URL should then be "jdbc:sqlite:plugins/CoalMiner/players.db" where you can replace players with another name :)
     
  8. Offline

    Quidam

    No, but I already knew how to create a config with getConfig(). I'll read this over and get back to you :)

    drtshock
    I'm assuming getDataFolder() gets the folder "/plugins/[plugin name]". But how would I save it to "/plugins/[plugin name]/players"?

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

    RainoBoy97

    getDataFolder() + File.separator + "players"
     
    drtshock and Quidam like this.
  10. Offline

    drtshock

    Quidam You mean another folder inside of that? Just add it to the path, and if it doesn't exist then do mkdir();
     
    Quidam likes this.
  11. Offline

    Quidam

    Thank you!
     
  12. Offline

    Technius

    Make a new file in your plugin folder titled players and make a new YamlConfiguration for each player. It should look something like this:

    Code:
    File f = plugin.getDataFolder(); //Assuming you aren't using your main class
    if(!f.exists())f.mkdir();
    File playerf = new File(f, "Players");
    if(!playerf.exists())playerf.mkdir();
    ArrayList<String> players = new ArrayList<String>();//Whatever you're using
    for(String s:players)
    {
        try
        {
            YamlConfiguration yaml = new YamlConfiguration();
            //Whatever you're doing here
            yaml.save(new File(playerf, s + ".yml"));
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
    
     
    Quidam and drtshock like this.
  13. Offline

    Quidam

    Thanks, even more compact and effective method! :D
     
  14. Offline

    Technius

    Glad to help! :D
     
Thread Status:
Not open for further replies.

Share This Page