Add Listender Method on Comamnd Method

Discussion in 'Plugin Development' started by masterofsixpack, Apr 9, 2014.

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

    masterofsixpack

    Code:
    public class ToggleEnable implements CommandExecutor {
     
     
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
     
            Player p = (Player) sender;
     
     
            if (command.equals("disablechat")) {
     
                if (p.hasPermission("chattoggle.disable")) {
     
     
                    for (Player player : Bukkit.getOnlinePlayers())
                        if (player.hasPermission("chattoggle.user")) {
                            player.sendMessage("Chat has been disabled");
     
     
                        }
     
                }
     
            }
     
     
            return true;
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
     
     
                    event.setCancelled(true);
     
     
            }
        }
    

    How do I add the listener so that whenever the command is executed, the players cant speak?
     
  2. Offline

    Gater12

    masterofsixpack
    You should have a boolean value and set it to false for in the instance where chat is enabled. Then when they execute the command, have the variable equalled to true for when the chat is disabled. Then in the event, check if the variable is true (meaning the chat is disabled) and cancel the event.
     
  3. Offline

    masterofsixpack

    How would I go about doing that?
     
  4. Offline

    Wolfey

    masterofsixpack He just told you...

    I can already tell you have no idea how to code in Java, so you should probably learn that before jumping into Bukkit.

    Also, just a FYI, you're checking if the command is equal to a string. Learning Java will help fix silly mistakes like this.
    With that said, checking the command name should look something like this.
    Code:java
    1. if(command.getName().equalsIgnoreCase("commandName")) {
    2. // code
    3. }
     
  5. Offline

    coasterman10

    If you want the event handler to actually work, you need to make this class implement Listener too. Then, create an instance variable that is a boolean to store whether the chat is muted, and check that before cancelling the event.
     
Thread Status:
Not open for further replies.

Share This Page