Solved For loop never finishes

Discussion in 'Plugin Development' started by CoderMusgrove, Apr 7, 2014.

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

    CoderMusgrove

    I am working on a minigame plugin for a network, and when the game ends I called endGame() but I am noticing that the for loop to remove all of the players never finish, it stops at the last person. It turns out the whole problem consists on when I remove the player from the list.
    Code:java
    1. public boolean removePlayer(Player p) {
    2. if (!isInGame(p)) {
    3. p.sendMessage(Message.ERR_NOT_IN_GAME);
    4. return false;
    5. }
    6. Arena a = getArena(p);
    7. a.getGameScoreboard().removePlayer(p);
    8. p.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
    9. p.getInventory().clear();
    10. p.getInventory().setArmorContents(null);
    11. p.setGameMode(GameMode.SURVIVAL);
    12. p.teleport(SticksAndStones.getSpawn());
    13. players.remove(p);
    14. a.getPlayers().remove(p); // THIS LINE
    15. if (a.getPlayerCount() < 2 && a.isStartCountdown()) {
    16. a.cancelCountdown();
    17. }
    18. if (a.getPlayerCount() < 2 && a.isRunning()) {
    19. a.endGame();
    20. }
    21. return true;
    22. }

    The whole problem is caused when I remove the player from the arena. This is the endGame() method:
    Code:
    for (int i = 0; i < players.size(); i++) {
        Player p = players.get(i);
        ArenaManager.getInstance().removePlayer(p);
    }
    I tried setting the loop with an integer called pc that gets the player list size, and then loops through to see if that was the problem, but that didn't fix it at all. The only way that I've seen able to fix it is to create a secondary for loop to remove that last player.

    Why is it removing all players except for the last one when I use a for loop to remove players?

    It's resolved by using a for loop of Bukkit.getOnlinePlayers() and checking if they're in the game.

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

Share This Page