Solved How to grab a argument

Discussion in 'Spigot Plugin Development' started by ZevoGaems, Apr 14, 2017.

  1. I have a ban plugin and the command is supposed to work like /ban (player) (reason), I need to know how I would be able to grab the reason and implement it into the kick message.
     
  2. You'll use a stringbuilder and collect all the args after 0(or 1)
    Code (Text):
    StringBuilder message = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
    message.append(args[i] + " ");
    So the message will be all the args. See that you're getting i all after 1. And adding it to the stringbuilder along with a space.
     
    • Optimistic Optimistic x 1
  3. using the last arguments the player entered.

    Would be something like this:

    String reason = "";
    for(int arg = 2;arg < args.length;arg++){
    reason += args[arg] + " ";
    }
     
  4. That would leave out the first message of the argument?
     
  5. WAS

    WAS

    Here you go. Index is the first argument the messages occurs, and wraps to the end of the arguments. Same as most methods... in a function. :p

    Code (Java):
      public static String messageFromArgs(String[] args, int index) {
       
         int maxArgs = args.length;
         StringBuilder message = new StringBuilder();

         for ( int i = index; i < maxArgs; i++ ) {
           
           message.append(args[i]);
           
           if ( i < maxArgs ) {
             message.append(" ");
           }
           
         }
       
         return message.toString();
       
       }
     
    • Optimistic Optimistic x 1
  6. WAS

    WAS

    This will always place a space at the end of the message.
     
  7. FrostedSnowman

    Resource Staff

    no need.

    use
    https://docs.oracle.com/javase/8/do...in-java.lang.CharSequence-java.lang.Iterable-
     
  8. Use String#trim() to remove trailing/leading whitespace
     
  9. Code (Text):
                    StringBuilder message = new StringBuilder();
                    for (int i = 1; i < args.length; i++) {
                    message.append(args[i] + " ");
                    other.kickPlayer(ChatColor.DARK_RED + "You have been banned from MineGird by " + sender.getName() + "/n" + ChatColor.RED + "Reason: " + args.toString(args[2]));
                   
            }
    I don't know what you guys mean so what would I do to implement it into the ban message?
     
  10. WAS

    WAS

  11. WAS

    WAS

    Of course, but you can always just not place the space.
     
  12. For a quick response to that here:

    Code (Text):

     StringBuilder message = new StringBuilder();
                    for (int i = 1; i < args.length; i++) {
                    message.append(args[i] + " ");
            }
                other.kickPlayer(ChatColor.DARK_RED + "You have been banned from MineGird by " + sender.getName() + "/n" + ChatColor.RED + "Reason: " + message.toString());
                   
     
     
  13. What would I put right here
    Code (Text):
    message.toString()
    to show the reason for the ban
     
  14. WAS

    WAS

    That should be the reason.
     
  15. I mean what should I put there to represent the reason
     
  16. So, hopefully finishing this thread, this is what you'll do for kicking them
    Code (Text):
    player.kickPlayer(ChatColor.RED + "You have been kicked" + "\n" + ChatColor.GREEN + "For: " + ChatColor.YELLOW + message.toString()
    The message is the reason.
     
  17. @
    No I mean how would I show the reason the moderator put into the player what he did wrong according to what the moderator said
     
  18. WAS

    WAS

    ..... Bro.
     
  19. Just send the moderator the same reason by using "message.toString()" .. ?