Solved Multiple Arguments

Discussion in 'Spigot Plugin Development' started by Fluddershy, May 14, 2017.

  1. Hello, I know this is stupid of me to ask, but I've always had trouble making commands that involve 3+ arguments. They have just always troubled me. Yes, I have looked up videos and stuff but it's hard. For example, I want a command that is /fist [player] [integer] [boolean] [string] [player]. I know that command doesn't make sense, but that's not the point. The point is, that command has 5 arguments. I've always have trouble checking if the command has this many arguments, then do this. It's hard for me to check if some arguments are not entered and I just get errors and I can never really seem to fix them. Therefore, I'm coming on here to see if you guys have anything special in mind, and maybe you can give me some example / pseudo code. By no means am I asking you to spoonfeed me, I'm good. I just need help with adding multiple arguments and checking if some aren't entered, then sending them a message. Thanks in advance, I know the Spigot community will not fail me. Love you all
    Edit: I know how to make commands like fucking /fly on, /fly off, /fly toggle, /fly set 0. So, don't think I'm retarded when it comes to commands. I can just can make crazy like 7 arugument commands, if you need another example, /fly [boolean] [player] [int] [String] or anything above.
    #1 Fluddershy, May 14, 2017
    Last edited: May 14, 2017
  2. Code (Text):
    if (cmd#getName#equals to ("fist")) {
                    if (args#length = 0) {
                        //Enter your code here
                          //First argument

                    } else if (args#length = 1)  {
                           //Enter your code here
                          //Second argument

                    } else if (args#length = 2)  {
                           //Enter your code here
                          //Third argument

                    } else if (args#length = 3)  {
                           //Enter your code here
                          //Fourth argument

    By that, you can add arguments to your command
    • Creative Creative x 1
  3. Mas


    This *shouldn't* be difficult:
    • Check arguments length: args.length (assuming your arguments array is named 'args').
    • To check if a String is an Integer, try Integer.parseInt(String) and a NumberFormatException will be thrown if it is not a valid int.
    • Get an online player with Bukkit.getPlayer(String) where String is the player name. Returns null if no player with that name is online.
    • To check the boolean valie of a String, use Boolean.parseBoolean(String)
    As long as you remember to check arguments length I'm not sure what errors you could get our of this.

    Off topic, but your signature is quite funny considering you are asking for help with command arguments :p
  4. What if you want arguments that have other features, it's hard to explain:
    Like I have /ironborn setgold [gamemode] [int] [string] [boolean]
    and I want another one to be /ironborn setiron [string] [boolean] [int]
    This examples I'm making have nothing to do with what I'm creating, just examples!
    Also: I've been doing that method. That's not my problem, my problem is checking if there aren't enough args, and if there are.
    Okay, I'm going to create some code, and we can correct it on the way, if we can?
    #4 Fluddershy, May 14, 2017
    Last edited: May 14, 2017
  5. why why do people always use # instead of the actual . ?
  6. Because that's the way you denote class members per javadoc. Many other languages use the convention of ::
    • Agree Agree x 1
  7. It makes the full method signature obvious compared to method chaining. ie java.lang.runtime#getRuntime leaves nothing ambiguous about what it means.

    @OP You should start by learning java, but if you really dont want to:

    There is no way to make a command that detects if things have been omitted, unless you specifically identify each argument in some way and investigate it to see if it can be parsed or converted to a string, int, whatever.
    Typically optional arguments can go on the end of the command and can only be omitted in order of last to first, not randomly.

    Just check the length of the array and handle the number of arguments in it, treating them as if they are in the expected order.
  8. Suggestion:

    I think you can do this:

    Code (Text):
    public void someMethod( Object ... args ){ // Stuff }
    And you, the dev, will know what each argument is and will be
    able to cast each object to its respective data type.

    I'm not entirely sure though.
    • Optimistic Optimistic x 1
  9. Alright, I figured it out. Thanks to everyone for helping me with this. What I learned from this is that you to be very specific and to the people who are having the same problem, just always check the last argument of the one you are appending from. That probably doesn't make sense, but whatever. Thanks again to everyone for helping me with my stupid question. Have a nice day