Solved Getting random number of players online

Discussion in 'Plugin Development' started by YogurtSmudge, Sep 7, 2018.

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


    So I currently have this code:
    public static void getRandomPlayers(String map) {
       int size = getPlayersAlive(map).size();
       int newsize = size / 2;
       if (newsize == Math.floor(newsize)) {
            for (int i = 1; i < newsize; i++) {
                  Player random = getRandomPlayer(map);
                  Manager.sendMessageToMap(map, "&6" + random.getName() + " was picked!");
    Basically, I am trying to get the current amount of players online, divide by 50% or 25% and getting those players and broadcast their names. So for example: There are 6 players online it would pick 2 or 3 players and broadcast their names. If there were 2 players, I would broadcast one name.

    Also is it possible to divide a number like 5 divide that by 2 and return 3.5 and round it?

    Last edited: Sep 7, 2018
  2. Offline


    Bukkit.getOnlinePlayers() returns a collection of all online players. Use a while loop to add certain players with Random() according to the all online players. Store them in a collection. For every loop check if that player is already in the list. If so do another random. Repeat this untill you got the requested amount of players in your list.

    Then interate over all the players in your collection and broadcast their name. In addition try to store the UUID instead of the player. This will spare you some memory and possible NPE.
    Last edited: Sep 9, 2018
Thread Status:
Not open for further replies.

Share This Page