Help?

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

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

    Xtremebrawler

    I am trying to make a message command for my plugin, and this is what I have so far:

    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. }
    18.  
    19. }
    20.  


    It works fine, except it only sends the first word of the message. Someone help please!
     
  2. Offline

    DJSkepter

    Xtremebrawler
    It depends what 'messageSent' is.
    Code:java
    1. public static String[] getMsgFromArgs(String[] args, int indexBegin, int indexEnd) {
    2. List<String> list = new ArrayList<String>();
    3. for (int i = indexBegin; i < indexEnd; i++) {
    4. list.add(args[i]);
    5. }
    6. return (String[]) list.toArray();
    7. }[/i]

    Use something along the lines of:
    Code:
    getMsgFromArgs(args, 1, args.length);
    to get the message as a String[]. You can then use Apache's StringUtils like this:
    Code:
    StringUtils.join(getMsgFromArgs(args, 1, args.length), " ");
    to turn it into a String.
     
  3. Offline

    AdamQpzm

    DJSkepter Wonderful method you have there to convert a String[] to a String[] :)
     
    thecrystalflame likes this.
  4. Offline

    thecrystalflame


    he made a typo, it will actually turn the String[] into a String.
     
  5. Offline

    AdamQpzm

    thecrystalflame I meant this method here
    Code:
    public static String[] getMsgFromArgs(String[] args, int indexBegin, int indexEnd) {
            List<String> list = new ArrayList<String>();
            for (int i = indexBegin; i < indexEnd; i++) {
                list.add(args[i]);
            }
            return (String[]) list.toArray();
        }
    Not the StingUtils method
     
    ZodiacTheories likes this.
Thread Status:
Not open for further replies.

Share This Page