Solved Random Teams

Discussion in 'Plugin Development' started by Bladibudies, Jan 19, 2016.

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

    Bladibudies

    Hi, I want to do that when a player clicks a sign,teleportation him to an arena, give him a compass and that the player join a random team... But if the player has a permission like "example.chooseteam" he can choose the team with the compass.

    This is what I have:
    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent e){
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK))return;
            if (e.getClickedBlock().getState() instanceof Sign){
                Sign letrerodeinteract = (Sign) e.getClickedBlock().getState();
                if (letrerodeinteract.getLine(0).equalsIgnoreCase("§b§l[CTW DUO]")){
                    letrerodeinteract.setLine(3, "§8" + numero1 + " §8Jugadores");
                    letrerodeinteract.update();
                    int x1 = instance.getConfig().getInt("Arena01.X1"), y1 = instance.getConfig().getInt("Arena01.Y1"), z1 = instance.getConfig().getInt("Arena01.Z1");
                    e.getPlayer().teleport((Entity) new Location(e.getPlayer().getWorld(), x1, y1, z1));
                    e.getPlayer().setGameMode(GameMode.SURVIVAL);
                    e.getPlayer().setFlying(true);
                    ItemStack ct = new ItemStack (Material.BOOK);
                    ItemMeta ctm = ct.getItemMeta();
                    ctm.setDisplayName("§aSelector de equipo");
                    ct.setItemMeta(ctm);
                    e.getPlayer().getInventory().addItem(ct);
                    //PUT THE PLAYER IN A RANDOM TEAM
        }}}
    Thank you!
     
  2. Offline

    Lightspeed

    @Bladibudies
    Set the teams to numbers then randomly generate a number from smallest team id to highest?
    Basically put all the teams in a array list
    Get that array list lenght
    Gen a rand number from 1 to the list lenght and
    Team.get(randNumb);
    Shall give you the team that it randomly picked
    Example:
    List<ArrayList<Players>> teams;
    teams.add(TeamBlue)
    teams.add(TeamRed)
    >Example end

    Somthin like that eh?
     
  3. Offline

    Zombie_Striker

    @Lightspeed
    That's a very strange way of doing that. Instead, @Bladibudies should do the following:
    • Create an instance of random.
    • boolean team1 = random.nextBoolean()
    • If team1 is true
    • join team1
    • Else
    • join team2
     
  4. Offline

    Bladibudies

  5. Offline

    Zombie_Striker

  6. Offline

    teej107

    That is what the OP wanted but I'm just going to say that there is a chance that the team sizes will be way un-even or not as even as they could be.
     
  7. Offline

    Bladibudies

    @Zombie_Striker Ok, I have the instance of random and the boolean redteam = random.nextBoolean()

    Code:
        Random random = new Random();
        public boolean redteam = random.nextBoolean();
    Now, how can I do that of if redteam is true...
     
  8. Offline

    teej107

  9. Offline

    Bladibudies

    @teej107 @Zombie_Striker Like this?
    Code:
    Random random = new Random();
        public boolean redteam = random.nextBoolean();
        boolean redteam(boolean b){
            if (redteam(true)){
                //join redteam
            }else{
                //join blueteam
            }
            return b;
        }
     
  10. Offline

    Zombie_Striker

    @Bladibudies
    This is a clear sign you really have no knowledge about Java. You MUST learn Java before moving onto Bukkit. Please, look at the link, find a tutorial, and learn what you're actually doing. Everything will make sense if you learn Java before working on Bukkit.

    Link again:
     
  11. Offline

    Bladibudies

  12. Offline

    Lightspeed

    We can't teach you java here
    Try learning enough here> https://www.youtube.com/playlist?list=PLE7E8B7F4856C9B19
    Then try search on google other new things added to java.
    This playlist should get you moving enough to program and learn new things(Updated things) fast.
    ^Note^: Don't give me crap when derek banas is suggested right in the bukkit wiki.
    http://wiki.bukkit.org/Plugin_Tutorial#Learning_Java
     
  13. Offline

    Zombie_Striker

    @Lightspeed
    Although his channel is a good place to learn, you should learn through reading documentation. It contains all the information about each method/class and might be easier to translate to another language than a video, since I assume English is not your native language, @Bladibudies .
     
  14. Offline

    Rexcantor64

    You SHOULD learn java, but if I say that I learned bukkit BEFORE learned java, most of you will say this is not the correct way. I never used forums, or etc. only youtube videos. Now, I have a server with my plugins(skywars, capturetheflag, etc).

    The most correct way to do what you want it's to have a central/neutral team.
    You need to create an enum with all teams. When a player joins the game he will join on the neutral team. After that, you need to have a method to equilibrate the teams. My method only works for 2 teams, but if you need more, I can help you!

    First, you need to create an enum:

    Code:
    package yourPackageNameHere;
    
    public enum Team{
        RED, BLUE, NEUTRAL;
    
        private Team() {
        }
    }
    
    After that, create a hashmap on your game class (if you have it):

    Code:
    private Map<Player, Team> playerTeamMap = Maps.newHashMap();
    Finally, create 2 methods:

    Code:
    private void equilibrateTeams() {
            ArrayList<Player> blue = new ArrayList<Player>();
            ArrayList<Player> red = new ArrayList<Player>();
            ArrayList<Player> neutral = new ArrayList<Player>();
            for (Player gp : this.playerTeamMap.keySet()) {
                if (this.playerTeamMap.get(gp) == Team.BLUE) {
                    blue.add(gp);
                } else if (this.playerTeamMap.get(gp) == Team.RED) {
                    red.add(gp);
                } else {
                    neutral.add(gp);
                }
            }
    
            if (blue.size() == red.size() && neutral.size() < 1)
                return;
    
            if ((this.playerTeamMap.size() % 2) != 0 && neutral.size() < 1) {
                if (blue.size() == red.size() + 1)
                    return;
                if (blue.size() + 1 == red.size())
                    return;
            }
    
            if (neutral.size() > 0) {
                Random r = new Random();
                while (neutral.size() > 0) {
                    int ri = r.nextInt(neutral.size());
                    if (blue.size() > red.size()) {
                        Player gp = neutral.get(ri);
                        neutral.remove(ri);
                        red.add(gp);
                    } else if (red.size() > blue.size()) {
                        Player gp = neutral.get(ri);
                        neutral.remove(ri);
                        blue.add(gp);
                    } else {
                        Player gp = neutral.get(ri);
                        neutral.remove(ri);
                        int i = r.nextInt(10);
                        if (i <= 5)
                            red.add(gp);
                        else
                            blue.add(gp);
                    }
                }
            }
            if (blue.size() > red.size()) {
                Random r = new Random();
                while (blue.size() > red.size()) {
                    int ri = r.nextInt(blue.size());
                    Player gp = blue.get(ri);
                    blue.remove(ri);
                    red.add(gp);
                }
            } else if (red.size() > blue.size()) {
                Random r = new Random();
                while (red.size() > blue.size()) {
                    int ri = r.nextInt(red.size());
                    Player gp = red.get(ri);
                    red.remove(ri);
                    blue.add(gp);
                }
            }
    
            transferToGame(blue, red);
    
        }
    
        public void transferToGame(List<GamePlayer> blue, List<GamePlayer> red) {
            this.playerTeamMap.clear();
            for (Player p1 : blue) {
                this.playerTeamMap.put(p1, Team.BLUE);
            }
            for (Player p2 : red) {
                this.playerTeamMap.put(p2, Team.RED);
            }
    
        }
    If there is any error, please let my know. I adapted it from my player and I have a custom player class(called "GamePlayer").

    Edit: Sorry for spoonfeeding, but he don't know nothing (sorry for the offence...). I try to don't do that, but he needs help and I'm here to help. If it's you, you will appreciate if I've posted it.

    I hope I've helped,
    Rexcantor64
     
    Last edited: Jan 21, 2016
  15. Offline

    mythbusterma

    @Rexcantor64

    Why wouldn't you have a "lobby" of sorts that contains everyone, then shuffle the players in the lobby, then assign them round-robin to teams? It's quite simple and scales to any number of teams.

    This is why we don't learn from YouTube videos.
     
    Zombie_Striker and Konato_K like this.
  16. Offline

    mcdorli

    Indeed, youtube videos are bad, but if I want to, I could find bad text tutorials too. Books are a bit more secure though, because publishing them costs money.
     
  17. Offline

    Rexcantor64

    I did NOT take that system from youtube, I invented it myself. And yes, you can have a lobby, but if you want some players to chose their team, it will not work. Other thing is, there is 4 players on a match, and 3 choose team red and 1 choose team blue, my system picks a random player from red team and put on blue team. It works very well and you can also adapt it to more teams.

    P.S.: I STARTED learning bukkit on youtube, but I worked "alone" (without youtube and other forums) in the last year.
     
  18. Offline

    ShowbizLocket61

    @Rexcantor64
    Does that not defeat the purpose of being able to choose your own team? I don't know how I would go about it, but just saying, that might tick off some people.
     
  19. Offline

    Rexcantor64

    If you do not equilibrate the teams, some people will not enjoy the game.
     
    Last edited: Jan 21, 2016
  20. Offline

    ShowbizLocket61

    What I've seen that is quite successful before, not in minecraft though, is that you are not allowed to join a team that has, say, 2 or 3 players more than the other. This ensures a balance, while giving them some free room. Then, when the peasants come rolling along, they can be placed to fill the gap.
     
  21. Offline

    mcdorli

    Logic.
    When you divide people in the teams, instead of round robin, just check the amoun t of players in the teams, and always put the next one in the team with the lowest amount of people.
     
  22. Offline

    ShowbizLocket61

    @mcdorli
    The problem is that there are people who can choose teams. There might be 5 vs 2, because a bunch of friends wanted to be together and had paid handsomely.
     
  23. Offline

    Mrs. bwfctower

    Please don't. That's called spoonfeeding and we don't appreciate it.
     
  24. Offline

    mcdorli

    You really need to spoonfeed? Please don't, if possible, edit it out.

    Remove them from the lobby maybe? Like seriously, use your heads.
     
  25. Offline

    Zombie_Striker

    Please do not spoonfeed on the bukkit forums. Here is al ist of all the reasons why you don't spoonfeed:
    • Spoonfeeding does not guarantee the code will even work (can be made to not work)
    • Spoonfeeding does not guarantee the code is written the proper way (can have bad practices)
    • Spoonfeeding does not guarantee the person receiving the code will understand how it works.
    • Spoonfeeding does not guarantee the person receiving the code will know how to use it.
    • Spoonfeeding does not guarantee the person receiving the code will know how to modify it to fit whatever they need.
    • Spoonfeeding does not guarantee the person receiving the code is even capable of programming! (spoonfeeding only requires the person to know how to use ctrl+c and ctrl+v)
    So please, if you actually want to help someone, tell them to officially learn Java first, or at the very least, tell the what to do.

    Are you really discouraging learning the language you're writing in first before working on projects? What you are currently saying is similar to "Hey, I learned to swim in the middle of the Atlantic, you should too!". I have been on this forum helping people out for years now, I know that without proper Java knowledge, we really have no way to help other people without writing the code for them. Please, try to tell people to actually learn Java before attempting work on projects like plugins, instead of saying they can swim in the "deep end of the pool".
     
    Mrs. bwfctower and mythbusterma like this.
  26. Offline

    mythbusterma

    @Rexcantor64

    I meant that poor code is why we don't learn from YouTube.

    Watch this.

    Code:
    public class Team {
     
         private List<UUID> players = new ArrayList<>();
    
         public void addPlayer(UUID id) {
             this.players.add(id);
         }
    }
    
    List<Team> teams = new ArrayList<>();
    
    // add a few teams here:
    teams.add(...);
    // etc.
    
    ArrayList<UUID> lobbyPlayers = ...;
    
    int numTeams = teams.size();
    for (int i = 0; i < lobbyPlayers.size(); i++) {
         teams.get(i % numTeams).addPlayer(lobbyPlayers.get(i));
    }
    lobbyPlayers.clear();
    
    Why does everything have to be done the hard way? This is so easy...

    Round robin ensures teams are no more than 1 player different. So, I really don't see what your point is?
     
    Zombie_Striker likes this.
  27. Offline

    mcdorli

    Read the post above that, That is good, if for example, there are some players already in the teams.
     
  28. Offline

    mythbusterma

    @mcdorli

    I suppose, but it would make sense to put all the players back in the lobby between matches, if you asked me. If not, just implement a simple method like this:

    Code:
    List<Team> teams = ....;
    
    public void addToLeast(UUID id) {
        Team lowest = null;
        int min = Integer.MAX_VALUE;
        for (Team team : teams) {
            if (team.size() < min) {
                lowest = team;
                min = team.size();
            }
         }
         team.addPlayer(id);
    }
     
  29. Offline

    mcdorli

    Again, read the post above. He only want to do the shuffle for players, who didn't join a specific team.
     
  30. Offline

    mythbusterma

    @mcdorli

    Well that's what my code does. It adds them to the smallest team.
     
Thread Status:
Not open for further replies.

Share This Page