Need help with checking args length

Discussion in 'Plugin Development' started by Rhino390, Mar 29, 2013.

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

    Rhino390

    I cant seem to get my plugin to alert the user when they use too many or too little arguments. Here is my code:

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Flame extends JavaPlugin {
    public void onEnable() {
    getLogger().info("Test plugin has been enabled!");
    }
    public void onDisable() {
    getLogger().info("Test plugin has been disabled!");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("ignite")){
    Player s = (Player)sender;
    Player target = s.getServer().getPlayer(args[0]);
    target.setFireTicks(10000);
    if(args.length < 1) {
    sender.sendMessage("Not enough args!");
    return false;
    }
    }
    return false;
    }
    }

    i've looked for the answer on other threads, but i cant seem to find anything. I'm very new to coding, so im still getting the hang of it. Any help would be greatly appreciated. thanks.
     
  2. Offline

    robbie260

    u should first test the length of args before u search the player. if args.length == 0 theres a nullpointerexception or a arrayindexoutofbounds exception
    Code:
    if(cmd.getName().equalsIgnoreCase("ignite"){
    if(args.length == 1){
    Player target = this.getServer().getPlayer(args[0]);
    target.setFireTicks(1000);
    }else{
    if(sender instanceof Player){
    Player player = (Player) sender;
    player.sendMessage("usage: /ignite <playername>");
    }else
    getLogger().info("usage: /ignite <playername>");
    }
    
     
Thread Status:
Not open for further replies.

Share This Page