user3421844
user3421844

Reputation: 1

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

I have this problem that I am trying to get the killers health from the PlayerDeathEvent but it gives me the error that the method getHealth() is ambiguous for the type Player

Here is a piece of the code.

@EventHandler
public void onDeath(PlayerDeathEvent event) {
    Player p = event.getEntity();
    Player killer = p.getKiller();
    double playerHealth = killer.getHealth();
}

Anyone got any idea why it's not working?

Upvotes: 0

Views: 1842

Answers (3)

clienthax
clienthax

Reputation: 1212

casting the player to Damagable will also work if you need nms code

Upvotes: 0

Jojodmo
Jojodmo

Reputation: 23616

The reason that you're getting an error could be because, lets say a creeper or another non-player entity kills the player. Here's what you should do to prevent this:

@EventHandler
public void onDeath(PlayerDeathEvent e){
    Player p = e.getEntity().getPlayer(); //use .getPlayer() just to be safe
    if(p.getLastDamageCause().equals(DamageCause.ENTITY_ATTACK) && p.getKiller() instanceof Player){
        //the player was last damaged by a LivingEntity, and the killer of the player is in-fact a player
        Player killer = p.getKiller();
        double playerHealth = killer.getHealth();
    }
}

Upvotes: 0

kabb
kabb

Reputation: 2502

There are two getHealth() methods, due to the way Bukkit handled Minecraft changing the way entity health is stored in 1.6. You can read more about this here.

If you aren't using any NMS code, you should use the bukkit.jar in your build path as opposed to craftbukkit.jar. This should resolve your issue easily enough.

If you do need NMS code, you need to have both bukkit.jar AND craftbukkit.jar in your build path. Furthermore, you have to have bukkit.jar above craftbukkit.jar in the build path for it to work.

Upvotes: 4

Related Questions