1.17.1 Looping through all players

Discussion in 'Spigot Plugin Development' started by CyberAlon, Jul 9, 2021.

  1. hey, I want to go through all the players and check if someone has specific permission, and if so to send a message to him. how can that be done?
  2. Code (Java):
    for (Player p : Bukkit.getOnlinePlayers()){
         if (p.hasPermission("PERM HERE"){
          // SEND THE MESSAGE
    • Useful Useful x 1
  3. Or
    Code (Text):
    (player) -> {
      if (player.hasPermission("permission") {
        //send message
  4. Code (Java):
    Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission("perm.node")).forEach(p -> p.sendMessage("Hi"));
    • Useful Useful x 2
    • Winner Winner x 1
  5. This seems to be a challange. I'll refuse that
  6. Or:
    Code (Java):
    Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission("<insert_permission_here>").forEach(p -> p.sendMessage("<insert_message_here>");
    Code (Java):
    Bukkit.getOnlinePlayers().stream().forEach(p -> if (p.hasPermission("<insert_permission_here>")) p.sendMessage("<insert_message_here>"));
  7. Thanks yall ill use this one
  8. You welcome man, there are infinite ways to achieve your goal, plese read my signature ;)
  9. There's a much easier and more efficient way to do this! Here's my code:
    Code (Java):
    public class YourClass {

        public YourClass() {
            // player iterator
            Iterator<Player> playerIterator = Bukkit.getOnlinePlayers().iterator();

            // players with permission
            List<Player> playersWithPermission = new ArrayList<>();

            // iterate
            while (playerIterator.hasNext()) {
                Player current = playerIterator.next();
                PlayerPermissionResolver playerPermissionResolver = new PlayerPermissionResolver(current);

                if (playerPermissionResolver.hasPermission("your.permission")) {

            StringMessageComputer stringMessageComputer = new StringMessageComputer();

            // loop through the players and send a message
            for (Player player : playersWithPermission) {
                // send them a message
                try {
                    Method sendMessage = Player.class.getMethod("sendMessage", String.class);
                    sendMessage.invoke(player, stringMessageComputer.compute());
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {


    class StringMessageComputer {

        public String compute() {
            return "Hello, world!";


    class PlayerPermissionResolver {

        private final Player context;

        public PlayerPermissionResolver(Player context) {
            this.context = context;

        public boolean hasPermission(String permission) {
            return context.hasPermission(permission);
    • Funny Funny x 12
    • Like Like x 1
    • Agree Agree x 1
    • Useful Useful x 1
  10. How is this faster?... Stream is the fastest way in Java to do almost anything - in this situation it is.
  11. I would not call it 'easier' but I have no clue if that is more efficient than my proposed way.
    • Like Like x 1
  12. This is a nice resource. Can I use this in my plugin library?
    • Winner Winner x 3
  13. Absolutely! Don't forget to credit me of course for this efficient way of coding which is much better than the other answers!
    • Friendly Friendly x 2
    • Like Like x 1
    • Winner Winner x 1
  14. More efficient doesnt have to mean faster. The code provided by @yakovliam is definitely easier to use then a stream, also it‘s way more readable and doesnt do any unnecessary bullshit.
    • Agree Agree x 3
  15. By easier to use you mean for people who have experience in Java and OOPs, for a new programmer looking at that resource will probably confuse him.
    Also, I dont see what uncessary bullshit the stream was doing since pretty much they both do the same on the background. The main difference is that one way iterates all players and the another saves the one with permissions, which is better if we are talking about a bigger server, yet this List of players with perms would need to be constantly updateded.
  16. How long will it take to realize...
    • Agree Agree x 1
    • Funny Funny x 1
  17. Ikr? Using StringMessageComputers is like the first thing you learn in every Java tutorial
    • Agree Agree x 1
    • Informative Informative x 1
  18. What I am trying to say, is that for someone new to Spigot, which most of the times are also new to Java and have not invested time in watching the same tutorials as you probably did, looking at your code can be a giant headache and it's better to start from the ground and then once he has a better idea of Java use more advanced and efficient ways like yours.
    Put yourself in a newcomer place, and think which way you would read the code better.
    I am not trying to criticise your code or saying that it is bad or a bad pratice, it is actual the inverse, just older programmers think in ways that younger programmers most of the times do not understand in order to save 1 or 2ms of execution time.
  19. My original post was satire; I am surprised it went this far though :LOL:
    • Funny Funny x 2
  20. Gosh you were making me freaking out xD
    • Like Like x 1