Loading a list from a config.

Discussion in 'Plugin Development' started by LegoPal92, Nov 4, 2013.

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

    LegoPal92

    So, the title makes it sound pretty easy, and I hope it is, as I am okay with feeling like a derp today.

    So, what I am trying to do is load a list from an unknown name.

    Like this:
    PHP:
    test:
      
    winBlock:
        
    Worldworld
        X
    128
        Y
    80
        Z
    272
      spawn
    :
        
    Worldworld
        X
    135
        Y
    68
        Z
    263
      lobby
    :
        
    Worldworld
        X
    134
        Y
    82
        Z
    281
      sign3
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    268
      sign2
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    269
      sign1
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    270
    In this case, I do not know test, let me throw in another example.


    PHP:
    test:
      
    winBlock:
        
    Worldworld
        X
    128
        Y
    80
        Z
    272
      spawn
    :
        
    Worldworld
        X
    135
        Y
    68
        Z
    263
      lobby
    :
        
    Worldworld
        X
    134
        Y
    82
        Z
    281
      sign3
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    268
      sign2
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    269
      sign1
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    270
    blah
    :
      
    winBlock:
        
    Worldworld
        X
    128
        Y
    80
        Z
    272
      spawn
    :
        
    Worldworld
        X
    135
        Y
    68
        Z
    263
      lobby
    :
        
    Worldworld
        X
    134
        Y
    82
        Z
    281
      sign3
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    268
      sign2
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    269
      sign1
    :
        
    Worldworld
        X
    121
        Y
    69
        Z
    270
    In this last case, I do not know test or blah, how would I get them?

    I have tried:

    Code:java
    1. for (String s : arenaConfig.getStringList("")){
    2. KOTL.log.severe(KOTL.tag + "after for s in getStringList -D-");
    3.  
    4. HashMap<String, Location> blah = new HashMap<String, Location>();
    5. KOTL.log.severe(KOTL.tag + s);
    6. for (String s2 : arenaConfig.getStringList(s)){
    7. KOTL.log.severe(KOTL.tag + s2);
    8. String wName = arenaConfig.getString(s + "." + s2 + ".World");
    9. double x = arenaConfig.getDouble(s + "." + s2 + ".X");
    10. double y = arenaConfig.getDouble(s + "." + s2 + ".Y");
    11. double z = arenaConfig.getDouble(s + "." + s2 + ".Z");
    12. Location one = new Location(KOTL.plugin.getServer().getWorld(wName), x, y, z);
    13. blah.put(s2, one);
    14. }


    In my mind that should have worked properly, but there was no output, I did not get to any of my debug line at all.

    Any help that you could give me would be greatly appreciated.

    Thanks in advance,
    LegoPal92.
     
  2. Offline

    sgavster

    I'd like to know also.. *follows*
     
  3. Offline

    GusGold

    LegoPal92
    You may be interested in the getConfig.getKeys(true); method. As explained here, it returns the key, and their children's keys recursively.
     
    LegoPal92 likes this.
  4. Offline

    LegoPal92

    Thanks, I'll try that.
    EDIT: Thanks, GusGold it worked!

    Now, on to the next problem.

    I am trying to save an arena to MySQL, the syntax is fine, but the Location is null.

    Here is the method that I am using.

    Code:java
    1.  
    2.  
    3.  
    4. public static void loadArenas(){
    5. KOTL.log.severe(KOTL.tag + "after loadArenas -D-");
    6. HashMap<String, HashMap<String, Location>> arenaParts = getLocations();
    7. for (String s : arenaParts.keySet()){
    8. KOTL.log.severe(KOTL.tag + s);
    9.  
    10. Location winBlock = null;
    11. Location spawn = null;
    12. Location lobby = null;
    13. Location sign1 = null;
    14. Location sign2 = null;
    15. Location sign3 = null;
    16. HashMap<String, Location> locations = arenaParts.get(s);
    17. for (String s2 : locations.keySet()){
    18. if (s2.equalsIgnoreCase("winBlock")){
    19. winBlock = locations.get(s2);
    20. } else if (s2.equalsIgnoreCase("spawn")){
    21. spawn = locations.get(s2);
    22. } else if (s2.equalsIgnoreCase("lobby")){
    23. lobby = locations.get(s2);
    24. } else if (s2.equalsIgnoreCase("sign1")){
    25. sign1 = locations.get(s2);
    26. } else if (s2.equalsIgnoreCase("sign2")){
    27. sign2 = locations.get(s2);
    28. } else if (s2.equalsIgnoreCase("sign3")){
    29. sign3 = locations.get(s2);
    30. }
    31. }
    32.  
    33. Arena a = new Arena(spawn, winBlock, lobby, sign1, sign2, sign3, s);
    34. KOTL.log.severe(KOTL.tag + a.getMapName() + " has been loaded.");
    35. LoadArenas.saveArena(a);
    36. }


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

    GusGold

  6. Offline

    LegoPal92

    Oh that, the getLocations() line?

    Just a sec.

    Code:java
    1. public static HashMap<String, HashMap<String, Location>> getLocations(){
    2. HashMap<String, HashMap<String, Location>> toBeReturned = new HashMap<String, HashMap<String, Location>>();
    3. KOTL.log.severe(KOTL.tag + "after getLocations -D-");
    4. for (String s : arenaConfig.getKeys(false)){
    5. KOTL.log.severe(KOTL.tag + "after for s in getStringList -D-");
    6.  
    7. HashMap<String, Location> blah = new HashMap<String, Location>();
    8. KOTL.log.severe(KOTL.tag + s);
    9. for (String s2 : arenaConfig.getStringList(s)){
    10. KOTL.log.severe(KOTL.tag + s2);
    11. String wName = arenaConfig.getString(s + "." + s2 + ".World");
    12. double x = arenaConfig.getDouble(s + "." + s2 + ".X");
    13. double y = arenaConfig.getDouble(s + "." + s2 + ".Y");
    14. double z = arenaConfig.getDouble(s + "." + s2 + ".Z");
    15. Location one = new Location(KOTL.plugin.getServer().getWorld(wName), x, y, z);
    16. blah.put(s2, one);
    17. }
    18.  
    19. toBeReturned.put(s, blah);
    20. }
    21. return toBeReturned;
    22. }
     
  7. Offline

    GusGold

    LegoPal92
    You might want to check that arenaParts.get(s); isn't returning null, and the same with locations.get(s2); Do a
    Code:java
    1. if (something == null)
    2. getLogger().severe("NULL!!!!!");
     
  8. Offline

    LegoPal92

    I just did, and it turns out that it is not null. Any more suggestions?
     
  9. Offline

    GusGold

    LegoPal92
    So locations.get(winBlock) != null?
     
  10. Offline

    LegoPal92

    I guess I misunderstood what you were asking. I tested if arenaParts.get(s) was null.

    That locations.get("winBlock") is null. Maybe I cannot store a HashMap inside of a HashMap. If not, what would you suggest me to use?
     
  11. Offline

    GusGold

    LegoPal92
    Alright, so I reckon that this bit is your problem (line inside your getLocation() function):
    Code:java
    1. KOTL.plugin.getServer().getWorld(wName)
    If the server can't find a world by that name, it will return null, so make sure
    Code:java
    1. arenaConfig.getString(s + "." + s2 + ".World");
    is returning the name of a currently loaded world.
     
  12. Offline

    LegoPal92

    GusGold Is the world loaded when plugins are enabling? because if not, that would be the cause, the world that I am testing, and that it is retrieving in is the main world.
     
  13. Offline

    GusGold

    LegoPal92
    So long as you haven't added something like "load: STARTUP" to your plugin.yml, your plugin will load after the world has. the issue may be capitalization or you have set a different world in your server.properties file?
     
  14. Offline

    LegoPal92

    I have not added that in my plugin.yml, The capitalization is the same, I know for certain because the way I am saving it is using Location.getWorld.getName(); The world is the same as the one in the server.properties as well.
     
  15. Offline

    GusGold

    LegoPal92
    I have had troubles with getting the world object from a string before. A sure-fire way would be to
    Code:java
    1. World worldIWant = null;
    2. List<World> worlds = getServer.getWorlds();
    3. for (World world : worlds){
    4. if (world.getName().equalsIgnoreCase(arenaConfig.getString(s + "." + s2 + ".World")){
    5. worldIWant = world;
    6. break;
    7. }
    8. }
    This loops through all loaded worlds and sees if any equal your worlds' string name
     
  16. Offline

    LegoPal92

    Thanks for all your help, I have realized that my error was that this is not a String List, and that what I should do is hard code the getting the Spawn, winBlock, etc. If I run in to problems later on, I will tahg you again, if that's alright.
     
  17. Offline

    JPG2000

    GusGold Random question:

    When ever I try to set a list as an ArrayList(Im pretty sure it takes a ArrayList) it turns out as:
    {}

    Any examples as using a list?
     
  18. Offline

    GusGold

    JPG2000 Nothing like threadjacking, but anyway :3
    Do you mean like
    Code:java
    1. List<String> list = new ArrayList<String>();
    ?
     
  19. Offline

    JPG2000

    GusGold Yeah.

    I know java, I know how to make an ArrayList. Just when ever I either set or get (specificly set) it just sets it as {}.

    Example:
    Code:java
    1. Config config; //Pretend this is set up
    2.  
    3. ArrayList<String> list = new ArrayList<String>();
    4. list.add("hello");
    5.  
    6. public void set() {
    7. config.set("flower", list);
    8. }


    It would turn out as:
    flower: {}


    EDIT:
    I'll try it again. Also, if I would wanto get all the values like so:
    Code:
    world:
    - mario
    - sethbling
    - yoga
    - soup
    Would I do so like this:
    Code:java
    1. for (String s: config.getList("mario")) {
    2. //S would be 'mario' 'sethbling' yadayada
    3. //Code
    4. }
     
  20. Offline

    GusGold

    JPG2000
    Try getConfig().getStringList("path.to.string.list");
     
  21. Offline

    JPG2000

    GusGold I men't getStringList, I was thinking out of my head, and I didn't look through the method list. I tried it again, and it worked. Seemed as my list was null ;(

    Anyways, does the list always return a string?
     
  22. Offline

    GusGold

    JPG2000 Yes, but you can do stuff like Integer.parseInt("123"); and Boolean.parseBoolean("false");
     
Thread Status:
Not open for further replies.

Share This Page