Solved Help needed in getting part of the args when the command is run

Discussion in 'Plugin Development' started by lordbobby104, Dec 14, 2013.

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

    lordbobby104

    I am trying to create a /msg command but I can't seem to figure out how to get all the args BUT args[0]. So the command would be like /msg [Player] [msg] amd I want to get the msg. Thanks in advance for any helps.
     
  2. Offline

    Pizza371

    lordbobby104 msg would be args[1] but that wouldn't allow you to do messages with spaces :p

    edit: sorry I read that wrong.
     
  3. Offline

    The_Doctor_123

    Get the rest of the values in the array after args[0] and join them together with a space.
     
  4. Offline

    lordbobby104

    The_Doctor_123 So like this?
    Code:
                               StringBuilder sb = new StringBuilder();
                                for(int n=0;n<args.length;n++) {
                                    if(n > 1) {
                                        sb.append(" ");
                                    }
                                    sb.append(args[n]);
                                }
    Done with a little Google search :p
    The problem is this returns a StringBuilder and I need a String[]

    The_Doctor_123 Thats not even colse to right is it? Lol

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

    Pizza371

    lordbobby104
    Code:
    for(int i = 1; i <= args.length; i++) {
    StringBuilder sb = new StringBuilder();
    sb.append(args[I] + " ");[/I]
    [I]//to get string sb.toString();[/I]
    [I]//you could use str + str but using StringBuilder will increase performance by a fraction[/I]
    } 





    edit: wow.. one edit messed up my whole post -_-
    whoops :p just noticed you will need to take the StringBuilder object initialization away from the foor loop :p
     
  6. Offline

    the_merciless

    Untested:

    Code:
            if (cmd.getName().equalsIgnoreCase("test")){
             
                String string = "";
             
                for (int i = 1; i < args.length; i++){
                    string = string + args[i];
                }
             
            }
     
  7. Offline

    lordbobby104

    the_merciless Doesn't work D:
    Here is my code:
    Code:
            if(commandLabel.equalsIgnoreCase("msg")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                    if(player.hasPermission("bc.msg")) {
                        if(args.length == 0 || args.length == 1) {
                            Exceptions.notEnoughArgs(player);
                        } else {
                            Player target = Bukkit.getServer().getPlayer(args[0]);
                            if(target == null) {
                                player.sendMessage(ChatColor.RED + "That player isn't online!");
                            } else {
                                target.sendMessage(ChatColor.GRAY + "<" + player.getName() + " -> me> " + MessageMethods.getMsg(args));
                                player.sendMessage(ChatColor.GRAY + "<me -> " + target.getName() + "> " + MessageMethods.getMsg(args));
                                MessageMethods.setLastMessaged(player, target);
                            }
                        }
                    } else {
                        Exceptions.notEnoughPermission(player);
                    }
                }
                return true;
            }
    My MessageMethods:
    Code:
        private static HashMap<String, String> players = new HashMap<String, String>();
     
        public static String getMsg(String[] msg) {
            String string = "";
     
            for (int i = 0; i < msg.length; i++){
                    string = string + msg;
                }
            string.replaceFirst(msg[0], "");
            return string;
        }
       
        public static String getLastMessaged(Player p) {
            return players.get(p.getName());
        }
       
        public static void setLastMessaged(Player p, Player t) {
            if(players.containsKey(p.getName())) {
                players.remove(p.getName());
                players.put(p.getName(), t.getName());
            } else {
                players.put(p.getName(), t.getName());
            }
        }
       
        public static boolean hasMessaged(Player p) {
            return players.containsKey(p.getName());
        }
     
  8. Offline

    the_merciless

    hang on, my posts keep going wrong...
     
  9. Offline

    Pizza371

    lordbobby104 what doesn't work about it? is it because it doesnt have spaces?
     
  10. Offline

    the_merciless

    Sorry had problems with the [ i ], it kept removing it to make the text italic.

    Try this....

    Code:
    public static String getMsg(String[] msg) {
    String string = "";
     
    for (int i = 1; i < msg.length; i++){
    string = string + msg[i] + " ";
    }
    return string;
    }
     
  11. Offline

    Pizza371

    the_merciless that will include args[0]

    Code:java
    1. //psuedo code
    2.  
    3. public static String getMsg(String[] msg) {
    4. StringBuilder sb = new StringBuilder();
    5. for(int i = 1; i < msg.length; i++) sb.append(msg[i] + " ");
    6. return sb.toString();
    7. }[/i]

    i think this should work..
     
  12. Offline

    the_merciless

    edited again, sorry. I keep doing it wrong. I think yours should work too.
     
    Pizza371 likes this.
  13. Offline

    lordbobby104

Thread Status:
Not open for further replies.

Share This Page