Solved my /broadcast doesn't work HELP!

Discussion in 'Plugin Development' started by Roadrunner2222, Feb 1, 2016.

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

    Roadrunner2222

    Code:
    package broadcast;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("broadcast")){
                Bukkit.broadcast(ChatColor.DARK_RED + " [Broadcast] " + args[0], "you dont have permission to perform this command");
                if (args[0] == null){
                    sender.sendMessage(ChatColor.RED + "/broadcast <message>");
                }
            }
            return true;
        }
    
    }
    hello everyone,

    i'm trying to make a broadcast plugin so if you do the command /broadcast message the plugin broadcasts the message to all the players on the server.

    but... there is a problem. if i try to do the command like /broadcast message the plugin does nothing, and if i do only /broadcast the server says: an internal error occurred while attempting to perform this command

    pls help! ( i'm a beginning programmer of minecraft plugins so dont bully me pls...)
     
  2. Offline

    MrTigreroux

    Try to use Bukkit.broadcastMessage("Text") in place of Bukkit.broadcast("Text"). If it doesn't work too, add .getServer() next to Bukkit.
    But i don't understand why you broadcast the message before the check of args[0], and i would use ((Player) sender).sendMessage("You don't have permission") in place of broadcasting this message to all players on the server.
     
  3. Offline

    fatpigsarefat

    Have you added it in plugin.yml?
     
  4. Offline

    teej107

    You aren't making checks to prevent IndexOutOfBoundsExceptions. You are using the wrong method. It should be Bukkit.broadcastMessage().
    You don't need to cast to a Player to send them a message. CommandSender has the sendMessage method.
     
  5. Offline

    WolfMage1

    And never cast Player to a player without knowing a player sent the command.
     
  6. Offline

    Roadrunner2222

    thx it works for now with this code:
    Code:
                if (args[0] == null){
                    sender.sendMessage(ChatColor.RED + "/broadcast <message>");
                    }
                else {Bukkit.broadcastMessage(ChatColor.DARK_RED + "[Broadcast] " + ChatColor.GREEN + args[0]);
    but the if code with (args[0]) doesnt work, can anyone help me with this?
    also i want to message multiple words how can i fix this?

    thx for al your help for now!
     
  7. Offline

    sam_0208

    @Roadrunner2222 you should check if the length of the args is higher then 0. If you try to get args[0] if it doesn't exists it will throw a IndexOutOfBoundsExeption
     
  8. Offline

    boomboompower

    @Roadrunner2222
    This is how I do it with my plugin
    Code:
    if (args.length == 0) {
      sender.sendMessage("Please add some arguments!");
    } else {
      // Other code
    
     
  9. Offline

    Roadrunner2222

    thx al for your help! it does now work!
     
Thread Status:
Not open for further replies.

Share This Page