All args except 0

Discussion in 'Plugin Development' started by Rufus5, Aug 5, 2014.

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

    Rufus5

    Ok, so I'm messing around with arguments and stuff, and I wanted to make a broadcast plugin. Desired effect:
    /tellall Hey guys! -> [Server]Hey guys!

    Now, in the code, my String "message" has to be all the arguments except 0. How can I fix this?

    Code:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Broadcast extends JavaPlugin {
     
     
    public boolean onCommand(CommandSender sender, Command command,
    String label, String[] args){
    if(label.equalsIgnoreCase("tellall")){
    Player player = (Player) sender;
    if(player.hasPermission(Broadcast.tell)){
    if(args.length == 0){
    player.sendMessage(ChatColor.RED + "Usage: /tellall <message>");
    }else{
    String message = args[1];
    Bukkit.broadcastMessage(message);
    }
     
     
    }
     
    return true;
    }
    return false;
    }
     
    }
     
  2. Offline

    Gater12

    Rufus5
    Loop through all args. Append them and a space to a StringBuilder. Invoke toString followed by trim (to get rid of the last space) to StringBuilder and broadcast that.
     
  3. Offline

    Skye

    Rufus5 Use a for statement to loop through the arguments, starting from the desired index, and append to a StringBuilder.
     
  4. Offline

    Flamedek

    Rufus5 For your example you can actually just use all arguments.. As 'tellall' is the command(label) and the args starts with 'Hey' (so args[0] = Hey)
    To get the args into a nice message follow Gater12
     
  5. Offline

    Dragonphase

    Rufus5

    Like Flamedek said, args do not include the command label.

    Code:java
    1. String message = "[Server] " + StringUtils.join(args, " ");
    2.  
    3. Bukkit.broadcastMessage(message);


    Your permissions check also needs to be a string - "Broadcast.tellall"
     
  6. Offline

    Rufus5

    Thanks guys! Forgot about the whole label not argument thing.
     
Thread Status:
Not open for further replies.

Share This Page