Solved How to loop through a yml file...

Discussion in 'Plugin Development' started by The_Coder, Dec 15, 2012.

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

    The_Coder

    Hey everyone,
    I have a question about looping through a yml to get 4 values. I have looked on the forums but they have using static nodes unlike mine:
    Code:
    Users:
    <player's name>
      world: World
      X: -289
      Y: 63
      Z: 83
    So I have to get world, x, y, and z. I did a debug line and they turned out 0.0 for x, y, and z. Any Ideas?
     
  2. Offline

    Barinade

  3. Offline

    fireblast709

    if your point is getting them for each player:
    Code:java
    1. ConfigurationSection users = getConfig().getConfigurationSection("Users");
    2. if(users == null)
    3. {
    4. users = getConfig().createSection("Users");
    5. }
    6. Set<String> players = users.getKeys(false);
    7. for(String player : players)
    8. {
    9. int x = users.getInt(player+".X");
    10. // etc
    11. }
     
  4. Offline

    The_Coder

    It doesn't seem to be working
     
  5. Offline

    fireblast709

    what not?
     
  6. Offline

    The_Coder

    It doesn't get through the loop. I did a few print lines inside the loop and none of them work.
     
  7. Offline

    fireblast709

    what is your config.yml? (current one)
     
  8. Offline

    The_Coder

    Code:
     
    Users:
      bluebailey:
        world: World
        X: -291
        Y: 63
        Z: 88
    
     
  9. Offline

    fireblast709

    what lines?
     
  10. Offline

    The_Coder

    I get a NPE on line 160
    Code:
    ConfigurationSection users = config.getConfigurationSection("Users");
                            if(users == null) {
                               
                            }
                           
                            Set<String> players = users.getKeys(false);
                           
                            for(String player : players) {
                               
                                String wl = users.getString(".world");
    this is line 160-> World world = getServer().getWorld(wl);
                                int x = users.getInt(player + ".XSanta");
                                int y = users.getInt(player + ".YSanta");
                                int z = users.getInt(player + ".ZSanta");
                               
                                getServer().broadcastMessage("Yes");
                                //getServer().broadcastMessage("HE" + world);
                                getServer().broadcastMessage("X" + x);
                                getServer().broadcastMessage("Y" + y);
                                getServer().broadcastMessage("Z" + z);
                         
                            }
     
  11. Offline

    Cybermaxke

    What is on line 160?
     
  12. Offline

    The_Coder

    World world = getServer().getWorld(wl);
     
  13. Offline

    tommycake50

    wl must be null then.
     
  14. Offline

    fireblast709

    The_Coder use users.getString(player+".world", ""); Note that Bukkit.getWorld(String name) still can return null if the world does not exist ;3
     
  15. Offline

    The_Coder

    Well the world does exist but I am still getting a null pointer

    Should I use what CorrieKay said

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

    fireblast709

    your current code is correct (with the modification I gave you before)
     
  17. Offline

    The_Coder

    this is what I have in my onEnable:


    Code:
    getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
     
                @Override
                public void run() {
                    if (SMonth == Month && dayOfMonth == SDay && config.contains("Users.")) {
                        //if(Hour == 1200) {
                            getServer().broadcastMessage(
                                    ChatColor.AQUA + config.getString("MessageOnChristmas"));
                         
                         
                         
                            ConfigurationSection users = config.getConfigurationSection("Users");
                            if(users == null) {
                             
                            }
                         
                            Set<String> players = users.getKeys(false);
                         
                            for(String player : players) {
                             
                                String wl = users.getString(player + ".world", "");
                                World world = Bukkit.getWorld(wl);
                                int x = users.getInt(player + ".XSanta");
                                int y = users.getInt(player + ".YSanta");
                                int z = users.getInt(player + ".ZSanta");
                             
                                getServer().broadcastMessage("Yes");
                                getServer().broadcastMessage("HE" + world);
                                getServer().broadcastMessage("HE" + x);
                                getServer().broadcastMessage("HE" + y);
                                getServer().broadcastMessage("HE" + z);
                             
                             
                                //Location chestloc = new Location(world, x, y, z);
                                //chestloc.getBlock().setType(Material.CHEST);
                                //Chest chest = (Chest) chestloc.getBlock().getState();
                             
                                ItemStack dia = new ItemStack(Material.DIAMOND, 1);
                             
                                //chest.getInventory().addItem(dia);
                             
                                getServer().broadcastMessage(ChatColor.BLUE + "Ho...Ho...Ho");
                             
                                config.set("Users", null);
                                saveYamls();
                             
                             
                             
                             
                            }
             
                         
                        //}else {
                            getServer().broadcastMessage(ChatColor.GOLD + "Get a prenest from santa!");
                        //}
                    }
                }
            }, 60L, 60L);
    I get the christmas message but not anything else.
     
  18. Offline

    fireblast709

    Code:java
    1. if(users == null)
    2. {
    3. users = config.createSection("Users");
    4. // saveConfig(); - save the config if you are using the default config
    5. }

    Don't forget the important parts :3
     
Thread Status:
Not open for further replies.

Share This Page