Search code examples
javaminecraftbukkitambiguous

The method getHealth() is ambiguous for the type Player - Bukkit 1.6.4


This is a piece of my code for a plugin.

public boolean reapSoul(Player player, Player tplayer, double damage)
{
    if(player.hasPermission("myrace.wraith.reap"))
    {
        player.sendMessage(ChatColor.DARK_GRAY + "You have reaped " + tplayer.getName() + "'s soul.");
        damage = player.getLevel()/10;
        double heal = damage/2;
        tplayer.damage(damage);
        player.setHealth(player.getHealth() + heal);
    }
    return true;
}

I am aware there are probably a lot of errors, and I can deal with them on my own. The one that I cannot deal with is

player.setHealth(player.getHealth() + heal);

Eclipse (my IDE) tells me that the method getHealth() is ambiguous for the type Player.

I am aware that this is probably a really popular question since the release of Bukkit 1.6.x but none of the searches I did gave me a solution I could understand.

If this is indeed the solution could you please help me understand what I can do...

Thank-you.


Solution

  • Can you try double h = player.getHealth()? From what I understand there are two getHealth methods because of backwards compatibility. There is probably some reflection magic to figure out whether the new (type double) or the old (type int) is requested.

    Likewise setHealth has two versions so the compiler cannot figure out whether it should cast getHealth() + health to int and use setHealth(int) or whether it should castgetHealth() + health to double and use setHealth(double) because getHealth() is ambigious.

    @Yourcomment It is deprecated but still supported for backwards compatibility, at this point I am clueless as to why this would happen, seems bukkit has a complicated build procedure with its jars so something might have gone wrong there, however a solution I found on google suggests that casting the player to Damageable gets rid of the problem.

    Damageable d = (Damageable) player;