Solved Send message to players with permission

Discussion in 'Plugin Development' started by GreatMinerZ, Dec 12, 2013.

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

    GreatMinerZ

    So i'm making a plugin where you can /report other players to the staff online at that moment.
    But i don't know how i would send the players with permission the message after "/report".
    How would i send them a message like "Report from john321: bob123 griefed my house."
    if john321 typed this: "/report bob123 griefed my house."

    Already have this:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String[] args) {
    2.  
    3. if (cmd.getName().equalsIgnoreCase("report"));
    4. //Code
    5.  
    6. return false;
    7.  
    8. }


    Thanks!
     
  2. Offline

    Deleted user

    GreatMinerZ

    The easiest way I can think of (but not the most resource efficient) is to use a foreach loop with getAllPlayers()

    Code:
    for (Player p : Bukkit.getOnlinePlayers())
    {
         if (p.hasPermission("report")
         {
              p.sendMessage("Report from " + reporter.getName() + ": " + message);
          }
    }
    
    GreatMinerZ
    So, assuming the command is /report, and the format is /report <message>, then the code would be:

    Code:
    public boolean onCommand(CommandSender cs, Command c, String l, String[] args) {
      if (c.getName().equalsIgnoreCase("report") {
        if (args.length == 0) {
          cs.sendMessage("Not enough arguments");
        } else {
          StringBuilder sb = new StringBuilder();
          for (String s : args) {
            sb.append(s + " ");
          }
          for (Player p : Bukkit.getOnlinePlayers()) {
            if (p.hasPermission("report") {
              p.sendMessage("Report by " + cs.getName() + ": " + sb.toString());
            }
        }
    return true;
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 12, 2022
Thread Status:
Not open for further replies.

Share This Page