Getting rest of arguments?

Discussion in 'Plugin Development' started by ImPhantom, Feb 26, 2014.

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

    ImPhantom

    So i am creating a VERY easy command ("/warn [player] [wanring message]") And i have most of it created but there are just two issues when the sender types /warn [player] with no message nothing happens when an error is supposed to be thrown. And also when they correctly type the command it only shows the first word in the message.

    CODE for (CommandWarn.java):

    Code:java
    1. package net.imphantom.commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class CommandWarn implements CommandExecutor{
    10. @Override
    11. public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
    12.  
    13. Player p = (Player)s;
    14.  
    15. if(cmd.getName().equalsIgnoreCase("warn")) {
    16. if(s.hasPermission("phant0m.warn")) {
    17. if(args.length < 1)
    18. {
    19. s.sendMessage(ChatColor.RED + "Usage: /warn [player] [message]");
    20. }
    21. else if(args.length > 2)
    22. {
    23. Player p1 = p.getServer().getPlayer(args[0]);
    24. if(p1 == null)
    25. {
    26. return false;
    27. }
    28. s.sendMessage("[" + ChatColor.RED + " WARNING " + ChatColor.WHITE + "] " + args[1]);
    29. }
    30. }
    31. else
    32. {
    33. s.sendMessage("[phant0m] " + ChatColor.RED + "You do not have permission to run this command.");
    34. }
    35. }
    36. return false;
    37. }
    38. }


    Can anyone help me out? Im kinda stuck?
     
  2. Offline

    Alshain01

    Use a string builder as Assist suggested, you will need to loop through using an actual for loop for this because you want to start at args[1] rather than just iterating all the args.

    Code:java
    1.  
    2. StringBuilder myStringBuilder = new StringBuilder(args[1]);
    3. for(int a = 2; a < args.length -1; a++)
    4. myStringBuilder.append(" ").append(args[a]);
    5. String message = myStringBuilder.toString();
     
  3. Offline

    ImPhantom

    Assist + Alshain01
    I got it all figured out by using
    Code:java
    1. String msg = StringUtils.join(args, " ", 1, args.length);


    But now i messed up the getting player from the argument code. Its the same as above
    Issue: When i type the commands towards someone else Nothing happens.

    Can any of you help me out?
     
  4. Offline

    Alshain01

    args.length returns a scale of 1 to n. Indecies are 0 to n - 1. So you should be getting an IndexOutOfBounds error. Change that to args.length - 1.
     
Thread Status:
Not open for further replies.

Share This Page