Commands won't work

Discussion in 'Plugin Development' started by sammyg7241, Mar 14, 2015.

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

    sammyg7241

    Hello, I am a fairly new plugin developer, and on all my plugins, the server recognizes the plugin, but the commands never work it just says unknown command. Any suggestions?
     
  2. Offline

    Funergy

    @sammyg7241 Did you define your commands in the plugin.yml?
     
  3. Offline

    sammyg7241

    @Funergy No I didn't. Do I need to do that?

    @Funergy I registered my command in the plug.yml and it still won't work

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Mar 14, 2015
  4. Offline

    Konato_K

  5. Offline

    SirMonkeyFood

    Post your code and plugin.yml. its pretty hard to figure out whats wrong without looking at it.@sammyg7241
     
  6. Offline

    sammyg7241

    Here is the main class file
    Code:
    package me.sammy.crb;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CosmicRealmsBans_Main extends JavaPlugin{
        public static CosmicRealmsBans_Main instance;
        public final static Logger log = Logger.getLogger("Minecraft");
        public void OnEnable(){
            CosmicRealmsBans_Main.instance = this;
            this.log("CosmicRealms Bans has been enabled!", Level.INFO);
            Bukkit.getServer().getPluginCommand("cr ban");
            Bukkit.getServer().getPluginCommand("cr kick");
        }
       
        public void OnDisable(){
            this.log("CosmicRealms Bans has been disabled!", Level.INFO);
           
        }
       
        public void log(String s,Level l){
            CosmicRealmsBans_Main.log.log(l, "[AM] " +s);
        }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("cr ban")) {
                if(player.hasPermission("cosmicbans.ban")) {
                   
                }
                if(args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Please enter a player name when using /cr ban!");
                }
               
                if(args.length <0) {
                    if(Bukkit.getPlayer(args[0]).isOnline()) {
                        Player tplayer = getServer().getPlayer(args[0]);
                        tplayer.setBanned(true);
                        tplayer.kickPlayer(ChatColor.GREEN + "You were banned by: " + ChatColor.RED + player.getName());
                        Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was banned!");
                    }else{
                        OfflinePlayer oplayer = getServer().getOfflinePlayer(args[0]);
                        oplayer.setBanned(true);
                    }
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("cr kick")) {
                if(player.hasPermission("cosmicbans.kick")) {
                    if(args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please enter a player name when using /cr kick!");
                    }
                   
                    if(args.length == 1) {
                        if(Bukkit.getPlayer(args[0]).isOnline()) {
                            Player tplayer = getServer().getPlayer(args[0]);
                            tplayer.kickPlayer(ChatColor.GREEN + "You were kicked by: " + ChatColor.RED + player.getName());
                            Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was kicked!");
                        }else{
                            player.sendMessage(ChatColor.RED + "That player is not online!");
                        }
                    }
                }
            }
           
           
            return false;
           
        }
    
    }
    
     
  7. Offline

    timtower Administrator Administrator Moderator

    @sammyg7241 And the plugin.yml?
    And your command check won't work that way, it can't contain spaces
     
  8. Offline

    LetsTalkTnTHere

    1. You cant put to args in an equalsIgnoreCase(); (I think)
    2. Why don't you put the code if the player has permission BETWEEN THE BRACKETS?
    3 <0 means smaller than and you can't have smaller than zero (in args)
    Try fixing that as first
     
  9. Offline

    sammyg7241

    OK here is the new main class file
    Code:
    package me.sammy.crb;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CosmicRealmsBans_Main extends JavaPlugin{
        public static CosmicRealmsBans_Main instance;
        public final static Logger log = Logger.getLogger("Minecraft");
        public void OnEnable(){
            CosmicRealmsBans_Main.instance = this;
            this.log("CosmicRealms Bans has been enabled!", Level.INFO);
            Bukkit.getServer().getPluginCommand("crban");
            Bukkit.getServer().getPluginCommand("crkick");
        }
       
        public void OnDisable(){
            this.log("CosmicRealms Bans has been disabled!", Level.INFO);
           
        }
       
        public void log(String s,Level l){
            CosmicRealmsBans_Main.log.log(l, "[AM] " +s);
        }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("crban")) {
                if(player.hasPermission("cosmicbans.ban")) {
                   
                }
                if(args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Please enter a player name when using /cr ban!");
                }
               
                if(args.length >0) {
                    if(Bukkit.getPlayer(args[0]).isOnline()) {
                        Player tplayer = getServer().getPlayer(args[0]);
                        tplayer.setBanned(true);
                        tplayer.kickPlayer(ChatColor.GREEN + "You were banned by: " + ChatColor.RED + player.getName());
                        Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was banned!");
                    }else{
                        OfflinePlayer oplayer = getServer().getOfflinePlayer(args[0]);
                        oplayer.setBanned(true);
                    }
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("crkick")) {
                if(player.hasPermission("cosmicbans.kick")) {
                    if(args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please enter a player name when using /cr kick!");
                    }
                   
                    if(args.length >0) {
                        if(Bukkit.getPlayer(args[0]).isOnline()) {
                            Player tplayer = getServer().getPlayer(args[0]);
                            tplayer.kickPlayer(ChatColor.GREEN + "You were kicked by: " + ChatColor.RED + player.getName());
                            Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was kicked!");
                        }else{
                            player.sendMessage(ChatColor.RED + "That player is not online!");
                        }
                    }
                }
            }
           
           
            return false;
           
        }
    
    }
    
    Ant the plugin.yml
    Code:
    name: CosmicRealms Bans
    version: 1.0.0
    main: me.sammy.crb.CosmicRealmsBans_Main
    description: The custom ban/kick plugin for CosmicRealms
    author: Sammy
    commands:
    crban:
        description: This is used to ban a player!
        usage: /<command>
    crkick:
        description: This is used to kick a player!
        usage: /<command>
    
     
  10. Offline

    teej107

  11. Offline

    Ruptur

    @sammyg7241
    You need to return or use else statements in the commands.
    You might want to return true, so there isnt feedback on the command
     
  12. Offline

    anson023

    i change some code that strange..

    Code:
    package me.sammy.crb;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class CosmicRealmsBans_Main extends JavaPlugin{
        public static CosmicRealmsBans_Main instance;
        public final static Logger log = Logger.getLogger("Minecraft");
        public void OnEnable(){
            CosmicRealmsBans_Main.instance = this;
            this.log("CosmicRealms Bans has been enabled!", Level.INFO);
            Bukkit.getServer().getPluginCommand("crban");
            Bukkit.getServer().getPluginCommand("crkick");
        }
    
        public void OnDisable(){
            this.log("CosmicRealms Bans has been disabled!", Level.INFO);
        
        }
    
        public void log(String s,Level l){
            CosmicRealmsBans_Main.log.log(l, "[AM] " +s);
        }
    
        @SuppressWarnings("deprecation") // i can't understand what is this..
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("crban")) {
                if(!player.hasPermission("cosmicbans.ban")) {  //i think ! can explain to not
                  return true;                                                       //if player don't have permission "cosmicbans" then return
                }
                if(args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Please enter a player name when using /cr ban!");
                    return true;
                }
            
                if(args.length >0) {
                    if(Bukkit.getPlayer(args[0]).isOnline()) {
                        Player tplayer = getServer().getPlayer(args[0]);
                        tplayer.setBanned(true);
                        tplayer.kickPlayer(ChatColor.GREEN + "You were banned by: " + ChatColor.RED + player.getName());
                        Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was banned!");
                    }else{
                        OfflinePlayer oplayer = getServer().getOfflinePlayer(args[0]);
                        oplayer.setBanned(true);
                    }
                }
            }
        
            if(cmd.getName().equalsIgnoreCase("crkick")) {
                if(!player.hasPermission("cosmicbans.kick")) {  //strange here
                   return true;
                }
                    if(args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please enter a player name when using /crkick!");
                    }
                
                    if(args.length >0) {
                        if(Bukkit.getPlayer(args[0]).isOnline()) {
                            Player tplayer = getServer().getPlayer(args[0]);
                            tplayer.kickPlayer(ChatColor.GREEN + "You were kicked by: " + ChatColor.RED + player.getName());
                            Bukkit.broadcastMessage(ChatColor.AQUA + tplayer.getName() + "was kicked!");
                        }else{
                            player.sendMessage(ChatColor.RED + "That player is not online!");
                        }
                    }
                }
                 return false;
        }
    }
    and then if u want player use /cr ban and not /crban
    u can use switch
    if there's some mistake please tell me and i will correct it
    i'm a starter too :D
    Code:
    public boolean onCommand(CommandSender s , Command cmd , String lable , String[] args) {
    Player p = (Player) s;
    if (lable.equalsIgnoreCase("cr")){
      if (args.length == 0){
         p.sendMessage("What message u want");
         return true;
    }
    switch (args[0].toLowerCase()){
                case "ban":
                    if (!p.hasPermission("cosmicbans.ban")){
                        p.sendMessage("You don't have permission to use this command.");
                        return true;
                    }
                   if (args[1] == null){
                      p.sendMessage("Please enter a player name when using /cr ban!");
                      return true;
                   }
                   //here write things u want it happen
                case "kick":
                    // done it urself here :D
               }
    }
    return false;
    }
    
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Mar 16, 2015
  13. Offline

    TylerLFC

    With your main code you need to change the "OnEnable" and "OnDisable" to "onEnable and "onDisable" and also layout the plugin.yml like:
    Code:
    commands:
      crban:
        description: This is used to ban a player!
        usage: /<command>
      crkick:
        description: This is used to kick a player!
        usage: /<command> 
     
Thread Status:
Not open for further replies.

Share This Page