Broadcasting a Message - Arguments

Discussion in 'Plugin Development' started by Dexeron, Nov 20, 2011.

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

    Dexeron

    I want to be able to do /bc <msg> and it will broadcast the <msg>. How to do this please?

    I thought it might be something like this:

    Code:
    public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[msg] args) {
                if(commandLabel.equalsIgnoreCase("bc" + [msg])){
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  2. Offline

    Terradominik

    try this:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("bc")) {
                    int laengeArray = args.length;
                    int indexArray = 0;
                    String text = "";
                    for (int i = 0; i < args.length; i++) {
                            text += args[i] + " ";
                    }
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + text);
                }
            return true;
     
  3. Offline

    Stephen A.

    You shouldn't just do Player player = (Player)sender .
    If you issue this from anywhere but a player(IRC plugin, Console) it will spit out tons of error code saying it can't map (insert whatever command was issued from here) to org.bukkit.entity.player. Or something like that.

    So you need some of instance of things:
    (Note this comes after right after if(cmd.getName().equalsIgnoreCase("bc"))
    And get rid of Player player = (Player)sender;
    It is re-added in here)
    Code:
    if(!(sender instanceof player))
    {
        if(!(sender instanceof org.bukkit.command.ConsoleCommandSender))
        {
            sender.sendMessage(Chatcolor.RED + "Error: You can't do that from there!");
        }else
        {
            if(args.lenth > 1)
            {
               int laengeArray = args.length;
                   int indexArray = 0;
                    String text = "";
                    for (int i = 0; i < args.length; i++) {
                           text += args[i] + " ";
                            }
                     Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + text);
            }
        }
    }else
    {
        Player p = (Player)sender;
        if(p.hasPermission("pemission.node.here"))
        {
            int laengeArray = args.length;
                   int indexArray = 0;
                     String text = "";
                    for (int i = 0; i < args.length; i++) {
                           text += args[i] + " ";
                            }
                     Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + text);
        }else
        {
             p.sendMessage(ChatColor.RED + "Error: No Permission!");
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page