Solved Get 25% of all online players

Discussion in 'Plugin Development' started by CoderMusgrove, Nov 17, 2013.

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

    CoderMusgrove

    What I am trying to do is calculate 25% of all of the online players, or players in a list, and store those players into an arraylist. I have had countless fails, and now it is time to ask..

    How would one get 25% of the players on the server, or rather in a list?
     
  2. Offline

    1Rogue

    The question isn't how but which 25% would you want to get?
     
  3. Offline

    L33m4n123

    To get the # how many those 25% are you prolly can do

    Bukkit.getServer().getOnlinePlayers().length() / 4; this should give you the ammount of 25% of players as an integer.. Maybe you can work it from there

    Edit: Oh and what 1Rogue said ofc.
     
  4. Offline

    clienthax

    Code:java
    1. Player[] onlinePlayers = getServer().getOnlinePlayers();
    2. int quarterOfPlayersAmount = onlinePlayers.length / 4;
    3.  
    4. ArrayList<String> playerNames = new ArrayList<String>();
    5. for(Player player : onlinePlayers)
    6. playerNames.add(player.getName());
    7.  
    8. Collections.shuffle(playerNames);
    9.  
    10. ArrayList<String> shortenedPlayerList = new ArrayList<String>();
    11. for(int i = 0; i < quarterOfPlayersAmount ; i++)
    12. shortenedPlayerList.add(playerNames.get(i));
    13.  
     
  5. Offline

    RealDope

    Assuming you want a random quarter of the players..
    Code:JAVA
    1.  
    2. int chosen = 0;
    3. int required = Bukkit.getServer().getOnlinePlayers().length / 4;
    4. List<String> players = new List<String>();
    5. Random r = new Random();
    6. while(chosen < required) {
    7. int pick = r.nextInt(Bukkit.getServer().getOnlinePlayers().length);
    8. String s = Bukkit.getServer().getOnlinePlayers()[pick].getName();
    9. while(players.contains(s) == true) {
    10. pick = r.nextInt(Bukkit.getServer().getOnlinePlayers().length);
    11. s = Bukkit.getServer().getOnlinePlayers()[pick].getName();
    12. }
    13. chosen++;
    14. players.add(s);
    15. }
    16.  
     
  6. Offline

    1Rogue


    That would cause repeated values, if you want random, it would be much simpler to:

    Code:java
    1. public List<String> getQuarter() {
    2. ArrayList<Player> players = Arrays.asList(Bukkit.getServer().getOnlinePlayers());
    3. List<String> back = new ArrayList();
    4. Collections.shuffle(players);
    5. for (int i = 0; i < players.size() / 4; i++) {
    6. back.add(players.get(i).getName());
    7. }
    8. return back;
    9. }
     
  7. Offline

    RealDope

    How would it cause repeated values?
    1Rogue
     
  8. Offline

    Polaris29

    This is probably the simplest way in regards to the shortest amount of code

    Code:java
    1.  
    2. ArrayList<Player> list = new ArrayList<Player>(Bukkit.getServer().getOnlinePlayers());
    3.  
    4. Collections.shuffle(list); // Omit this if you don't want randomization
    5.  
    6. ArrayList<Player> quarterOfPlayers = new ArrayList<Player>(list.subList(0, (int) (list.size() / 4)));
    7.  
     
    1Rogue likes this.
  9. Offline

    1Rogue

    Ah, my mistake, I didn't quite read through the entirety of the code. However, that code is still very resource-intensive (and considering it is random, it has the potential to take an unlimited amount of time).


    Yup, subList was that method I couldn't find earlier.
     
  10. Offline

    CoderMusgrove

    I'm not highly sure if these work, because I am testing with only two players, while there are less than 4, I need to be able to get one of the 1/1, 1/2, or 1/3 players that are currently online. Is there a method or conversion from the current ones given that will enable this feature, because as far as I know it doesn't work with < 4 players.
     
  11. Offline

    1Rogue


    So then add a check if there are less than four players, and then if there are just select a random one.
     
    CoderMusgrove likes this.
  12. Offline

    CoderMusgrove

    This topic is halfway resolved, if I need more help when I can get 4+ of my friends to test this and if it doesn't work I will reply.
     
Thread Status:
Not open for further replies.

Share This Page