I need help..

Discussion in 'Plugin Development' started by Xtremebrawler, Jun 7, 2014.

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

    Xtremebrawler

    I am trying to do a message command on my bukkit plugin, and this is what I have:

    Code:java
    1. if(commandLabel.equalsIgnoreCase("msg"))
    2. {
    3. try {
    4. Player player = (Player) theSender;
    5. Player playerToMessage = Bukkit.getPlayer(args[0]);
    6. player.sendMessage(ChatColor.DARK_GRAY + "[Me] --> " + "[" + ChatColor.DARK_AQUA + playerToMessage.getName() + ChatColor.DARK_GRAY + "] " + messageSent);
    7. playerToMessage.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_AQUA + player.getName() + ChatColor.DARK_GRAY +"] --> [Me] " + messageSent);
    8.  
    9.  
    10.  
    11. }
    12.  
    13. catch(IndexOutOfBoundsException exception)
    14. {
    15. Player player = (Player) theSender;
    16. player.sendMessage(ChatColor.DARK_RED + "Usage: /msg <player> <msg>");
    17. }


    It works fine, except it only lets you put one word in the message. I can't figure out how to extend to args to open up to more words
     
  2. Offline

    rangewonk

    You should post this in the Plugin Development section on the forums.
     
  3. Offline

    JaguarJo

    Thread moved to a more appropriate section.
     
  4. Offline

    teej107

    Xtremebrawler Whats the problem exactly? What do you want to happen? What happens insetad?
     
  5. Offline

    TCO_007

    I havent built a msg plugin but personally I would have to believe it would be something like args > 0 or something like that. It might be possible but I honest have not tried it. Ill input your code and see if I can come up with anything. So if you look at it algebraically though, where x = args, it should look like x >0 or x>=1 I believe
    Xtremebrawler

    so does it work for you if where it says args[0] you put args[0] > 0 so like this.
    Code:java
    1. Player playerToMessage = Bukkit.getPlayer(args[0] > 0);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
  6. Offline

    coasterman10

    Command arguments are separated by spaces, so every word after the player's name will be another element in the args array. Iterate through the rest of the argument list and use a StringBuilder to rebuild the message.

    ???
     
  7. Offline

    mine-care

    TCO_007 why the >0? It's a string...
     
  8. Offline

    TCO_007

    Sorry, I have no idea what I was thinking XD

    Ive never dealt with multiple args in a command because I am also fairly new to coding but learning a lot from you guys. Excuse me for my dumb mistakes XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
  9. Offline

    Azubuso

    TCO_007 Use a StringBuilder or StringBuffer to mend together those args as one string, heres an example

    PHP:
    StringBuilder sb = new StringBuilder("");
    for (
    int i xargs.length(); i++) { // x being where your message starts in args[]
        
    sb.append(args[i]).append(" ");
    }
     
    playerIssuingCommand.sendMessage("Me -> " targetPlayer.getName() + ": " sb.toString());
     
  10. Offline

    itzrobotix

    Or; String message = StringUtil.join(args, ' ', 0, args.length);
    join(Object[] listofstuff, char seperator, int startIndex, int endIndex);
     
  11. Offline

    Azubuso

    itzrobotix likes this.
  12. Offline

    itzrobotix

    True, just my way consumes less space. :p
     
Thread Status:
Not open for further replies.

Share This Page