Added command to toggler, unknown command

Discussion in 'Plugin Development' started by jorrik98, Sep 17, 2014.

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

    jorrik98

    So I decided to add a toggler to my antifenceglitch plugin, so admins can toggle the notifications they get when people are trying to fence glitch, on and off. but when i did /af it says unknown command, could someone tell me what I am doing wrong?

    AntiFence.java
    Code:
    package me.jorrik98;
     
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AntiFence extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable() {
            getLogger().info("PollosAntiFence by jorrik98 has been enabled!");
            new PlayerListener(this);
        }
       
        @Override
        public void onDisable() {
            getLogger().info("PollosAntiFence by jorrik98 has been disabled!");
        }
    }
    
    PlayerListener.java
    Code:
    package me.jorrik98;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.EventHandler;
     
    import me.jorrik98.AntiFence;
     
    public class PlayerListener implements Listener {
    List<String> toggled = new ArrayList<String>();
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("af")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player to use this command");
                    return false;
                }
                Player player = (Player) sender;
                if(toggled.contains(player.getName())) {
                    player.sendMessage(ChatColor.RED +  "No longer receing fence glitch notifications");
                    toggled.remove(player.getName());
                    return true;
                }
               
                player.sendMessage(ChatColor.RED +  "Now receing fence glitch notifications");
                toggled.add(player.getName());
                return true;
            }
            return false;
        }
       
        public PlayerListener(AntiFence plugin)  {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType().equals(Material.FENCE) || e.getClickedBlock().getType().equals(Material.NETHER_FENCE)) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.RED + "You actually thought fence glitchin still works? - jorrik98");
                    for(String player : toggled){
                          Bukkit.getPlayer(player).sendMessage(ChatColor.DARK_PURPLE +  e.getPlayer().getName() + ChatColor.AQUA + " just failed to fence glitch.");
                        }
                }   
            }
        }
    }
    
    Also, I just started coding, so don't rage at me for not knowing something basic, thanks.
     
  2. Offline

    AronTheGamer

    Listeners don't listen for Commands but for events, such as PlayerMoveEvent

    You should let your class implements CommandExecutor instead
     
  3. AronTheGamer
    He has an event in the class he needs to listen, so instead let the class implement both CommandExecutor and Listener.
     
    AronTheGamer likes this.
  4. Offline

    AronTheGamer

    Oh overlooked that.
    Code:java
    1.  
    2. package me.jorrik98;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.command.CommandExecutor;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.EventHandler;
    18.  
    19. import me.jorrik98.AntiFence;
    20.  
    21. public class PlayerListener implements Listener, CommandExecutor {
    22. List<String> toggled = new ArrayList<String>();
    23.  
    24. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    25. if (label.equalsIgnoreCase("af")) {
    26. if(!(sender instanceof Player)) {
    27. sender.sendMessage(ChatColor.RED + "You must be a player to use this command");
    28. return false;
    29. }
    30. Player player = (Player) sender;
    31. if(toggled.contains(player.getName())) {
    32. player.sendMessage(ChatColor.RED + "No longer receing fence glitch notifications");
    33. toggled.remove(player.getName());
    34. return true;
    35. }
    36.  
    37. player.sendMessage(ChatColor.RED + "Now receing fence glitch notifications");
    38. toggled.add(player.getName());
    39. return true;
    40. }
    41. return false;
    42. }
    43.  
    44. public PlayerListener(AntiFence plugin) {
    45. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    46. plugin.getServer().getCommand("af").setExecutor(this);
    47. }
    48.  
    49. @EventHandler
    50. public void onPlayerInteract(PlayerInteractEvent e) {
    51. Player p = e.getPlayer();
    52. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    53. if(e.getClickedBlock().getType().equals(Material.FENCE) || e.getClickedBlock().getType().equals(Material.NETHER_FENCE)) {
    54. e.setCancelled(true);
    55. p.sendMessage(ChatColor.RED + "You actually thought fence glitchin still works? - jorrik98");
    56. for(String player : toggled){
    57. Bukkit.getPlayer(player).sendMessage(ChatColor.DARK_PURPLE + e.getPlayer().getName() + ChatColor.AQUA + " just failed to fence glitch.");
    58. }
    59. }
    60. }
    61. }
    62. }
     
Thread Status:
Not open for further replies.

Share This Page