How to get a players health

Discussion in 'Spigot Help' started by JifOnASpoon, Jul 9, 2021.

  1. In Intellij the text "getHealth" is crossed out and when I try to compile it it errors "java: reference to getHealth is ambiguous both method getHealth() in org.bukkit.entity.Damageable and method getHealth() in org.bukkit.entity.Damageable match". I don't know how to get the health of the player. If someone could show me how to save the double "health" as "player"'s health that would be helpful.

    Also, this program is supposed to make it so when you right-click with a mushroom stew it heals you 3.5 hearts (7 health points).

    Code (Java):
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class SoupEat implements Listener {
        public static void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            double health = player.getHealth();

            if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
                player.setHealth(health + 7);
  2. Just perform a cast:
    Code (Java):
    ((Damageable) player).getHealth();
    • Like Like x 1
  3. I’m guessing I have to import “org.bukkit.entity.Damageable”
  4. Yes I figured that you'd know that.
  5. Just double checking, but thanks for the help! :)
    • Friendly Friendly x 1