Solved Randomize 4 Teams

Discussion in 'Plugin Development' started by CyberSoul, Nov 24, 2015.

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

    CyberSoul

    I need to randomize 4 teams. I want all players on the server who aren't in spectator mode placed on to 1 of 4 teams. I need the teams to be balanced. I have looked everywhere and I can't find a good way to do this. Thanks in advance for any assistance you may be to me.
     
  2. Offline

    teej107

  3. Offline

    CyberSoul

    I have tried many things... nothing really works the way I want it to.
     
  4. Offline

    Scimiguy

    Collect all your online players and put them in an array if they aren't in spectator mode

    Run a loop for the size of that array, and in each loop, grab a player from that array using a Random#nextInt(Array#size())
     
  5. Offline

    CyberSoul

    Is there any way you can work with what I have already...
    At the beginning of the plugin:
    Code:
    private HashMap<Player, String> team = new HashMap<Player, String>();
    And then where I want to do the randomizing:
    Code:
    for (Player players : Bukkit.getOnlinePlayers()) {
                          
                        if (players.getGameMode() != GameMode.SPECTATOR) {
                      
                            if (team.get(players) == null) {
                              
                            }
                          
                        }
                    }
     
  6. Offline

    teej107

    A Collection would be better to use than an Array (unless you mean an ArrayList because of your incorrect method name Array#size()). Just make sure to remove the player from the Collection (to avoid randomly getting a player twice) and without causing a ConcurrentModificationException
     
  7. Offline

    Scimiguy

    Something like that, having some personal issues at the moment, sorry
     
  8. Offline

    CyberSoul

  9. Offline

    Scimiguy

    Bump?

    did you try the suggestions?
     
  10. Offline

    CyberSoul

    I asked for a way to do it based on what I have already
     
  11. Offline

    Scimiguy

    You shouldn't be using your code anyway, don't store Players unless it's for extremely short term
     
  12. Offline

    Lordloss

    they gave you good suggestions based on what you have done allready... come on, its not that hard. tell us whats the problem instead of "bump"
     
    PeterXonwiiXx and dlange like this.
  13. Offline

    CyberSoul

    I said "bump" because I was almost 100% certain that their suggestions had been before they read my explanation that I wanted it to work around what I had. As I have been doing other things for a little while, I have not read this thread in a little while, I am going to work on it however, and I will try the suggestions... You don't have to be rude... Oh... and just because: it's spelled "already" not "allready"

    Anyway... now, can you maybe explain collections a little more? It would be helpful
     
    Last edited: Dec 3, 2015
  14. Offline

    Scimiguy

    Haha that's the most self contradictory thing I've seen in a while, congrats.
     
  15. @CyberSoul Collections are part of the java-basics. if you dont know about them you should go back and learn the basics of java instread of programming a plugin here #noOffense

    As soon as you know the basics follow those steps:
    1. get all players in an array
    2. build a Map<String, Set<UUID>> which is gonna contain as key the teamname and as the value a set of all uuids of the players who are in the team
    3. loop through all players
    4. generate for every player a random int-value
    5. switch the int-value and decide dependend on the int-value in which team the player is gonna be moved

    Just saying, people won't spoonfeed you here. They won't just take your code, complete it and send it back.
     
  16. Offline

    CyberSoul

    I didn't ask for you to complete my code... I asked for you to explain something. I never learned Java... I am learning Java by coding plugins... that happens to be my method of learning it. I have found that coding Bukkit plugins is much easier that most other forms of Java. My plan is to use plugin coding to become better at Java basics in order to move on to more difficult things. It just so happens that I don't know how to do this particular thing and you are getting up in my face... this is not supporting me, and not being helpful in any way... so please to don't reply if it isn't going to be helpful...
    Oh and one more thing... people who say "No Offense" are terrible people... when someone says that they are obviously being offensive and know it. Saying "No Offense" doesn't make it any less offensive.
     
  17. Offline

    mcdorli

    A lot of people know that wrong. No, you can't learn java trough bukkit, when someone creates a tutorial, they don't teach you the basic things. Go, learn java, because you doesn't make learning java easier by doing it trough bukkit, but harder. And No offense means, that the guy read trough his text, and found it kind of offensive, when it isn't, so he puts it there, for you to know, he isn't angry or anything-

    Btw.: your problem can be solved by just creating an array, then swapping around values in it, then get the first n / 4 (n is the length of the array), and put it in the first group, then the next n/4 in the second group, etc.
     
  18. Offline

    CyberSoul

    I'm very close now... I would just really like someone to help me now... I have read through everything @Shmobi said and I went to learn a little about Java collections and Maps and stuff, now I need someone to explain better what I need to do after getting all the players into an array.
     
  19. Offline

    mcdorli

    I said you above
     
  20. Offline

    CyberSoul

    I don't really understand what you mean.
     
  21. Offline

    mythbusterma

    @CyberSoul

    So you want it to fit in to "what [you] already have," but what you have is wrong. You're going to need to rewrite what you have based on what we've told you.

    Then, don't bother putting the Players into an array, simply create a List of them (or their UUIDs, doesn't matter for the short time during team assignment), and use Collections.shuffle(...) on your List. Then, iterate over the resultant List, assigning players to teams one after the other, i.e.:

    0th player: team 1
    1st player: team 2
    2nd player: team 3
    3rd player: team 4
    4th player: team 1
    5th player: team 2
    .....

    and so on. A simple way to do this would be to assign them to the team represented by the modulus of the index in the List.
     
  22. Offline

    CyberSoul

    @mythbusterma
    I'm confused... List<Player> sort = new ArrayList<Player>();
    then I put all of the players on the server who aren't in spectator mode on that list. Then I do Collctions.shuffle(sort);
    From there I am not sure how to assign each player on the list to a different team. I know how to assign them all to the same team... but that's obviously not what I want.
     
  23. Offline

    mythbusterma

    @CyberSoul

    What wasn't clear, I told you how in my last post.
     
  24. Offline

    CyberSoul

    @mythbusterma
    Code:
    List<Player> sort = new ArrayList<Player>();
                  
                    for (Player players : Bukkit.getOnlinePlayers()) {
                      
                        if (players.getGameMode() != GameMode.SPECTATOR) {
                            sort.add(players);
                        }
                      
                    }
                  
                    Collections.shuffle(sort);
                  
                    for (int i = 0; i < sort.size(); i++) {
                      
                        Player nplayer = sort.get(i);
                      
                        //Need Help Here
                      
                    }
    Could you help me with what goes in the for loop under Collections.shuffle(sort);?
     
  25. Offline

    Lordloss

    Not really proper way, but you could do it like this:
    Code:
    while(!sort.isEmpty()) {
    
    //add player at index 0 to team 1
    //remove player at index 0
    
    //check again isEmpty()
    //add player at index 0 to team 2
    //remove player at index 0
    
    //check again isEmpty()
    //add player at index 0 to team 3
    //remove player at index 0
    ...
    }
     
  26. Offline

    mythbusterma

    @Lordloss

    What a ridiculously convoluted way to do it.


    @CyberSoul

    Like this:

    Code:
    for (int i =0; i <sort.size(); i++) {
        Player ply = sort.getPlayer(i);
        switch (i % 4) {
            case 0:
                team1.add(ply);
                break;
            case 1:
                team2. add(ply);
                break;
            case 2:
                team3.add(ply);
                break;
             case 3:
                 team4.add(ply);
                 break;
            }
    }
     
    CyberSoul and Lordloss like this.
  27. Offline

    Lordloss

    @mythbusterma well okay, your solution is better. At least i learned a neat use for modulus :p
     
    mythbusterma likes this.
  28. Offline

    mythbusterma

    @Lordloss

    You would be very surprised how useful it is, especially in performance programming.
     
  29. Offline

    CyberSoul

    Thank you... It worked :)
     
  30. @CyberSoul i know i am replying rlly late, sorry for that. i just wanted to say again. What i typed there was no offense. It was an advice. Im not judging you for not being a highly practiced javaprogrammer. i just said since you don't know collections you should take a look at them since they are basic java-stuff and you will almost always have to use them. Thats why i said no offense. Lots of ppl feel attacked when you tell them that they should learn the basics because they think you are trying to make fun of them or whatever. besides that my post was sensefull and helpfull since it was a good solution (maybe not the best tho, but easy) even step by step which were deffenetly doable if you know how maps and collections work. And well you asked ppl to complete your code, referring to the 10th post in this thread. just wanted to have it said that there usually won't somebody come up with a solution and tell you that easily. usually ppl give you hints in which direction to go or and explanation how to do it.
     
Thread Status:
Not open for further replies.

Share This Page