Broadcast Plugin not working

Discussion in 'Plugin Development' started by arnoldmcshuffleberry, Jan 6, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I made this plugin for broadcasting messages, however, when I run the command nothing happens. Here is the code:

    Code:
    package me.PoTigger.announcer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Broadcast extends JavaPlugin {
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                Player p = (Player) sender;
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please write a message you would like to broadcast.");
                } else {
                    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "[BROADCAST]" + ChatColor.RESET + ChatColor.DARK_RED + message(args));
                }
            }
            return false;
        }
        public String message(String[] args) {
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < args.length; i++)
            builder.append(args[i]);
            builder.append(" ");
            return builder.toString().trim();
        }
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Max8801

    Does the plugin load? Do you get any errors? Have you registered the command in the plugin.yml file? What exactly happens when you execute the command?

    Also, check if the CommandSender is a player before casting it to one.
     
  4. @Max8801 Yes, the plugin loads fine. I get no errors from the console when it loads up nor when I run it. I have registered it in the plugin.yml. When I execute the command nothing shows in-game and there is nothing shown in the console, except for the usual message that shows what commands players try to use.

    Also, I was thinking about having it check if it's a player, but shouldn't it be fine if the command is run from the console? Seeing as it's just a plugin that sends a message.
     
  5. Offline

    Protophite

    If the command is being sent from console there will be errors. The console isn't a Player.
     
  6. Offline

    Max8801

    Yes, that does not explain why absolutely nothing happens when the command is executed though.
    Would you mind posting the content of your plugin.yml file?
     
  7. No problem. @Max8801
    Code:
    name: Announcer
    version: 1.0
    main: me.PoTigger.announcer.Announcer
    description: A simple announcer!
    
    commands:
        annreload:
            usage: /<command>
            description: Reload Announcer Config
       
        broadcast:
            usage: /<command>
            description: Broadcasts a message.
            aliases: [b]
    You can ignore the annreload and announcer parts, it's part of an announcer plugin in the same project.
     
  8. Offline

    Max8801

    I see the problem now. In each plugin, only one class (the main class) can extend JavaPlugin. Judging by your plugin.yml, the main class is me.<Package Name>.Announcer, while the broadcast command is in me.<Package Name>.Broadcast, which is why the command executor is not automatically registered. You will have to do this manually if you wish to seperate the main class from the Broadcast command executor. See this thread for further instructions.
     
Thread Status:
Not open for further replies.

Share This Page