Making a player say something on Command

Discussion in 'Plugin Development' started by TaiSmoove, Jul 2, 2013.

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

    TaiSmoove

    Okay so I'm making this Troll plugin and I want a command like /sayme <Player> <Message>

    How would I make it so the Message will make the Target Player say it?
     
  2. Offline

    Forseth11

    TaiSmoove I think it is:
    Code:java
    1. player.chat("What You want them to say!");
     
  3. Offline

    TaiSmoove

    Forseth11
    I know that, but I want it like /sayme <Player> <Message>
    So the sender can decide on what he wants the player to say
     
  4. Offline

    Forseth11

    TaiSmoove Create a String and add agrs 1+ to the string putting a space between each arg put into String.
     
  5. Offline

    TaiSmoove

    Forseth11
    So like:
    Code:java
    1. if(args.length == 1){
    2. player.chat(args[0] + " " + args[1] + " ")
    3. }else if(args.length == 2){
    4. player.chat(args[1] + " " + args[2])
    5. }
     
  6. Offline

    zack6849

    No, use a stringbuilder.
    Code:
     Player target = getServer().getPlayer(args[0]);
     if(target != null && target.isOnline()){
         StringBuilder sb = new StringBuilder();
         for(int i = 2; i < args.length; i++){
             sb.append(args[i]).append(" ");
         }
         String message = sb.toString().trim();
         target.chat(message);
     }
    
     
    Eats_Rainbows likes this.
  7. Offline

    xXSilentYoshiXx

    lol, this isn't all that hard.

    Code:
    Code:
        @Override
        public boolean onCommand(CommandSender s, Command cmd, String cl, String[] args) {
            if (cmd.getName().equalsIgnoreCase("sayme")) {
                if (args.length < 2) {
                    s.sendMessage("Incorrect usage!");
                } else {
                    if (s.hasPermission("sayme.use")) {
                        Player targetPlayer = Bukkit.getServer().getPlayer(args[0]);
                        String msg;
                       
                        for (int x = 2; x < args.length; x++)
                            msg = msg + args[x] + (x + 1 < args.length ? " " : "");
                       
                        targetPlayer.chat(msg);
                    } else {
                        s.sendMessage("No permission!");
                    }
                }
            }
            return false;
        }
     
  8. Offline

    Jamplifier

Thread Status:
Not open for further replies.

Share This Page