How to assign online players between 4 teams?

Discussion in 'Plugin Development' started by IconByte, Feb 23, 2015.

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

    IconByte

    Hello.
    I need help with assigning online players between 4 teams.. With 2 teams it is easy:
    Code:
    if (blueteam < redteam) {
    // add to blue team
    } else {
    // add to red team
    }
     
  2. Offline

    teej107

    @IconByte Split the Players into 2 Lists, then split the 2 Lists again.

    You could also use a for-loop and have an int represent a List and when a player is added to a List, change the int. This way would be better.
     
  3. Offline

    IconByte

    @teej107 So you mean that?
    Code:
    if (blueteam < redteam) {
    // add to blue team
        if (greenteam < yellowteam) {
            // add to green team
        } else {
            // add to yellow team
        }
    } else {
    // add to red team
    }
     
  4. Offline

    teej107

    @IconByte no

    EDIT:

    The less efficient way would be to use a method that takes a Collection and returns 2 collections aka splitting it in 2. The more efficient way would be
     
  5. @IconByte
    You have some teams (java.util.Set)
    Code:
    Map<String, Set<UUID>> teams = new HashMap<String, Set<UUID>>();
    The key is the name of the team. for example red, green, yellow and blue
    Code:
    teams.put("red", new HashSet<String>());
    teams.put("blue", new HashSet<String>());
    teams.put("yellow", new HashSet<String>());
    teams.put("green", new HashSet<String>());
    This way you can add as many teams as you want and it will work anyways. Then use this method to add a player to a team
    Code:
    public void addPlayerToTeam(final Player p){
    if(!teams.isEmpty()){
      Set<String> teamNames = teams.keySet();
      Set<UUID> team = null;
      for(String name : teamNames){
       if(team!=null){
        Set<UUID> nextTeam = teams.get(name);
        if(team.size()>nextTeam.size()){
         team = nextTeam;
        }
       }
       else{
        team = teams.get(name);
       }
      }
    }
    team.add(p.getUUID());
    }
    It loops through all registered teams and picks the one with the lowest amount of member. Then it adds the uuid of the player to it and you're done.

    EDIT: should work
     
    Last edited: Feb 23, 2015
  6. Offline

    IconByte

    @Shmobi Thanks! I have been working at the moment, but I have some problems with this, it adds the player to team, but when the player quits, then it does not remove him from the team and gives NPE error...:

    onPlayerJoin:
    Code:
                for (Player onlinePlayers : Bukkit.getOnlinePlayers()) {
                    if (!(TeamClass.isInTeam(onlinePlayers))) {
                        if (plugin.teamValue == 0) {
                            TeamClass.join(onlinePlayers, TeamClass.getTeam("Blue"));
                            plugin.Scoreboard1.getTeam("Blue").addPlayer(onlinePlayers);
                            plugin.teamValue++;
                            continue;
                        }
                        if (plugin.teamValue == 1) {
                            TeamClass.join(onlinePlayers, TeamClass.getTeam("Green"));
                            plugin.Scoreboard1.getTeam("Green").addPlayer(onlinePlayers);
                            plugin.teamValue++;
                            continue;
                        }
                        if (plugin.teamValue == 2) {
                            TeamClass.join(onlinePlayers, TeamClass.getTeam("Red"));
                            plugin.Scoreboard1.getTeam("Red").addPlayer(onlinePlayers);
                            plugin.teamValue++;
                            continue;
                        }
                        if (plugin.teamValue == 3) {
                            TeamClass.join(onlinePlayers, TeamClass.getTeam("Yellow"));
                            plugin.Scoreboard1.getTeam("Yellow").addPlayer(onlinePlayers);
                            plugin.teamValue = 0;
                            continue;
                        }
                    }
                    onlinePlayers.setScoreboard(plugin.Scoreboard1);
                }
    onPlayerQuit:
    Code:
                for (Player onlinePlayers : Bukkit.getOnlinePlayers()) {
                    if (TeamClass.isInTeam(onlinePlayers)) {
                        if (TeamClass.getTeam(player).getName().equals("Blue")) {
                            TeamClass.leave(player);
                            plugin.Scoreboard1.getTeam("Blue").removePlayer(player);
                            continue;
                        }
                        if (TeamClass.getTeam(player).getName().equals("Green")) {
                            TeamClass.leave(player);
                            plugin.Scoreboard1.getTeam("Green").removePlayer(player);
                            continue;
                        }
                        if (TeamClass.getTeam(player).getName().equals("Red")) {
                            TeamClass.leave(player);
                            plugin.Scoreboard1.getTeam("Red").removePlayer(player);
                            continue;
                        }
                        if (TeamClass.getTeam(player).getName().equals("Yellow")) {
                            TeamClass.leave(player);
                            plugin.Scoreboard1.getTeam("Yellow").removePlayer(player);
                            continue;
                        }
                    }
                    onlinePlayers.setScoreboard(plugin.Scoreboard1);
                }
     
  7. @IconByte a) dont just throw code at me. tell me what the code is supposed to do and what doesnt work. b) show me all code i need to see to understand. Your class "TeamClass" would be usefull for me
     
  8. Offline

    IconByte

    The first code supposed to add player who joins into TeamClass's team and Scoreboard's team, so he will get nametag colored aswell.

    The second code supposed to remove player who quits the server from TeamClass's team and Scoreboard's team.

    TeamClass.java:
    Code:
    package net.iconbyte.test.teams;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    import net.iconbyte.test.teams.TeamClass;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.DyeColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class TeamClass {
        private String name;
        private ChatColor displayColor;
        private Color armorColor;
        private DyeColor woolColor;
        private List<UUID> users = new ArrayList<UUID>();
        private static List<TeamClass> teams = new ArrayList<TeamClass>();
        public static List<Location> spawns = new ArrayList<>();
    
        public TeamClass (String name, ChatColor displayColor, Color armorColor, DyeColor woolColor) {
            this.name = name;
            this.displayColor = displayColor;
            this.armorColor = armorColor;
            this.woolColor = woolColor;
        }
    
        public String getName() {
            return name;
        }
    
        public ChatColor getColor() {
            return displayColor;
        }
    
        public DyeColor getWoolColor() {
            return woolColor;
        }
    
        public Color getArmorColor() {
            return armorColor;
        }
    
        public List<UUID> getPlayers() {
            return users;
        }
    
        public static List<TeamClass> getTeams() {
            return teams;
        }
    
        public static boolean isInTeam(Player p) {
            for (TeamClass t : getTeams()) {
                return t.getPlayers().contains(p.getUniqueId());
            }
            return false;
        }
    
        public static void join(Player p, TeamClass t) {
            if (t == null || isInTeam(p)) {
                return;
            }
            t.getPlayers().add(p.getUniqueId());
            p.setCustomName(t.getColor() + p.getName() + ChatColor.RESET);
            p.setCustomNameVisible(true);
            p.setDisplayName(t.getColor() + p.getName() + ChatColor.RESET);
            p.setPlayerListName(t.getColor() + p.getName() + ChatColor.RESET);
        }
    
        public static void leave(Player p) {
            if (!isInTeam(p)) {
                return;
            }
            getTeam(p).getPlayers().remove(p.getUniqueId());
            p.setCustomName(ChatColor.WHITE + p.getName() + ChatColor.RESET);
            p.setCustomNameVisible(true);
            p.setDisplayName(ChatColor.WHITE + p.getName() + ChatColor.RESET);
            p.setPlayerListName(ChatColor.WHITE + p.getName() + ChatColor.RESET);
        }
    
        public static TeamClass getTeam(String name) {
            for (TeamClass t : getTeams()) {
                if (t.getName().equalsIgnoreCase(name)) {
                    return t;
                }
            }
            return null;
        }
    
        public static TeamClass getTeam(Player p) {
            for (TeamClass t : getTeams()) {
                if (t.getPlayers().contains(p.getUniqueId())) {
                    return t;
                }
            }
            return null;
        }
    
        public static void clearTeams() {
            for (TeamClass t : getTeams()) {
                t.getPlayers().clear();
            }
        }
    }
     
  9. Offline

    teej107

Thread Status:
Not open for further replies.

Share This Page