Send message to all players with a certain permission

Discussion in 'Plugin Development' started by Flawedspirit, Oct 29, 2012.

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

    Flawedspirit

    I'm attempting to have a plugin I'm making broadcast a message to all players on the server that have a certain permission (like, for example, plugin_name.*, or a child, plugin_name.alert).

    I tried the simple option and went with
    Code:
    if(player.hasPermission("plugin_name.alert")) {
        player.sendMessage("You'll see this if you have that permission node!");
    }
    However, that appears to not be the way to go about it. So how would I do that? For some reason, I'm thinking of a hashmap of all players that have either of those permissions, but I've read through the bukkit API tutorial. So far what they have there isn't really helping me translate my idea into working code.

    However, I'm not exactly great at Java. If anyone could help, I'd be much obliged.
     
  2. Code:java
    1.  
    2. bukkit.broadcast("Message here", "Permission here");
    3.  

    this will broadcast a message to everyplayer with the your chosen permission
     
    Slayde_Blayde, Shortninja66 and adde like this.
  3. Offline

    RainoBoy97

    Code:
    for(World w : getServer().getWorlds()){
        for(Player p : w.getPlayers()){
            if(p.hasPermission("permission.node")){
                p.sendMessage("Hi");
            }
        }
    }
    Sorry for typos, made it on my iPad!
     
  4. you dont need the world for loop just do:
    PHP:
            for(Player p Bukkit.getOnlinePlayers()){
     
                if(
    p.hasPermission("permission.node")){
                    
    p.sendMessage("message");
                }
     
            }
     
    bobacadodl likes this.
  5. Offline

    RainoBoy97

    Yes, but i like it like that, more failproof in my opinion! :)
     
  6. Offline

    Lolmewn

    Actually, with the loops over all worlds, I imagine more maps being checked, causing a (somewhat) longer processing time.
     
    A3O2 likes this.
  7. Offline

    RainoBoy97

    Hmm, i make this as a method in my main class, mainly as a broadcastStaff... I have never experienced a longer process time :p
     
  8. Offline

    Lolmewn

    Nah, it's barely noticable. It's like milliseconds :)
     
  9. Offline

    Hoolean

    ^ While everybody was talking about for loops, everyone ignored this guy! ^
     
    Shortninja66, Bone008 and zack6849 like this.
  10. Offline

    Flawedspirit

    Ah, I had seen the bukkit.broadcast command, but I wasn't aware that you could filter it by permission too. Awesome. Thanks for the help, everyone!

    Oh, but question. If I'm calling this in my playerListener class, as opposed to my main class, how do I tell it what "bukkit" means?
     
  11. Offline

    Panjab

    Just import 'Bukkit' in your class ;)
     
  12. Offline

    RainoBoy97

    :)
     
Thread Status:
Not open for further replies.

Share This Page