Solved How to create an Unknown argument message.

Discussion in 'Spigot Plugin Development' started by Cervinakuy, May 16, 2016.

  1. Hello everyone!

    Does anyone know how to create an unknown argument message?

    So if someone types /jep <gibberish>

    it would send a message saying that argument does not exist?

    - Cervinakuy
  2. This would have to be done in code.
  3. if !args[0].equalsIgnoreCase("args"){
    //wrong arguments
  4. Just create an array of valid arguments, when they type the command: loop through those arguements, if a match isn't found send them the message.
  5. onCommand
    if args swag return
    send unknown argument
    • Agree Agree x 1
  6. What @Alexandeh said is how I handle it. In my opinion the easiest and simplest-to-read way of handling commands is to "return" after every subcommand is handled. Example:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
      if (args.length == 0){
         return true; ****
      //if it hasn't returned yet, there's at least 1 arg
      if (args[0].equalsIgnoreCase("run"){
        return true; ***
      //if it hasn't returned yet, the user hasn't typed in anything that I originally planned to handle, so logically, they have an unknown command
      sender.sendMessage("Unknown command. Type /"+label+" for a help message.");
      return true; // because the method returns a Boolean, all commands have to return a value.
  7. Actually, a switch is a good way to go too:
    Code (Text):

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
         switch(args.length == 0 ? "" : args[0]) // if args length is 0 trigger default otherwise try looking for valid argument
              case "stuff": // this is a valid argument
                  // do stuff
                  return true;
                  sender.sendMessage("Woops! That argument is invalid."); // if it can't find a valid argument this is the default message that will pop-up  
                  return true;
  8. It really isn't
  9. That statement is quite vague. Please support your arguments! ;)
  10. JamesJ


    Why would you use a switch statement, for 2 possibilities?

    Please, just use a if/else statement. Stop reading all these "guides" that say switches are love, switches are life, use them everywhere.
  11. Also pretty sure that is case sensitive. If they typed "stuFf" it'd send them the unknown message
  12. My code was an example. With more arguments the switch becomes nicer to use.
    Switches can be quite powerful when used correctly.

    As far as that goes, you can simply translate the string to lowercase.
  13. Besides being non-case sensitive (you didn't use .toLowerCase()) there isn't a great way to handle args.length == 0 in your code there, but besides that it seems pretty acceptable. You could use an extra if statement to handle args.length == 0 but then again you'd still need to return before the code even reaches the switch statement. it doesn't make much sense to me to use both. But if you love your switch cases, that's fine to use.
    On a more personal level I hate typing them out so I never use them frankly and like @Alexandeh said they look like shit
    #13 akselm, May 16, 2016
    Last edited: May 16, 2016
  14. Yeah you could, I guess it all boils down to preference really. I personally do what @akselm suggested. Can we all agree that putting all the arguments in an array is beyond stupid though?
    • Funny Funny x 1
  15. Putting arguments in an array isn't 'stupid'. Perhaps the OP wanted to display a help message displaying valid arguments that could be used. In that case, it would be quite useful. You would be able to knockout two birds with one stone.
    • Agree Agree x 1
  16. He could do that at the end of the block.
  17. :( ;( :(
  18. I'm not going to read 3 pages of arguing (I've reported so a mod can cleanup the thread), so I'm sorry if this has been answered/solved already.

    What you're going to want to do is chain if/if else/else statements. Once you get to the very last else all known arguments should have been tried. You can display an error like normal here. ;)

    Code (Text):

    if (args[0].equalsIgnoreCase("something") {
        // do something
    } else if (args[0].equalsIgnoreCase("otherthing") {
        // other thing
    } else if (args[0].equalsIgnoreCase("reload") {
        // reload
    } else {
        // error here unknown subcommand.
    • Like Like x 2
  19. Probably one of the worst ways to accomplish this, a heavy chain of if/if else/else statements can be extremely hard to read.
  20. Instead of being destructive, why not be constructive? Lets see what you have to suggest for the OP instead of bantering with people.. And if you say make some abstract subcommand system I'm going to slap you. This is obviously a beginner thread with a beginner question that deserves a beginner answer to help get the OP going with his learning.
    • Agree Agree x 4