Solved Checking if any player with permission is online

Discussion in 'Plugin Development' started by KarimAKL, May 26, 2018.

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

    KarimAKL

    I've made a plugin where i send a message to all players with a permission but if no players with that permission is online it sends the the usage(from plugin.yml) so that's why i want to check if any players with permission is online.
    Code to check if player has permission and then send a message to them:
    Code:Java
    1.  
    2. for (Player op : Bukkit.getOnlinePlayers()) {
    3. if (op.hasPermission("some.permission")) {
    4. op.sendMessage(Color.here("Some message"));
    5. return true;
    6. }
    7. }
    8.  

    My guess is i should loop through all online players which i already have with the "for (Player op : Bukkit.getOnlinePlayers()) {" i guess, but what should i do next? :7 This might be a pretty dumb question, sorry. :/
    EDIT: I've tried searching around but all i could find is posts about how to check for permission. (like i have in the code)
     
    Last edited by a moderator: May 26, 2018
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL This is correct already.
    You need to handle something after the loop to send an alternate message.
     
  3. Offline

    KarimAKL

    @timtower What i want is for it to send a message to the sender saying "no players with some.permission is online at the moment" instead of the usage. (from plugin.yml) How would i do that?
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You do something after the loop. There you send a message.
     
  5. Offline

    KarimAKL

    @timtower "something" doesn't really help me much. :7 Where can i read about what i need to do here?
     
  6. Online

    timtower Administrator Administrator Moderator

    You send the message after the loop and return true.
    That is all you need to do.
    No checks are needed anymore because the loop returns already if somebody with the permissions is online.
     
  7. Offline

    KarimAKL

    @timtower But if no-one with the permission is online then it returns the usage, how do i check if anyone with the perm is online?
     
  8. Online

    timtower Administrator Administrator Moderator

    You have the return for that!
    You don't need to check!
    You just send the message and return!
     
  9. Offline

    KarimAKL

    @timtower Nvm, i just fixed it by doing this:
    Code:Java
    1.  
    2. for (Player op : Bukkit.getOnlinePlayers()) {
    3. if (op.hasPermission("some.permission")) {
    4. op.sendMessage(Color.here("Some message"));
    5. return true;
    6. }
    7. return true;
    8. }
    9.  

    Solved.
    EDIT: Not exactly what i wanted but it removes the usage message.
     
  10. Online

    timtower Administrator Administrator Moderator

    @KarimAKL That is what I told you to do multiple times.
    And then send an alternative message.
     
  11. Offline

    KarimAKL

    @timtower Oh, i see. My bad i didn't understand what you meant. Thank you for the help tho. :)
     
Thread Status:
Not open for further replies.

Share This Page