Solved Get less amount of players in a team

Discussion in 'Plugin Development' started by dominikremes, Apr 19, 2017.

Thread Status:
Not open for further replies.
  1. Hello. I`m working on a minigame and I`ve got no idea how to get the team with the less amount of players. There must not be all the teams avible for that arena so I`ve got witch teams ae in game and the amount of players in teams. Here is the code:
    Code:
    boolean blue = false;
                boolean red = false;
                boolean green = false;
                boolean yellow = false;
                boolean gray = false;
                boolean orange = false;
                boolean cyan = false;
                boolean purple = false;
               
    From here it starts to get witch teams are in game
                if (setupManager.getBlueTeam(arena)) {
                    blue = true;
                }
                if (setupManager.getRedTeam(arena)) {
                    red = true;
                }
                if (setupManager.getGreenTeam(arena)) {
                    green = true;
                }
                if (setupManager.getYellowTeam(arena)) {
                    yellow = true;
                }
                if (setupManager.getOrangeTeam(arena)) {
                    orange = true;
                }
                if (setupManager.getCyanTeam(arena)) {
                    cyan = true;
                }
                if (setupManager.getGrayTeam(arena)) {
                    gray = true;
                }
                if (setupManager.getPurpleTeam(arena)) {
                    purple = true;
                }
               
                int blueInt = -1;
                int redInt = -1;
                int greenInt = -1;
                int yellowInt = -1;
                int grayInt = -1;
                int orangeInt = -1;
                int cyanInt = -1;
                int purpleInt = -1;
               
    If the teams are in game it set them to the amount of players witch are in them.
                if (blue) {
                    blueInt = manager.getBlue();
                }
                if (red) {
                    blueInt = manager.getRed();
                }
                if (green) {
                    blueInt = manager.getGreen();
                }
                if (yellow) {
                    blueInt = manager.getYellow();
                }
                if (orange) {
                    blueInt = manager.getOrange();
                }
                if (cyan) {
                    blueInt = manager.getCyan();
                }
                if (gray) {
                    blueInt = manager.getGray();
                }
                if (purple) {
                    blueInt = manager.getPurple();
                }
    
    From here I don`t know. The teams witch aren`t in the game are -1. At least there must be 2 teams, but there CAN be amount of players in a team set to 0!
                
    Thanks for help
     
  2. Offline

    timtower Administrator Administrator Moderator

    @dominikremes Why not use Lists to manage your teams instead of variables for each team?
     
    MCMastery likes this.
  3. Offline

    mine2012craft

    @dominikremes I would recommend that when you add a player to a team, add them to a list using their UUID. Then if you need to find which team has the least amount of players, get the size of the list, and compare it to the other teams.

    Also, the list will also most likely come in handy with a bunch of other team-related mechanics (ex. Friendly Fire, Team Spawn/Lobby points, Colored name tags, etc) so it is highly recommended to do so
     
  4. @timtower @mine2012craft
    I use lists. Each team has an own list
    With this part I get the size of those lists. But how can I compare them easy and fast to not compare all of them with all of them?
     
  5. Offline

    DoggyCode™

    Take a more object-orientated approach such as so:
    Code:java
    1. public class Team {
    2.  
    3. private String id;
    4. private Set<UUID> members;
    5.  
    6. public Team(String name) {
    7. this.id = name;
    8. this.members = new HashSet<UUID>();
    9. }
    10.  
    11. public String getId() {
    12. return this.id;
    13. }
    14.  
    15. public Set<UUID> getMembers() {
    16. return this.members;
    17. }
    18. }


    Now you can easily keep track of for example a Map where you get get Team-s by their id. For example:
    Code:java
    1. new HashMap<String, Team> teams


    Call Map#get(), then use the team's id. This will return you your Team object where you can easily manage the members using Team#getMembers() and remove/add UUIDs (members) by calling Set#remove() or Set#add()


    Hope this helped!


    Marius
     
Thread Status:
Not open for further replies.

Share This Page