for() not running

Discussion in 'Plugin Development' started by LazerAspect, May 24, 2016.

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

    LazerAspect

    for loop (in seperate class from where buildGame is located):

    Code:
    public void onLeave(PlayerQuitEvent e){
            ArrayList<Player> players = new ArrayList<Player>();
            Player p = e.getPlayer();
           
            if (ArenaPVPArenas.buildQueue.contains(p)){
    
            }
            if (ArenaPVPArenas.buildGame.contains(p)){
                Bukkit.broadcastMessage("debug");
                for (Player player : ArenaPVPArenas.buildGame){
                    players.add(player);
                    ArenaPVPArenas.endBuild(players.get(0), players.get(1));
                    players.get(0).performCommand("ac test");
                    Bukkit.broadcastMessage(players.get(0).getName() + players.get(1).getName());
                    Bukkit.broadcastMessage(player.getName());
                    ArenaPVPArenas.buildGame.remove(p);
                    Bukkit.broadcastMessage("debug");
                }   
            }   
        }
    buildGame hashset:

    Code:
    public static HashSet<Player> buildGame = new HashSet<Player>();
    Basically, everything in onLeave() works fine until the for loop. The first debug message displays, but the one inside the loop does not.
     
  2. Offline

    mine-care

    a few things,
    first of all don't abuse static.
    Secondly, don't store Player objects in collections/maps or in general anywhere there they will remain after the player disconnects. That causes ram leaks.
    Lastly encapsulate!
    I supose that you also get an error that you didn't mention unless you catch exceptions without handling them which is another issue. As you see, you try to get index 1 from players which in the first run, has 1 element in it. So you try to access an inexistant element.

    Why is a List required there? you only access the two first indexes of it...

    Also,
    That might cause a ConcurentModificationException since it is what you loop through and you modify it at the same time...

    More information here
     
    Lordloss likes this.
Thread Status:
Not open for further replies.

Share This Page