Looping

Discussion in 'Plugin Development' started by BillyBobJoe168, Apr 12, 2014.

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

    BillyBobJoe168

    Hi I have a question about looping. I know there is a for loop but for what I am doing, this will not work. I am making a minigame plugin where the players are stored in String Lists in a config file and the config goes like so:
    There is a section called arenas which stores all the arenas as a string list. After, whenever you do /pvc create arenaname, it will create a section called arenaname and inside that, it will create some more sections. The only relevant ones are arenaname.police and arenaname.criminals. Now, when someone does /pvc leave, I want to loop through all the arenas and if they are in any of the arenaname.police or arenaname.criminals, I want to remove them and I have this but I am using a for loop which says the following:
    for every arena, if the player is in in the arenaname.police or arenaname.criminals, remove them and send them a message saying they left the game or else send them a message saying they aren't in a game. This is faulty because as I said, it is for every arena so therefore if I create 10 arenas and they are in only 1, It will send the player 9 messages saying they aren't in an arena and 1 saying they left the arena. Is there a way to tell them they left and NOT tell them they are not in 9 arenas and yet still keep the possibility that if they aren't in an arena then tell them so?

    Btw here is the code:
    Code:java
    1. if (args[0].equalsIgnoreCase("leave")) {
    2. for (String s : getConfig().getStringList("arenas")) {
    3. if (getConfig().getStringList(s + ".police").contains(player.getName())) {
    4. List<String> police = getConfig().getStringList(s + ".police");
    5. police.remove(player.getName());
    6. getConfig().set(s + ".police", police);
    7. saveConfig();
    8. for (String p : getConfig().getStringList(s + ".police")) {
    9. Player pol = getServer().getPlayer(p);
    10. pol.sendMessage(ChatColor.YELLOW + player.getName() + " has left the game!");
    11. }
    12. for (String c : getConfig().getStringList(s + ".criminals")) {
    13. Player crim = getServer().getPlayer(c);
    14. crim.sendMessage(ChatColor.YELLOW + player.getName() + " has left the game!");
    15. }
    16. player.sendMessage(ChatColor.YELLOW + "You have left the game!");
    17. break;
    18. }
    19. if (getConfig().getStringList(s + ".criminals").contains(player.getName())) {
    20. List<String> criminals = getConfig().getStringList(s + ".criminals");
    21. criminals.remove(player.getName());
    22. getConfig().set(s + ".criminals", criminals);
    23. saveConfig();
    24. for (String p : getConfig().getStringList(s + ".police")) {
    25. Player pol = getServer().getPlayer(p);
    26. pol.sendMessage(ChatColor.YELLOW + player.getName() + " has left the game!");
    27. }
    28. for (String c : getConfig().getStringList(s + ".criminals")) {
    29. Player crim = getServer().getPlayer(c);
    30. crim.sendMessage(ChatColor.YELLOW + player.getName() + " has left the game!");
    31. }
    32. player.sendMessage(ChatColor.YELLOW + "You have left the game!");
    33. break;
    34. } else {
    35. player.sendMessage(ChatColor.RED + "You are not in a game!");
    36. }
    37. }
    38. }


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

    amhokies

    BillyBobJoe168
    This would probably be so much easier if you just had an Arena class with a HashSet to store player names...
     
  3. Offline

    BillyBobJoe168

    Alright how would I do this? I've never used HashSets before...I want to be able to have multiple arenas so if you need to make 2 hashsets per arena, I would like to be able to name them like arenaname + "police"

    Oh and if you do have a way to solve this I would appreciate it because I have a whole bunch of stuff coded and I don't really want to change my whole code. It's pretty big now

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

    amhokies

    You don't understand. You wouldn't be using the config in the way you are now.

    A Set is sort of like a List. It's a Collection, used to store Objects, except that duplicate Objects aren't allowed. You don't want to store the same Player name twice.

    Basically what you will do is create a class called Arena. Here, you will have all the attributes of an Arena- The arena name, the players, the spawn point, etc. as well as the methods- getName, addPlayer, removePlayer, etc. These are kind of basic object oriented programming basics, so I'm not really sure how much detail I need (or am willing) to go into.
     
Thread Status:
Not open for further replies.

Share This Page