Solved Me = Confuzzled. How to get the name of YamlConfiguration

Discussion in 'Plugin Development' started by Pizza371, Sep 22, 2013.

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

    Pizza371

    How do you do this?
    I have a yamlconfiguration loaded, I just need to get the NAME of the yamlfile
    e.g.
    YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
    String yname = Y.NAME;
    //How do I get the name??

    Thanks! Help much appreciate
     
  2. Offline

    chasechocolate

    You can't. You would have to get the name of the file. YamlConfigurations simply store data and not the file (which only gets saved to the file when you call the save(file) method).
     
  3. Offline

    Pizza371

    chasechocolate
    CaptainBern
    Thanks for replying!/
    I can't get the file since I'm sorting values then retrieving the yaml again :( I don't know what to do here, any help?
    File[] files = getDataFolder().listFiles();
    for(int i = 0; i < getDataFolder().listFiles().length; i++) {
    YamlConfiguration y = YamlConfiguration.loadConfiguration(files);
    killstop.add(y);
    }
    Collections.sort(killstop, new Comparator<YamlConfiguration>() {
    @Override
    public int compare(YamlConfiguration o1,
    YamlConfiguration o2) {
    int kills1 = o1.getInt("kills");
    int kills2 = o2.getInt("kills");

    return Integer.compare(kills1, kills2);
    }
    });
    p.sendMessage("kills top");
    p.sendMessage("1. " + NAME + " kills: " + killstop.get(1).getInt("kills")); EDIT: the integer kills works, but im not sure how to go about getting the nam e:p
     
  4. Code:
    YamlConfiguration.loadConfiguration(files);
    This part won't compile. You are passing a File-Array, but it expects a plain file. Correct would be files, which also gets you access to the name.

    You probably need the name after the loop though.
    There are 2 easy solutions:
    1. Save both the YamlConfiguration and the filename in the Collection you are sorting (e.g. create a wrapper class containing both).
    2. (Hackish workaround) In the for-loop, set an arbitrary value within the YamlConfiguration to the filename. Only really works if you are not planning on saving it later. That approach allows you to retrieve the value with the same key again.
     
    Pizza371 likes this.
  5. Offline

    Pizza371

    Bone008
    Thanks for responding!
    I actually have it as:
    YamlConfiguration y = YamlConfiguration.loadConfiguration(files);
    I see it didn't post as that, but I have no idea why cuz I didn't edit that part... anyhow :p
    I created a wrapper killsWrapper, but I'm unsure exactly where to put it or how to use it, do you mean an array?
    How would I get the information and insert it into this?
    Couldn't I just use a HashMap if I can get the playername, kills WHEN sorted?
    Thanks!
     
  6. Oh, so you are dealing with player names. In that case, a HashMap<String, Configuration> would probably make sense, but it can't be sorted.

    The wrapper class would contain the String and the Configuration as attributes and you would store them in your Collection<NamedConfiguration> (as an example).

    You could use a TreeMap, which works like a HashMap, but is ordered and sorted automatically. You have to provide it with a Comparator, just like you did for the Collections.sort-Method.
    I don't know what exactly you want to do with your configurations, but a TreeMap would allow you to both lookup values by key and iterate over them in a sorted fashion.

    To get the first entry (= the one with the most kills), you would do something like that:
    Code:
    TreeMap<String, Configuration> map = new TreeMap<String, Configuration>(/* ... your comparator here ... */);
     
    // ...
    // fill the map in the loop here
    // ...
     
    // get the first entry
    Entry<String, Configuration> first = map.entrySet().iterator().next();
    
    BTW: Please use [ CODE ] or [ syntax=java ] Tags when posting code, it makes it more readable.[/CODE]
     
    Pizza371 likes this.
  7. Offline

    Pizza371

    Bone008

    Thank you so much!

    I ended up using the method you told me about earlier, took me a little to understand all of it, but it's done now! :D

    And I will use tags, thanks for reminding me.

    Incase anyone wonders over this thread when searching for help, this is how I did it:
    Code:java
    1. class LbK {
    2.  
    3. int kills;
    4.  
    5. String name;
    6.  
    7.  
    8.  
    9. public String getName() {
    10.  
    11. return name;
    12.  
    13. }
    14.  
    15. public int getKills() {
    16.  
    17. return kills;
    18.  
    19. }
    20. }
    21. //class LBK end
    22.  
    23. //ORGANIZING LEADERBOARD
    24. LbK l = new LbK();
    25.  
    26. File[] files = getDataFolder().listFiles();
    27.  
    28. for(int i = 0; i < getDataFolder().listFiles().length; i++) {
    29.  
    30. YamlConfiguration y = YamlConfiguration.loadConfiguration(files);
    31.  
    32. l.kills = y.getInt("kills");
    33.  
    34. l.name = files.getName().replaceAll(".yml", "");
    35.  
    36. lbktop.add(l);
    37.  
    38. }
    39.  
    40. Collections.sort(lbktop, new Comparator<LbK>() {
    41.  
    42. @Override
    43.  
    44. public int compare(LbK o1,
    45.  
    46. LbK o2) {
    47.  
    48. int kills1 = o1.getKills();
    49.  
    50. int kills2 = o2.getKills();
    51.  
    52.  
    53.  
    54. return Integer.compare(kills1, kills2);
    55.  
    56. }
    57.  
    58. });[/syntax=java]

    Thanks Again!
     
Thread Status:
Not open for further replies.

Share This Page