Solved if (all.hasPermission("PERMISSION")) {

Discussion in 'Plugin Development' started by M&R Coding, Jun 20, 2015.

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

    M&R Coding

    So my friend and I have started developing a plugin and we have it set up so that if someone does this thing in game it will alert everyone on the server that has the permission but, when we do it, only 1 person gets the notification rather than everyone on the server?

    if ((arrayOfPlayer = Bukkit.getServer().getOnlinePlayers()).length != 0) { Player all = arrayOfPlayer[0];

    if (all.hasPermission("OUR PERMISSION NODE")) {
    all.sendMessage(THE MESSAGE);
    }
    return true;
    }

    For some reason it only sends it to one person rather than everyone on the entire server with that permission node??!?!? Need help ASAP, thanks in advance.
     
  2. @M&R Coding You are only telling it to send to 1 person. You are setting all to the first player in the collection.
     
  3. Offline

    Konato_K

  4. Offline

    Ratismal

    I would iterate through the array using a foreach
    Code:
    if ((arrayOfPlayer = Bukkit.getServer().getOnlinePlayers()).length != 0) { 
      for (Player p : arrayOfPlayer) {
        if (p.hasPermission(permissionNode)) {
          p.sendMessage(message);
        }
      }
      return true;
    }
    
    But that's just me
     
  5. Offline

    mythbusterma

    @Ratismal

    Some might just use:

    Bukkit.broadcast(<message>,<permission>);
     
  6. Offline

    MrNewbie

    ^ Agreed. Using a method where it checks all players in an array list is just wasting time.
     
  7. Offline

    SuperSniper

    Use a "for" statement.

    Code:
    for(Player players : Bukkit.getOnlinePlayers()) {
    players.SOMETHING
     
  8. And how exactly do you think broadcast() actually works? It's basically the same thing, except slightly different and you don't have to write the full method yourself :)
     
  9. Offline

    MrNewbie

    Adam I know. I was saying its a waste of time to make a script to check if all players have a permission when Bukkit already has one built in.
     
  10. Offline

    Ratismal

    @MrNewbie This may be so, but look at the OP's code. They thought that arrayOfPlayer[0] would return every entry in the array. While simply using Bukkit's inherent methods in this specific case may be more efficient, my solution was more educational and applicable to scenarios where Bukkit doesn't provide a method for you. Iterating through an array is a fairly essential skill to have.
     
  11. Offline

    Konato_K

    This also sends a message to the console as far as I remember, which is useful in most cases.
     
  12. Offline

    mythbusterma

    @Ratismal

    Except that you didn't explain it to him, you just gave him the (sub-par) code. I fail to see how that validates your answer. This isn't a forum for learning basic Java, and your code is extremely difficult for a new-comer to understand. It took me more than one read-through to figure out what was going on.

    Also, it isn't an array.
     
  13. Offline

    M&R Coding

    haha thanks guys, I got it to work with all of your input! Highly appreciated :D
     
  14. Offline

    nverdier

    Please mark this thread as solved ;D
     
Thread Status:
Not open for further replies.

Share This Page