How to get arguments to string

Discussion in 'Plugin Development' started by OptimalBread, May 14, 2014.

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

    OptimalBread

    So I am trying to make the plugin where I type /test message here It will say to the player "message here". Here is what I have so far for the second part:
    Code:java
    1. String string = "";
    2. if (args[1].contains(string)) {
    3. p.sendMessage(string);

    anyone have a clue to what I am doing wrong. Any help would be great thanks!
     
  2. Offline

    harry10potter

    You want to make /test a command? That sends the text the player typed back to the player?
     
  3. Offline

    OptimalBread

    harry10potter Yes. I might not have been clear on what I wanted
     
  4. Offline

    Drkmaster83

    Well, args is a String array, so you can invoke .length on it to get the amount of arguments in the command.
    "/test" is the commandLabel,
    "message" is args[0],
    "here" is args[1].
    Here's a hint: You're going to use fixed repetition with a for-loop, using args.length. Each time you repeat, you're going to add the current String element at the array index that you created in the for-loop, followed by a space.
     
  5. Offline

    OptimalBread

  6. Offline

    harry10potter

    Great! :D
     
  7. Offline

    Gamecube762

    You would want to check if the args isn't 0 before you put it into a string, otherwise you could still get an error.

    Here is some psuedo like code to help you get an idea of what Drkmaster83 :

    message = ""
    if args is not 0 then:

    for String arg : args
    message = message .. arg .. " "

    This should help you with how loops work, looking at the "Enhanced for loop in Java" section will help you with what you need.
     
  8. Offline

    Drkmaster83

    For this specific portion of code, that is more than applicable, but I was thinking more of repetition using an int data type, that way you could grab the index of what you're starting at, not just all of it. (If he had a player name as args[0], your way wouldn't be applicable).
     
    Gamecube762 likes this.
  9. Offline

    itzrobotix

    Could you not just do;
    Code:java
    1. String message = StringUtils.join(args, ' ', 0, args.length);

    StringUtils.join(args, ' ', 0, args.length)
    First parameter is what you want to join. (Has to be a string list)
    Second parameter is what you want to split each argument joined with. (A space)
    Third parameter is at what index you want to start in the list. (0 is the first one, being Java an all)
    Fourth parameter is at what index you want to end in the list. (The length of it)
     
    mazentheamazin and rsod like this.
Thread Status:
Not open for further replies.

Share This Page