Double Adding To Teams

Discussion in 'Plugin Development' started by Airbornz, Jun 16, 2015.

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

    Airbornz

    Hello, so im working on a mingame and ive very had this issue before until now. When I add a player to a team it is adding it to both blue and red, no it is not being double called or anything.

    Code:
    public static void joinTeam(Player player, Team team) throws TeamsException{
            if (Instances.isinGame(player)){
                Team currentTeam = getPlayersTeam(player);
                if (currentTeam == null){
                    currentTeam = getTeam("Pending");
                    getTeam("Pending").getMembers().get(Instances.locatePlayer(player)).add(player);
                }
                currentTeam.getMembers().get(Instances.locatePlayer(player)).remove(player);
                if (Instances.locatePlayer(player).getState().equals(GameState.Lobby)){
                    team.getMembers().get(Instances.locatePlayer(player)).add(player);
                    player.sendMessage(AlertManager.getPrefix("TDM")+"§eYou have joined "+team.getColor()+team.getName());
                    for (Player p : Instances.locatePlayer(player).getRawPlayers()){
                        p.sendMessage(AlertManager.getPrefix("TDM")+player.getName()+" has joined team "+team.getName());
                    }
                }
                else{
                    player.sendMessage("§cTeams can only be changed during lobby!");
                }
            }
        }
    
    Its just one of those times where I made a stupid mistake, and i need a fresh set of eyes to look at it, so I just facedesk and wonder how I didnt see that.

    Thanks,
    Air

    @mythbusterma Help maybe?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    JBoss925

    I'm confused. You add the player to the "Pending" team, but then immediately remove them from it? Other than that, I can't find any flaws. My best guess is the "Instances#locatePlayer()" is the problem. Other than that, why not just assign them to the team after checking if they're in the lobby? Why are you checking if they don't have a team yet?
     
  3. Offline

    Airbornz

    Instances wouldn't be the problem that handles the lobbies. I add them to pending anyway when they do join, that join and leave the team is a little bit of error catching.

    The Team members map is this

    Map<Instance, List<Player>> members = new HashMap<Instance, List<Player>>();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  4. Offline

    mythbusterma

    @Airbornz

    First you should make everything not static. Then the way you should be doing this is:

    Have a "TeamsManager" (or something of that nature, it can be a Singleton if you like), then make a class called "Team" that keeps track of all the players on a team using their UUID, not the Player instance (additionally, you can associated more information with that Team, like a name or something).
     
Thread Status:
Not open for further replies.

Share This Page