Command not working properly

Discussion in 'Plugin Development' started by 9903286, Feb 1, 2012.

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

    9903286

    Every time i type in: /xpcomm it gives me a internal error! (Works when i type /xpcomm reload)
    Why?

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("xpcomm")){
                    
                if(args[0].contentEquals("reload")) {
                    
                    String name = sender.getName();
                    sender.sendMessage(name);
                    return true;
                    
                } else {
                    
                    sender.sendMessage(ChatColor.WHITE + "Usage: " + ChatColor.DARK_RED + "/xpc <command>");
                    return true; 
                    
                }
             }
            return false;
       
        }
    
     
  2. Offline

    theguynextdoor

    Try
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("xpcomm")) {
                sender.sendMessage(ChatColor.WHITE + "Usage: " + ChatColor.DARK_RED + "this is sent if you type it with nothing afterwards *i think*");
                if (args[0].equalsIgnoreCase("reload")) {
                    String name = sender.getName();
                    sender.sendMessage(name);
                    return true;
                }
                else {
                    sender.sendMessage(ChatColor.WHITE + "Usage: " + ChatColor.DARK_RED + "/xpc <command>");
                    return true;
                }
            }
            return false;
        }
    And tell me what happens and tbh i have no idea what the contentEquals stuff did, so i changed that to equalsIgnoreCase
     
  3. Offline

    9903286

    Nope, the exact same thing happend, only thing is that the "this is sent if you type it with nothing afterwards *i think*"
    message came up whatever i did.
     
  4. Offline

    LaLa

    Before you check that args[0] is equal to something, check that the args.length() > 0. If not, then return false or send them the proper use of the command because they didn't supply any arguments.
     
    9903286 likes this.
  5. Offline

    9903286

    This worked, thank you!
     
  6. Offline

    desht

    Google is your friend here (and so is stackoverflow, very handy site): http://stackoverflow.com/questions/...quals-and-stringcontentequals-methods-in-java

    Basically, contentEquals() can be used to compare a String with objects of other string-like types. equals()/equalsIgnoreCase() can only be used to compare with other Strings. In the code above, there's no particular need to use contentEquals().
     
  7. Offline

    9903286

    Uhm, that was me jsut trying random stuff to see if it worked.. :D
     
Thread Status:
Not open for further replies.

Share This Page