Вопрос

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.

Это было полезно?

Решение

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;

Другие советы

Seems like there is more than one getHealth(). Java cannot figure out which you are referring to because they are both valid in the context that you are using it in.

If the first responders answer does not work you can also do...

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

I'm not certain, but I think you might need to explicitly cast to a type:

double playerHealth = (double)player.getHealth();
player.setHealth(playerHealth + heal);

I had the same problem, but I found a simple solution for it. Only you need to use some Reflections: You can get the health in double, but also in int:

Player j;   
int health_in_int=(int)j.getClass().getMethod("getHealth", double.class).invoke(j);
double health_in_double=(double)j.getClass().getMethod("getHealth", double.class).invoke(j);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top