Solved For loop problem

Discussion in 'Plugin Development' started by CheesyFreezy, Feb 1, 2015.

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

    CheesyFreezy

    Heey developers,
    when i run this loop it returns 0.

    Code:
    for(int i=0;i<core.getConfig().getKeys(false).size();i++) {
                            player.sendMessage("" + i);
                           
                            String iString = Integer.toString(i);
                           
                            if(core.getConfig().getConfigurationSection(iString).getString("Name").equalsIgnoreCase(event.getLine(2).toString())) {
                                player.sendMessage("4");
                                player.sendMessage(Core.prefix + ChatColor.DARK_GREEN + "Start " + ChatColor.GREEN + "sign created!");
                                event.setLine(1, ChatColor.YELLOW + "Start");
                                event.setLine(2, ChatColor.YELLOW + core.getConfig().getConfigurationSection(iString).getString("Name"));
                            } else {
                                event.getBlock().setType(Material.AIR);
                                player.playSound(player.getLocation(), Sound.STEP_STONE, 10, 0);
                                player.sendMessage(Core.prefix + ChatColor.RED + "This map doesn't exist!");
                            }
                        }
     
  2. Offline

    nverdier

    @CheesyFreezy What do you mean it 'returns' 0? Do you mean it sends the player the message '0'? Because a for loop doesn't return anything.
     
  3. Offline

    Skionz

    @CheesyFreezy Are the sections in your YAML file labeled with numbers? I think your logic may be a bit off.
     
  4. Offline

    CheesyFreezy

    @nverdier it returns 0 when i do player.sendMessage("" + i);
    @Skionz the configurationsections are all strings. But i'm getting the size of the amount of configurationsectionsections. So it's an integer
     
  5. Offline

    Skionz

    @CheesyFreezy Your attempting to get values using keys such as 0, 1, 2, 3, 4, 5, etc. Based on your code this is what it should look like
    Code:
    0:
        Name: 'Stuff'
    1:
        Name: 'Stuff'
    2:
        Name: 'Stuff'
    3:
        Name: 'stuff'
    I'm not sure if thats a logic error.
     
  6. Offline

    CheesyFreezy

    @Skionz
    that's exactly what i want.... but what's the problem? why is it not working?
     
  7. Offline

    Burnett

    @CheesyFreezy Iterate over the keys instead of using an int to get them? Would be easier.
     
  8. Offline

    CheesyFreezy

  9. Offline

    Burnett

  10. Offline

    CheesyFreezy

    @Burnett
    getKeys(false) returns [1,2]
    getKeys(false).size() returns 2
     
  11. Offline

    Konato_K

    @Skionz Just a little note, as far I know you should use '0' instead of just 0 when you use just a number in the yml (or at least that's what bukkit seems to do)
     
  12. Offline

    1Rogue

    It doesn't matter, it will always be a string key. That's simply what happens when bukkit stores the key as a string and saves it.
     
  13. Offline

    Konato_K

    @1Rogue Ahh, alright, thanks for the clarification :)
     
  14. Offline

    Burnett

    @CheesyFreezy No, getKeys() returns a Set<String> containing your keys. You can then use:

    Code:java
    1. for(String s : core.getConfig().getKeys(false))
     
    CheesyFreezy likes this.
  15. Offline

    CheesyFreezy

    @Burnett
    Okay i'll try that

    @Burnett,
    it's returning 1, eventhough i'm having 2 configurationsections

    EDIT: It's working now, i had to reload the server, now it s working. Thanks for the help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page