Assigning players to teams randomly

Discussion in 'Spigot Plugin Development' started by tatemylove, Apr 25, 2017.

  1. Assigning players to random teams, seems to work for 1 player but when I get my alt accounts on aswell, only 1 of them get assigned lol.

    Code (Text):
        public static void assignTeam(String id){
            if ((ArenaFile.getData().getString("Arenas." + id + ".Type").equals("bbd")))
            {
                Main.PlayingPlayers.addAll(Main.WaitingPlayers);
                Main.WaitingPlayers.clear();

                BaseArena.Type = BaseArena.type.BBD;
                for (int assign = 0; assign < Main.PlayingPlayers.size(); assign++)
                {
                    Player p = Main.PlayingPlayers.get(assign);
                    if (Bulldogs.size() < Runners.size())
                    {
                        Bulldogs.add(p);
                    }else if(Runners.size() < Bulldogs.size()){
                        Runners.add(p);
                    }
                    else
                    {
                        Random RandomTeam = new Random();
                        int TeamID = 0;
                        TeamID = RandomTeam.nextInt(2);
                        if (TeamID == 0) {
                            Bulldogs.add(p);
                        } else {
                            Runners.add(p);
                        }
                    }
                    if (Bulldogs.contains(p)) {
                        Team.put(p, "B");
                    } else {
                        Team.put(p, "R");
                    }
                }
            }
        }
     
    Stored everything into ArrayLists
    Code (Text):
        public static ArrayList<Player> Runners = new ArrayList<>();
        public static ArrayList<Player> Bulldogs = new ArrayList<>();
        public static HashMap<Player, String> Team = new HashMap<>();
        public static ArrayList<Player> Players = Main.PlayingPlayers;
        public static ArrayList<Player> ComPlayers = new ArrayList<>();
     
  2. Where do you populate the Main.WaitingPlayers? Are you sure it has more than one entry in it?

    Why not use
    for(Player p:Main.PlayingPlayers)

    Since you don't really need to get them out in reverse order or something odd you could change this to a Set<Player> and do it the same way. The for I loop is just a waste.


    Sent from my iPhone using Tapatalk
     
  3. The Main.PlayingPlayers and Main.WaitingPlayers is another arraylist in my main class. I added the main.Waiting to main.playing before the for loop. And the reason for a for loop is to assign each player to a team
     
  4. I understand that, but you don't show how players are added to the waiting list, add a system.out or bukkit.broadcast before your loop that tells you what the size of the array list is so you know for sure that more than one person is added. If it's only adding one then that's why the other player never gets assigned to a team


    Sent from my iPhone using Tapatalk
     
  5. They are added through the join command, I didn't include the code, unless you would like to see it.
     
  6. Either that or you'll need to physically print out the size of the waiting list or joined list before the loop runs to verify that it actually contains the right amount of players.


    Sent from my iPhone using Tapatalk
     
  7. I'd recommend putting some debugs in your code to see what is and isn't being called when you call the method.

    Some other points:

    - Please follow Java naming conventions. Variable names should be in lowerCamelCase
    eg. WaitingPlayers should be waitingPlayers

    - You should use List<T> rather than ArrayList<T>
    This explains it well: http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java

    - You misuse static quite a bit here. If those lists are all in the same class as the method, there is no need for them to be static.
    If they are not in the same class, try passing an instance of the class in the method, or creating a singleton.
     
  8. Ok I believe I found a solution. In my code I specified that if so and so is less than so and so, add player to that team and vice versa, but after 2 players are assigned, then the teams are equal, so I'm going to add an if statement to see if it gets resolved.
     
  9. I had to use static, they are global variables. And yes I know that's frowned upon but it organizes it better for me
     
  10. You can make them private to the class and create getters/setters for them and not get yelled at by the static cops :p


    Sent from my iPhone using Tapatalk
     
  11. I did use getters and setters for the lobbytimer part of the plugin but I find using static more convenient