Unknown command?

Discussion in 'Spigot Plugin Development' started by GeeGeeTom, May 17, 2015.

  1. Hi, I'm getting into Java/Bukkit programming and am creating this plugin for testing purposes, I used to know how to do this stuff and am trying to refresh myself. Can anyone tell me why I am getting an unknown command error when I say /mc (/cc works)
    Code (Text):
    package org.mcprison.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Chatmanager extends JavaPlugin implements Listener{
        public static boolean chatMuted = false;
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this,  this);
           
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            if (chatMuted = true){
                p.sendMessage("§cCannot talk in chat because chat is currently muted. For help with a problem, use /helpop!");
                e.setCancelled(true);
               
            }else{
                e.setCancelled(false);
            }
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("cc")) {
                if(!(sender instanceof Player)) {
                   
                    int clearloop = 0;
                    while( clearloop < 200 ) {
                        Bukkit.broadcastMessage("");
                        clearloop++;
                    }
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been cleared by §9Console§e.");
                   
                }
                else {
                int clearloop = 0;
                while( clearloop < 200 ) {
                    Bukkit.broadcastMessage("");
                    clearloop++;
                }
                Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been cleared by §9" + sender.getName() + "§e.");
                }
            }
            if(cmd.getName().equalsIgnoreCase("mc")) {
                if (chatMuted = false) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been muted by §" + sender.getName() + "§e.");
                    chatMuted = true;
                   
                }else{
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been unmuted by §" + sender.getName() + "§e.");
                    chatMuted = false;
                   
                }
               
            }

            return true;
        }
    }
               
           
     
     
  2. 'mc' isn't registered in your 'plugin.yml' file.
     
    • Like Like x 1
  3. Thanks :) never wouldve noticed that xD
     
  4. @Skionz Now I'm having another issue, when I type /mc it just says chat was unmuted, but doesn't let me talk.
     
  5. You are setting 'chatMuted' to true inside an if statement meaning it will pass every time.
     
  6. Then how do I toggle the boolean when I type /mc @Skionz ?
     
  7. Change the boolean with the command, check if the boolean is true when the player chats.
     
  8. @Skionz I don't think I understand. If I change the boolean with the command, I will have to put chatMuted = !chatMuted within the command's IF statement...
     
  9. @Skionz I honestly have no idea what I'm supposed to get from that
     
  10. The reason why you cannot find this command in the server is due to the fact that you don't have it in your plugin.yml (Like Skionz said).

    Simply add it by adding this into your plugin.yml
    Code (Text):
    commands:
        command:
            description: <description here>
            usage: /<command> (Keep it that way <--)
            permission: <permission>
        anothercommand:
            description: <description here>
            usage: /<command>
            permission: <permission>
    # Etc etc.
    I also suggest you add permissions to muting the chat, as if you do not have them all players on the server will be able to mute and unmute chat. It can be simply registered by one line of code:

    Code (Text):
    if(player.hasPermission("somepermission.permission") {
    //Do this
    } else {
    // Tell player they have no permission
    }
    Now to add permissions to your plugin.yml..
    Code (Text):
    permissions:
        permission.*: (always required)
            description: <description>
            default: <normally "op">
            children:
                permission.child: true
        permission.child:
            description: <description>
            default: <normally "op">
           
    Nice and simple, just write 3-4 more bukkit plugins and you will remember this just as well as your own name.
     
  11. Checking boolean statements like this:
    PHP:
    if(yourStatement == true_false)
    {

    }
    is redundant. You won't need to do this, it is already checked.