For Loop never Finishes

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

  1. I am working on a minigame plugin for a network, and when the game ends I calledendGame() 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 (Text):
    public boolean removePlayer(Player p) {
        if (!isInGame(p)) {
            p.sendMessage(Message.ERR_NOT_IN_GAME);
            return false;
        }
        Arena a = getArena(p);
        a.getGameScoreboard().removePlayer(p);
        p.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
        p.getInventory().clear();
        p.getInventory().setArmorContents(null);
        p.setGameMode(GameMode.SURVIVAL);
        p.teleport(SticksAndStones.getSpawn());
        players.remove(p);
        a.getPlayers().remove(p); // PROBLEM IS HERE
        if (a.getPlayerCount() < 2 && a.isStartCountdown()) {
            a.cancelCountdown();
        }
        if (a.getPlayerCount() < 2 && a.isRunning()) {
            a.endGame();
        }
        return true;
    }
    The whole problem is caused when I remove the player from the arena. This is theendGame() method:

    Code (Text):
    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?
     
  2. Try s for each loop.
    for(Player p : Bukkit.getOnlinePlayers()){
    // lel
    }
     
    • Like Like x 1
  3. I've tried that before, and it's no difference, except I didn't use Bukkit.getOnlinePlayers().. I'll see how that functions.
     
  4. My god, that fixed it, using Bukkit.getOnlinePlayers() then checking if they're in game. Thank you so much. :)
     
    • Like Like x 1