Get all online players with permission

Discussion in 'Plugin Development' started by victor2748, Jul 22, 2014.

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

    victor2748

    Hey all, I was developing a plugin called BossMessage, and I faced a problem: I need to get all online players with a permission node. EX:

    Code:java
    1. List<Player> output = new ArrayList<Player>();
    2. for (Player player : Bukkit.getOnlinePlayers()) {
    3. if (player.hasPermission("my.permission.node") {
    4. output.add(player);
    5. }
    6. }
    7. return output;

    But is there a better way of doing it, without having to go through every single player that is online, and checking if he has that permission? And does BukkitAPI already have a method for that, or do I have to implement it myself? Please reply if you have any info about it. Thanks.
     
  2. Offline

    CorrieKay

    Considering that the CB method for sending a message to all players with a permission does just this, i'd say no.

    Regardless, internally if they had a way to do this, it would likely be functionally identical to this.
     
    victor2748 and Paradrakor like this.
  3. Offline

    Paradrakor

    This. Your code isn't hacky and doesn't look hacky (to me)
     
    victor2748 likes this.
  4. Offline

    mine-care

    Paradrakor I agree.
    victor2748 I think ur code is right, but just a correction, don't use player object in maps/lists ect. Or if you do make sure you dispose it properly on disconnect and on server restart/shut Down ect...
     
    victor2748 and Dragonphase like this.
  5. Offline

    Dragonphase

    victor2748 likes this.
  6. Offline

    1Rogue

    You need an O(n) time complexity because you need to verify every single player on the server. So, logically, checking each player once would be the most efficient (which you've done).
     
    Dragonphase and victor2748 like this.
Thread Status:
Not open for further replies.

Share This Page