Automatic team assignment dont work. for loop, arraylists

Discussion in 'Plugin Development' started by mollekake, Oct 22, 2012.

Thread Status:
Not open for further replies.
  1. Here's what i want to accomplish:
    when players join, they get put into a Players list. When10 ppl are on things happen and they are supposed to get placed into each team list, and removed from the Players list.

    Here's what happens:
    (been testing with 2 players)
    when the team assignment loop start, they both end up in one team, teamblue, and only one of them gets TPed and receives items.

    the loop does loop through the list twice, but don't do what i want it to.
    Code that dont work:
    Code:
       
    protected void startGame(Player p) { // called when the tenth player logs on, in this implementation       
            PlayerInventory inventory = p.getInventory();
            ItemStack sword = new ItemStack(272, 1); // A wooden sword
            ItemStack snowballs = new ItemStack(332, 16); //16 snowballs
            ItemStack bow = new ItemStack(261, 1); // A bow
            ItemStack arrows = new ItemStack(262, 32); // 32 arrows
           
            inventory.addItem(snowballs);
            inventory.addItem(sword);
            inventory.addItem(bow);
            inventory.addItem(arrows);
            System.out.print(Players);
           
            boolean r = random.nextBoolean();
            if(r == true){
                Match1.add(true);
                Match2.add(false);
            }else if(r == false){
                Match1.add(false);
                Match2.add(true);
            }
           
           
            for(Player player : Players){
                System.out.print("loop");
                if (redTeam.size() > blueTeam.size()) {
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    if(Match1.contains(true)){
                        p.teleport(plugin.Loc.get("Blue1"));
                    }else if(Match2.contains(true)){
                        p.teleport((Location) plugin.Loc.get("Blue2"));
                    }
                }else{
                    blueTeam.add(player); 
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    if(Match1.contains(true)){
                        p.teleport(plugin.Loc.get("Blue1"));
                    }else if(Match2.contains(true)){
                        p.teleport((Location) plugin.Loc.get("Blue2"));
                    }
                }
                //Players.remove(p);
            }
           
           
            //Players.remove(p);
            System.out.print(redTeam);
            System.out.print(blueTeam);
            System.out.print(Players);
            startGameTime(p);
        }
     
  2. Offline

    fritzwalter

    Code:
     if (redTeam.size() > blueTeam.size()) {
    blueTeam.add(player);
    [...]
    else{
    blueTeam.add(player);

    You add the player both times to team blue, thats the reason.
     
  3. lmao. i removed the wrong part it seems, but i've fixed it now to the correct. same thing still happens:
    Code:
    }else{
                    redTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "TeamRed" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.RED + "red" + ChatColor.DARK_GRAY + "!");
                    if(Match1.contains(true)){
                        p.teleport(plugin.Loc.get("Red1"));
                    }else if(Match2.contains(true)){
                        p.teleport((Location) plugin.Loc.get("Red2"));
                    }
                }
     
  4. Offline

    fritzwalter

    Hmmm, it should work now.

    If you would upload the complete code i could take a look on it.
     
  5. i dont really see the point of posting the entire code.
    here is what the console says:
    14:50:12 [INFO] Game starts NOW!
    14:50:12 [INFO] [CraftPlayer{name=mollekake}, CraftPlayer{name=Player}]
    14:50:12 [INFO] loop
    14:50:12 [INFO] loop
    14:50:12 [INFO] []
    14:50:12 [INFO] [CraftPlayer{name=mollekake}, CraftPlayer{name=Player}]
    14:50:12 [INFO] [CraftPlayer{name=mollekake}, CraftPlayer{name=Player}]
    14:50:12 [INFO] 20 seconds until game ends!

    here is the lists:
    List<Player> redTeam = new ArrayList<Player>();
    List<Player> blueTeam = new ArrayList<Player>();
    List<Player> Players = new ArrayList<Player>();
     
  6. Offline

    fritzwalter

    I'm willing to help you but can't find the bug in the posted code.....
    And if you upload the code i can test it by my self, so it is easier to find the bug.

    Try to write System.out.println() before adding the players to the teams.....
     
  7. And i appreciate it, but i'd rather not post it, as i might want the source for myself :p besides, it's well above 500 lines
    As you see in the console, i do a system out print of Players before the team adding, and then do system out print on:
    redteam
    blueteam
    players
    after the team thing

    so it don't put any player in redteam, put both in blue team, and only give items to 1 player and only teleports 1 player.
    it also tells the blue player twice that it's in blue team :S

    anyone else can see why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  8. Offline

    the_merciless

    if (redTeam.size() > blueTeam.size())

    this is where you are going wrong, assuming both start at 0, the 1st player will be placed into blue team, and then red will not be higher than blue so it will continue to always add to the blue team. Maybe use a bit of math,



    max = math.max(redTeam.size, blueTeam.size);
    if (redTeam.size == blueTeam.size){
    // place into redTeam
    }else if (redTeam.size == max){
    // place into blueTeam
    }else if (blueTeam.size == max){
    // place into redteam
    }

    Something like that
     
    calebbfmv likes this.
  9. changed the code a little, to what it was before.
    This is the consol output:
    01:09:34 [INFO] Game starts NOW!
    01:09:34 [INFO] [CraftPlayer{name=Player}, CraftPlayer{name=mollekake}]
    01:09:34 [INFO] loop
    01:09:34 [INFO] blueteam
    01:09:34 [INFO] [CraftPlayer{name=Player}]
    01:09:34 [INFO] loop
    01:09:34 [INFO] redteam
    01:09:34 [INFO] [CraftPlayer{name=mollekake}]
    01:09:34 [INFO] [CraftPlayer{name=mollekake}]
    01:09:34 [INFO] [CraftPlayer{name=Player}]
    01:09:34 [INFO] []

    this is the code:
    Code:
           
    for(Player player : Players){         
                System.out.print("loop");
                if (redTeam.size() == blueTeam.size()) {
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }else if (redTeam.size() < blueTeam.size()) {
                    redTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "TeamRed" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.RED + "red" + ChatColor.DARK_GRAY + "!");
                    System.out.print("redteam");
                    System.out.print(redTeam);
                }else if(redTeam.size() > blueTeam.size()){
                    blueTeam.add(player); 
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }
            }
            inventory.addItem(snowballs);
            inventory.addItem(sword);
            inventory.addItem(bow);
            inventory.addItem(arrows);
            Players.clear();
            System.out.print(redTeam);
            System.out.print(blueTeam);
            System.out.print(Players);
            tp(p);
            //startGameTime(p);
        }
    the results are:
    one player gets placed on one team, then the other one, and then gets the items and then gets tped. but nothing happens with the other player, although the system out print clearly states that there is one player on each team.
    i dont get it :(
     
  10. Offline

    Tirelessly

    Because at the end of your loop you're clearing the list that you're iterating through, so it only gets to the first index then gets wiped.

    EDIT: Also, store player names and not their instance.
     
  11. Offline

    the_merciless

    Your else statements are inside the first if statement. You need to separate them.

    For (players){

    If (red == blue){
    Do this
    }

    Else if (red < blue){
    Do this
    }

    Else if (blue < red){
    Do this
    }

    Tp player
    }
     
  12. i don't see what you guys mean, it seems right. this is the for:
    Code:
            for(Player player : Players){            
                System.out.print("loop");
                if (redTeam.size() == blueTeam.size()) {
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }else if (redTeam.size() < blueTeam.size()) {
                    redTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "TeamRed" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.RED + "red" + ChatColor.DARK_GRAY + "!");
                    System.out.print("redteam");
                    System.out.print(redTeam);
                }else if(redTeam.size() > blueTeam.size()){
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }
            }
    and this is outside:
    Code:
            inventory.addItem(snowballs);
            inventory.addItem(sword);
            inventory.addItem(bow);
            inventory.addItem(arrows);
            Players.clear();
            System.out.print(redTeam);
            System.out.print(blueTeam);
            System.out.print(Players);
            tp(p);
    i dont wipe players before after the loop. when i did it inside the loop it didn't work at all.
     
  13. Offline

    the_merciless

    Why not just remove the players 1 at a Time instead of clearing the list.

    Players.remove(p)

    Also it is not wise to store players in a list/map. It can cause memory leaks. Instead store just Thier name
    String pname = p.getname();

    And the change your map/list from Player to string
     
  14. i've tried that also, tried to remove when they get added to new team, one at a time, but it seems that it just clears the list.

    i know i should use the name, but that makes it hard when i'm trying to check inside my onDamage if they are on the same team.

    No matter what i do, it seems that the for loop don't work, it only affects 1 player, not everyone in the list.
    this is the last thing i tried now:
    Code:
    boolean nextTeam = true;
            for(Player player : Players){
                System.out.print("loop");
                if(nextTeam){
                    redTeam.add(player.getName());
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "TeamRed" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.RED + "red" + ChatColor.DARK_GRAY + "!");
                    System.out.print("redteam");
                    System.out.print(redTeam);
                }else{
                    blueTeam.add(player.getName());
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }
                nextTeam = !nextTeam;
            }
    and this is what the console outputs:
    21:36:55 [INFO] [CraftPlayer{name=mollekake}, CraftPlayer{name=Player}]
    21:36:55 [INFO] loop
    21:36:55 [INFO] redteam
    21:36:55 [INFO] [mollekake]
    21:36:55 [INFO] loop
    21:36:55 [INFO] blueteam
    21:36:55 [INFO] [Player]
    21:36:55 [INFO] [mollekake]
    21:36:55 [INFO] [Player]
    21:36:55 [INFO] [CraftPlayer{name=mollekake}, CraftPlayer{name=Player}]

    the last 3 lines are, systemoutprint redteam, blueteam and Players.

    and the thing that happens to the one player it works for is that he first gets placed in red team, then in blue team.
    makes no sense...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  15. Offline

    the_merciless

    Try this

    Code:
     for(Player player : Players){           
                System.out.print("loop");
                if (redTeam.size() == blueTeam.size()) {
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    players.remove(p);
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }else if (redTeam.size() < blueTeam.size()) {
                    redTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "TeamRed" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.RED + "red" + ChatColor.DARK_GRAY + "!");
                    players.remove(p);
                    System.out.print("redteam");
                    System.out.print(redTeam);
                }else if(redTeam.size() > blueTeam.size()){
                    blueTeam.add(player);
                    p.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "TeamBlue" + ChatColor.DARK_GRAY + "] " + p.getName() + ChatColor.RESET);
                    p.sendMessage(ChatColor.GRAY + "You are in team " + ChatColor.BLUE + "blue" + ChatColor.DARK_GRAY + "!");
                    players.remove(p);
                    System.out.print("blueteam");
                    System.out.print(blueTeam);
                }
            }
     
  16. i figured out what i did wrong. I apparently didn't think about everything :p
    what i did wrong:

    when the required player joined (the 2nd one in this case), all the actions after that join part happened to only that player because i did some stuff wrong in my methods. So i solved it, and it works! thanks for all help and suggestions!
    The for loop was correct all along, but the player declerations were wrong
     
  17. Offline

    the_merciless

    Glad you worked it out on your own.
     
Thread Status:
Not open for further replies.

Share This Page