Arguments

Discussion in 'Plugin Development' started by DaanSander, Mar 2, 2015.

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

    DaanSander

    Hello i am trying to make a /heal command and when i do /heal it would heal me and if i do /heal <PlayerName> it would heal him but when i try /heal it gives me a internal error and when i do /heal <MyUsername> it heals me and sends the debug message for /heal

    code:
    Code:
    package me.daansander.hmcessentials.cmds;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    /**
    * Created by Daan on 2-3-2015.
    */
    public class Heal implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("heal") && sender instanceof Player) {
                Player p = (Player) sender;
                Player t = (Player) Bukkit.getPlayerExact(args[0]);
                if(args.length == 1) {
                    p.setHealth(20);
                    p.setFoodLevel(20);
                    p.sendMessage("test1 healed");
                    return true;
                }
                if(args.length == 2) {
                    if(t == null) {
                        p.sendMessage("§4§lCould not find player " + t.getName());
                        t.setHealth(20);
                        t.setFoodLevel(20);
                        p.sendMessage("test2 healed");
                        return true;
                    }
    
                }
            }
            return true;
        }
    }
    
    sorry for bad english
     
  2. Offline

    greyhunter99

    First with this line: Player t =(Player) Bukkit.getPlayerExact(args[0]); you are getting the first arg, but there could be no first arg since you first get it and then check whether it is there. So put that line after you checked there is a second player, to avoid the .NullpointerException.

    Also the "if(t == null)" line indicates that there is no player t. And when there is no player t you set the health of player t to 20. So change that as well.

    Also change your (length.args == 1) to (length.args== 0), as well as change the other one to (length.args == 1).
     
  3. Offline

    Zombie_Striker

    Why is there be two args? If you want just the player name, then all you need is one. Also, you should put an if Statement around your t.
     
Thread Status:
Not open for further replies.

Share This Page