Solved Putting players into "Random" Teams

Discussion in 'Plugin Development' started by reider45, Oct 28, 2013.

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

    reider45

    Hello there bukkit, so i've been trying to code a plugin where basically, there are 2 teams, robbers and cops, there are 2 players randomly selected to be on the robber's team, the rest are on the cops team. I've tried tons of various methods for this, but none seem to be working :|

    Code:
        public void start(){
            robber1 = null;
            robber2 = null;
            Player[] players = Bukkit.getServer().getOnlinePlayers();
            Random random = new Random();
           
            if(plugin.robbers.size() == 0){
                int robber1index = random.nextInt(players.length);
                robber1 = players[robber1index];
                plugin.robbers.add(robber1);
                Bukkit.broadcastMessage("Robber 1 was added " + robber1.getDisplayName());
            }
            if(plugin.robbers.size() == 1){
                int robber2index = random.nextInt(players.length);
                robber2 = players[robber2index];
                plugin.robbers.add(robber2);
                Bukkit.broadcastMessage("Robber 2 was added " + robber2.getDisplayName());
            }
            if(plugin.robbers.size() >= 2){
                int copindex = random.nextInt(players.length);
                cop[copindex] = players[copindex];
                plugin.cops.add(cop);
                Bukkit.broadcastMessage("Cops: " + cop.getDisplayName());
            }
           
            while(robber1.getDisplayName() == robber2.getDisplayName()){
                plugin.robbers.clear();
                plugin.robbers.add(robber1);
                int redo = random.nextInt(players.length);
                robber2 = players[redo];
                Bukkit.broadcastMessage(robber2 + " is the new robber");
            }
    this is my most recent attempt, can anyone help? Thanks :) Btw i have looked at other threads but the ones I've found do not work, thanks :)
     
  2. Offline

    remremrem

    It will be helpful if you can say exactly what it is you expect to happen, and what is happening instead, and if there are any errors.

    One thing I see is this

    while(robber1.getDisplayName() == robber2.getDisplayName())

    When is the display name being changed? Is there more code that reassigns their display names? Right now I see potential for an infinite while loop.

    Secondly this

    int copindex = random.nextInt(players.length);
    cop[copindex] = players[copindex];

    this is going to randomly select players to become cops, in fact it will even select already selected robbers to become cops.

    Instead, when the robbers are selected they should be removed from the array of players and the remaining players will be the cops.
     
  3. Offline

    reider45

    remremrem First, thanks for replying :)

    Second, what i expect to happen is when start() happens, the server will pick 2 random people, and make them robber1 and robber2
    Then they players who are not robber1 and robber2 are made "cops"

    Thirdly, the while(robber1.getDisplayName() == robber2.getDisplayName())
    is meant to be if robber1 is the same player as robber2, robber2 is repicked, but i assume i messed up there '-'

    Fourthly, How would I go about "when the robbers are selected they should be removed from the array of players and the remaining players will be the cops." Thanks :)

    Ok, so changing some things around, would this be any better?
    (Any other devs feel free to jump in)

    Code:
        public void start(){
            Player[] players = Bukkit.getServer().getOnlinePlayers();
            Random rand = new Random();
            int robber1index = rand.nextInt(players.length);
            int robber2index = rand.nextInt(players.length);
           
            while(robber1index == robber2index)
                robber2index = rand.nextInt(players.length);
            robber1 = players[robber1index];
            robber2 = players[robber2index];
           
            cops = new Player[Bukkit.getServer().getOnlinePlayers().length - 2];
           
            for(int i = 0; i < players.length; i++)
            if(robber1 != players[i] || robber2 != players[i])
                cops[i] = players[i];
           
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    reider45

    ~ Le Bump ~
     
  5. Offline

    qhenckel

    try just using .shuffle and take the first two.
    Code:
    public void start(){
      Player[] players = Bukkit.getServer().getOnlineplayers();
      Collections.shuffle(Arrays.asList(players));
      Player robber1 = players[0];
      Player robber2 = players[1];
    }
     
    Maulss, 1Rogue and reider45 like this.
  6. Offline

    reider45

    qhenckel Works great, thanks so much :) == Would you happen to know how to add the rest of the players to the cops? I've tried

    Code:
            Iterator<Player> iterator = plugin.onlineplayers.iterator();
           
            while(iterator.hasNext()){
                Player onlinePlayer = iterator.next();
                if(!plugin.robbers.contains(onlinePlayer.getName())){
                    plugin.cops.add(onlinePlayer.getName());
                    Bukkit.broadcastMessage(plugin.cops.size() + " cops");
                }
            }
    and this is the onlineplayers list in my main class
    Code:
        public List<Player> onlineplayers = new ArrayList<Player>();
    Except it just doesnt work, it only says 0 cops, and no errors in console :| Thanks again :)
     
  7. Offline

    qhenckel

    reider45
    try something like this. Iterators are kinda fussy.
    Code:
    for(Player player : players){
      if(player.getName() instanceof robbers){continue;}
      cops.add(player.name());
    }
     
  8. Offline

    reider45

    qhenckel What do you mean exactly when you say

    Code:
    if(player.getName() instanceof robbers)
    Couldn't you do something like

    Code:
            for(Player player : players){
                  if(!plugin.robbers.contains(player)){continue;}
                  plugin.cops.add(player.getName());
                }
    Would this work the same way?
     
  9. Offline

    qhenckel

    yes that would work the same 'instanceof' is like '&&', '==' or '||' it's a condition.
    so it is a fast way of doing 'plugin.robbers.contains(player)' ;)
     
  10. Offline

    reider45

    qhenckel Ah i see, learning one step at a time ;) But if my lists are created like
    Code:
    List<String> robbers = new ArrayList<String>;
    List<String> cops = new ArrayList<String>;
    // Or something like that.. i can't remember exactly..
    And with your code i can't seem to access plugin.robbers as a type? I'm assuming because it's using instanceof?
     
  11. Offline

    qhenckel

    Right it just depends on how your variables are inherited. I'm not sure why you need to use the 'plugin.'
    Code:
    plugin.robbers.contains(player)
    if that works there isn't anything wrong about it. so use that.
    EDIT: correction 'instanceof' is for checking if it is a type not if it is in an array or set. My bad.
     
  12. Offline

    reider45

    qhenckel Alright thanks :) also the reason for 'plugin' was because it was the reference to my main class ;)
     
Thread Status:
Not open for further replies.

Share This Page