Dividing Players Fairly To Teams

Discussion in 'Spigot Plugin Development' started by toxiccoke, May 18, 2015.

  1. Hi
    I am trying to make a paintball mini-game but i am having some troubles when adding players to teams
    to i want to make this so it will add a player to a team when they join i have done this here but for some reason when a player joins it will take the player that was already online and move it to the other team i cannot work out why if some one could help me out that would be great.
    Code (Text):
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            int i = 0;
            for(Player p : Bukkit.getOnlinePlayers()){
                if(i < Bukkit.getOnlinePlayers().length / 2){
                    redTeam.add(p.getName());
                    p.setPlayerListName(ChatColor.RED + p.getName());
                    ChatUtils.sendMessage(p, "You Have Been Added To The Red Team");
                }else{
                    blueTeam.add(p.getName());
                    p.setPlayerListName(ChatColor.BLUE + p.getName());
                    ChatUtils.sendMessage(p, "You Have Been Added To The Blue Team");
                   
                }
                i++;
           
        }
    If you need more information or more code i am happy to supply it
    Thanks toxiccoke
     
  2. I Will Think About It!
     
    • Funny Funny x 1
  3. -.-
    Either give the solution or don't post at all.

    OP, you should store the players in lists/hashmaps.
    Code (Text):
    List<String> redteam = new ArrayList<String>();
    List<String> blueteam = new ArrayList<String>();

    // on join, blah, blah
    if(redteam().size() < blueteam().size()) {
        redteam.add(p.getName());
    }
     
    • Friendly Friendly x 2
  4. Maybe this?

    Code (Java):
    if (redTeam.size() > blueTeam.size()) {
    blueTeam.add(p.getName());
    // if the red team is larger than the blue team, add them to the blue team
    }
    else if (blueTeam.size() > redTeam.size()) {
    // if the blue team is larger than the red team, add them to the red team
    redTeam.add(p.getName());
    }
    else {
    // if neither of the above is the case, lets assign them to a random team
    Random r = new Random();
        if(r.nextBoolean())
        {
    redTeam.add(p.getName());
        }
        else
        {
    blueTeam.add(p.getName());
        }}
     
    #4 Arman, May 18, 2015
    Last edited: May 18, 2015
  5. Seems about right.