[Solved] Help with command args

Discussion in 'Plugin Development' started by FurmigaHumana, Jun 9, 2012.

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

    FurmigaHumana

    What I need to do is that: When someone forgot to put the required argument in the command, the plugin will show the command help, here is my code:

    Code:
    if (args.length > 1) {
        //Command: /cc tool [add/del]
        if (args[0].equalsIgnoreCase("tool")) {
     
            if (args[1].equalsIgnoreCase("add")) {
                sender.sendMessage("tool add ok");
                //TODO: Tool Add Code
                return true;
            }
     
            if ((args[1].equalsIgnoreCase("del")) || (args[1].equalsIgnoreCase("dell"))) {
                sender.sendMessage("tool del ok");
                //TODO: Tool Del Code
                return true;
            }
     
            //If the player dont add the "add or del", or ask for help with the "?" or "help" show this:
            if ((args[1] == null) || (args.length >= 1) || ((args[1].equalsIgnoreCase("?")) || (args[1].equalsIgnoreCase("help")))) {
                msg(sender, "/cc tool add - Give you a tool to add blocks manualy to the database", "utily.Tool", CmdTag.NOTAG, false);
                msg(sender, "/cc tool del - Give you a tool to delete blocks manualy from the database", "utily.Tool", CmdTag.NOTAG, false);
                return true;
            }
        }
    }
    But if I type /cc tool, I dont receive anything, any error or nothing.

    Someone have any idea of how to do that?
     
  2. Offline

    CorrieKay

    its returning false past your first if statement, and theres nothing there to catch it.

    Another way to do this is a set of if/if else/ else blocks. For instance

    if(arg == add){
    add
    } else if (arg == del){
    del
    } else {
    help
    }
     
  3. Or you can just add the help at the end of that code block because you use return after each method and if no command matches it will show the help.

    Code:
        if (args[0].equalsIgnoreCase("tool")) {
     
            if (args[1].equalsIgnoreCase("add")) {
                sender.sendMessage("tool add ok");
                //TODO: Tool Add Code
                return true;
            }
     
            if ((args[1].equalsIgnoreCase("del")) || (args[1].equalsIgnoreCase("dell"))) {
                sender.sendMessage("tool del ok");
                //TODO: Tool Del Code
                return true;
            }
     
            //If the player dont add the "add or del", or ask for help with the "?" or "help" show this:
            if ((args[1] == null) || (args.length >= 1) || ((args[1].equalsIgnoreCase("?")) || (args[1].equalsIgnoreCase("help")))) {
                msg(sender, "/cc tool add - Give you a tool to add blocks manualy to the database", "utily.Tool", CmdTag.NOTAG, false);
                msg(sender, "/cc tool del - Give you a tool to delete blocks manualy from the database", "utily.Tool", CmdTag.NOTAG, false);
                return true;
            }
    
            // print help here.
        }
    Oh yes, and change that args.length > 1 to > 0 or >= 1 as CorrieKay said :p
     
  4. Offline

    CorrieKay

    Only if the first if statement is removed.
    if he types in /cc tool, args length is 1. his first if statement would skip the entire block.
    But then if someone didnt type args[1], an array index out of bounds exception would be thrown..
     
  5. Offline

    FurmigaHumana

    Digi set to >0 generate and exception

    I still can't make it work :mad:

    Code:
            if (args.length == 0) {
                if (args[0].equalsIgnoreCase("tool")) {
                    args = new String[]{"tool", "?"}; //Anything different than "add" or "del" should work.
                }
            }
            if (args.length > 1) {
                if (args[0].equalsIgnoreCase("tool")) {
                    if (args[1].equalsIgnoreCase("add")) {
                        sender.sendMessage("tool add ok");
                        //TODO: Tool Add Code
                        return true;
                    } else
                    if (args[1].equalsIgnoreCase("del")) {
                        sender.sendMessage("tool del ok");
                        //TODO: Tool Del Code
                        return true;
                    } else {
                        //HELP BLOCK
                    }
                }
            }

    Any more ideias?
     
  6. Well, first you need to understand an array.
    args[] is an array, its args.length variable returns the maximum amount of elements it can hold but elements start from 0 and the last element index is length minus 1.

    So, when you check if it's equal to 0 then it means it has no elements but you are getting the 1st element with args[0]... so you see where you did wrong ? :p
    I always like to use > or < and avoid == or >= or <= whenever possible with arrays to avoid confusion about the above rules, because you see, if you use args.length > 0 you know you can use the index 0, if you use args.length > 10 you know you can use args[10] :p
    Still, it depends on your needs, if you need to check if it doesn't have any arguments, you just use args.length == 0.

    Also, pick one style... either use return on each condition or use if-else, both are kinda pointless :p

    Here's how I'd do it:
    Code:
    if(args.length > 0)
    {
    	if(args[0].equalsIgnoreCase("tool"))
    	{
    		// typed /cc tool
    		
    		if(args.length > 1)
    		{
    			if(args[1].equalsIgnoreCase("add"))
    			{
    				// typed /cc tool add
    			}
    			else if(args[1].equalsIgnoreCase("del"))
    			{
    				// typed /cc tool del
    			}
    			else
    			{
    				// typed /cc tool ??? - print available commands
    			}
    		}
    		else
    		{
    			// typed /cc tool - print help
    		}
    	}
    	else
    	{
    		// typed /cc ??? - print list of subcommands
    	}
    }
    else
    {
    	// typed /cc, print list of sub-commands
    }
    
    return true;
     
  7. Offline

    FurmigaHumana

    AHHH Now I understand! I really was thinking wrong... Thank you so much!

    Here is my --final-- code: http://pastebin.com/raw.php?i=hGXVQrvB doing exactly what I need :D, now is just more 800 lines to fix D:

    I will remember to say your name when I finish :)
     
Thread Status:
Not open for further replies.

Share This Page