[UNSOLVED] Getting All Children of a Node

Discussion in 'Plugin Development' started by Icyene, Jul 17, 2012.

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

    Icyene

    I have been trying to get all the children of a given node from a configuration file. The file looks like this:

    Code:
    luring:
      loot:
        rawFish: [349, 10, 50, 331, 1]
    
    Having read this post of StackOverflow, I thought that doing what the answer there was was going to solve my problem.

    And so, I wrote this code:

    Code:
    Set<String> a = this.getConfig().getConfigurationSection("luring.loot.").getKeys(false);
    System.out.println(a);
    
    However, it always returns "[]", and when converted to an array, its length is 0.

    Could someone please tell me what I'm doing wrong?
     
  2. Offline

    Bauer

    Are you trying to get the contents of luring.loot.rawfish or are you planning there to be many keys (in the same format as raw fish) that you'd like to put into their own sets? If you're just trying to get rawfish, wouldn't you need to use "luring.loot.rawfish" as opposed to luring.loot.?
     
  3. Offline

    Icyene

    There are about 100 keys. I just posted one for an example :].
     
  4. Offline

    Kodfod

    What i would do is make a file that you can load into an array list. like so:

    Code:JAVA
    1. package me.kodfod.teams.util;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.BufferedWriter;
    5. import java.io.DataInputStream;
    6. import java.io.File;
    7. import java.io.FileInputStream;
    8. import java.io.FileWriter;
    9. import java.io.IOException;
    10. import java.io.InputStreamReader;
    11. import java.util.ArrayList;
    12.  
    13. public class TeamStore {
    14.  
    15. private final File teamfile;
    16. public final ArrayList<String> teams;
    17.  
    18. public TeamStore(File file) {
    19. this.teamfile = file;
    20. this.teams = new ArrayList<String>();
    21.  
    22. if (this.teamfile.exists() == false) {
    23. try {
    24. this.teamfile.createNewFile();
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. }
    29. }
    30.  
    31. public void load() {
    32.  
    33. try {
    34. this.teamfile));
    35.  
    36. input));
    37.  
    38. String team;
    39.  
    40. while ((team = reader.readLine()) != null) {
    41. if (this.contains(team) == false) {
    42. this.teams.add(team);
    43. }
    44. }
    45.  
    46. reader.close();
    47. input.close();
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52.  
    53. public void save() {
    54. try {
    55. FileWriter stream = new FileWriter(this.teamfile);
    56. BufferedWriter out = new BufferedWriter(stream);
    57.  
    58. for (String team : this.teams) {
    59. out.write(team);
    60. out.newLine();
    61. }
    62.  
    63. out.close();
    64. stream.close();
    65.  
    66. } catch (IOException e) {
    67. e.printStackTrace();
    68. }
    69. }
    70.  
    71. public boolean contains(String value) {
    72. return this.teams.contains(value);
    73. }
    74.  
    75. public void add(String value) {
    76. if (this.teams.contains(value)) {
    77. return;
    78. }
    79. this.teams.add(value);
    80. }
    81.  
    82. public void remove(String value) {
    83. this.teams.remove(value);
    84. }
    85.  
    86. public ArrayList<String> getTeams() {
    87. return this.teams;
    88.  
    89. }
    90.  
    91. }
     
  5. Offline

    Icyene

    @Kofod Sorry, I don't see how that answers my question. The problem is not with the file. It loads the file fine, and I can run this.getConfig.getIntegerList("luring.loot.rawFish") and it will return
    [349, 10, 50, 331, 1].
     
  6. Offline

    Kodfod

    Ah, I'm sorry. I guess i should have read it over more. Thought that's what you were after
     
  7. Offline

    Icyene

    No problem.
     
  8. getConfigurationSection("luring.loot.")
    chance to
    getConfigurationSection("luring.loot")
     
    dark navi likes this.
  9. Offline

    Icyene

    ferrybig That was pasted by accident when I copied the code. It is luring.loot, and still isn't working.

    No ideas?

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

    UltraMC

    Than do it lame way:
    1. Convert ARRAY to STRING
    2. STRIP ","
    3. LOOP-x5: get INTEGER, save as separate VARIBLE, (and repeat)

    PS http://wiki.bukkit.org/Introduction_to_the_New_Configuration#Keys ?
     
  11. Offline

    Icyene

    No, that works fine. getIntegerList returns the integer array properly. Its just that I can't find how many integer arrays there are because getKeys is no working!
     
  12. Offline

    pzxc

    Try it like this:

    for (String key : getConfig().getConfigurationSection("luring.loot").getKeys(false)) {
    List<Integer> a = getConfig().getIntegerList("luring.loot."+key);
    ...
    otherStuff();
    ...
    }

    That will let you process each list of integers without having to worry about how many of them there are
     
  13. Offline

    Icyene

    pzxc Yes, I had a method like that, but the problem isn't how many there are. Its that getConfig().getConfigurationSection("luring.loot").getKeys(false) ALWAYS returns null!
     
  14. Offline

    pzxc

    check your config file for formatting errors
    for example in your original post in this thread, on line 3 where you define the rawFish integer list, you have 5 spaces when it should be 4
     
  15. Offline

    Icyene

    pzxc Thanks! I didn't notice that. Having corrected it, however, the following code:

    Code:JAVA
    1.  
    2. FileConfiguration config = getConfig();
    3. Set<String> a = null;
    4. a = config.getConfigurationSection("luring.loot").getKeys(false);
    5. System.out.println(a);
    6.  

    Still returns null. My config looks like:
    Code:
    luring:
      loot: 
        rawFish: [349, 10, 50, 331, 1]
    
    And yet it still returns an empty set. I will continue to try and get it to work.
     
  16. Offline

    pzxc

    It looks right to me. Only thing I can think of is that maybe you're not looking at the same configuration file that your plugin is using?
    In the same code try List<Integer> a = config.getIntegerList("luring.loot.rawFish") - does it show the numbers?
    or Set<String> a = config.getKeys(false) - does it show "luring"?
    Try to load other things from the config and make sure they work
     
  17. Offline

    Icyene

    pzxc getIntegerList Does show the set, but config.getKeys fails to return anything but an empty set.

    I've also redownloaded and referenced the Bukkit API and Craftbukkit (the latter probably useless in this case), but it still fails. Is there an alternative to the geyKeys method?

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

    UltraMC

    Have you tried looking into code of plugins that resolved it?
     
  19. Offline

    Icyene

    They didn't "resolve" it. They used the same method as me, its just that mine is failing for a currently unknown reason.

    Perhaps its just me? Could someone try compiling the following code an see if it returns something other then an empty set?

    Core.java
    Code:java
    1.  
    2. import java.util.Set;
    3. import java.lang.String;
    4.  
    5. import org.bukkit.configuration.file.FileConfiguration;
    6.  
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Core extends JavaPlugin {
    10.  
    11. public void onEnable() {
    12.  
    13. getLootFromConfig();
    14.  
    15. }
    16.  
    17. public void getLootFromConfig() {
    18. FileConfiguration config = getConfig();
    19. Set<String> a = null;
    20. System.out.println(config.getConfigurationSection("luring.loot")
    21. .getKeys(false));
    22. }
    23.  
    24. }
    25.  


    config.yml:

    Code:
    luring:
      loot: 
        rawFish:  [349, 10, 50, 331, 1] 
        rawFishx20:  [349, 20, 20, 331, 1]
        compass:  [345, 1, 10, 331, 1]
        paper:  [339, 1, 10, 331, 1]
        paperX10:  [339, 10, 5, 331, 1]
        boat:  [333, 1, 5, 331, 1]
        paintings:  [321, 1, 5, 331, 1]
        bone:  [352, 1, 5, 331, 1]
        map:  [358, 1, 5, 331, 1]
        chainChestPlate:  [303, 1, 1, 331, 10] 
        diamond:  [264, 1, 1, 331, 15] 
     
    
    Thanks in advance!

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

    pzxc

    I haven't compiled it, but try changing FileConfiguration to YamlConfiguration
     
  21. Offline

    Icyene

    Tried this:
    Code:java
    1. YamlConfiguration config = (YamlConfiguration) getConfig();
    but still no luck.
     
  22. Offline

    Sagacious_Zed Bukkit Docs

    This does nothing because under the hood your instance of FileConfiguration is a YamlConfiguation.

    Icyene
    The fact that it is not working, hints at some other problem, because that method works for everyone else.
     
  23. Offline

    Icyene

    Sagacious_Zed That is what is so frustrating. In theory, getIntegerList working would imply that getKeys would also work, but it doesn't. Collectively, I've spent 6 hours of my life trying to figure this out, and I've cut down almost all the possibilities for why it would not work. I am compiling with JSE 1.6, so no Java 7 issues. I have redownloaded BukkitAPI and craftbukkit, in case I had accidently deleted some files from them while I was bored. I have tried reinstalling eclipse in the infitestimally small chance that it was compiling wrong. Therefore I would say that there is something wrong with either the code or the config (or a small chance Bukkit). However, I wouldn't be asking if I wasn't absolutely sure that I couldn't figure it out myself. P.S. All the code you see in the earlier post is all the code I am compiling, so its impossible for another thing I'm compiling with it to be a factor in the problem.
     
  24. Offline

    Sagacious_Zed Bukkit Docs

    Icyene
    As implemented In the current SNAPSHOT as of this writing, getKeys will never return null. It can return an empty set, however.
    There is also one final note that the Javadocs does not mention, is that getKeys does not return default keys unless copyDefaults is true. Looking at the source code this is by design.

    I have taken the time to write a plugin that demonstrates this.

    Here the plugin LureCounter-0.0.1-SNAPSHOT.jar
    alsong with it's source LureCounter-0.0.1-SNAPSHOT-sources.jar
     
  25. Offline

    Icyene

    Thank you so much!
     
Thread Status:
Not open for further replies.

Share This Page