1

I am making a simple /heal Bukkit plugin for CraftBukkit Beta Build 1.7.2 R0.3. (I'm new to java.) I am programming in Eclipse. I'm getting an error that says:

enter image description here

Here is my code:

public boolean onCommand_heal(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("heal")){
        if(args.length == 0){
        player.setHealth(20);
        player.setFireTicks(0);
        player.sendMessage(ChatColor.GOLD + "You are healed!"); 
        }else if(args.length == 1){
            if(player.getServer().getPlayer(args[0]) !=null){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                targetPlayer.setHealth(20);
                player.sendMessage(ChatColor.GOLD + "Player Healed!");
            }else{
                player.sendMessage(ChatColor.RED + "PLAYER NOT ONLINE!");
            }
        }
    }
    return false;
}

1 Answer 1

2

Try doing this:

Player targetPlayer = Bukkit.getServer().getPlayer(args[0]);
//or this:
Player targetPlayer = plugin.getServer().getPlayer(args[0]);

instead of

Player targetPlayer = player.getServer().getPlayer(args[0]);

and the same thing here, replace:

if(player.getServer().getPlayer(args[0]) !=null){

with:

if(Bukkit.getServer().getPlayer(args[0]) != null){

Also, you should always use floats when setting health since a recent Bukkit update. setHealth(int) is deprecated, and replaced with setHealth(float), so try:

targetPlayer.setHealth(20.0f);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It was the float issue
Your comment about replacing player.getServer() with Bukkit.getServer() is irrelevant and actually can be frowned upon depending on the coding style. @mas0701 make sure you check that commandsender instanceof Player before casting, as right now if you run that command from the console you'll get a nasty error.
@Tips48 I really see no reason to frown upon Bukkit.getServer()as opposed to player.getServer(), and plugin.getServer()... The only difference is that Bukkit.getServer() is static, and adding onto your comment about checking if sender instanceof Player, you might also want to make an else statement that would send the sender a message like Only players can heal themselves so they know why nothing is happening
I agree with your second point @jojodmo, and I was just saying that because I know there were points where they were considering removing Bukkit. Granted, I haven' used Bukkit in over a year, but you get the point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.