Solved Problem with health in "if" statement

Discussion in 'Spigot Plugin Development' started by DoggyPlayss, Feb 2, 2020.

  1. Hello! I'm here with a problem where "if (player.getHealth() == 20)" it doesn't actually do what is written under, although else statement works... So what I should rewrite here for it to work?

    Code:

    Code (Java):
    package net.hexilion.Basics.Events;

    import static org.bukkit.ChatColor.*;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    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;
    import org.bukkit.inventory.EquipmentSlot;

    public class Interact implements Listener
    {
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            EquipmentSlot eslot = event.getHand();
            Action action = event.getAction();
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            if(eslot.equals(EquipmentSlot.HAND))
            {
                if (action.equals(Action.LEFT_CLICK_BLOCK) || action.equals(Action.RIGHT_CLICK_BLOCK))
                {
                    if (block.getType().equals(Material.LEAVES))
                    {
                        if (player.getHealth() == 20)
                        {
                            player.sendMessage(YELLOW + "" + BOLD + "  -  " + BLUE + "You are fully healed already!");//if my healt full it doesnt send me this message
                        }
                        else
                        {
                            player.setHealth(player.getHealth() + 1);
                            player.sendMessage(GREEN + "" + BOLD + "  :heavy_check_mark:  " + BLUE + "You have been healed (" + GREEN + BOLD + "+1" + BLUE + ") by clicking this block.");
                        }
                    }
                    else
                    {
                        player.sendMessage(GRAY + "       You haven't been healed by clicking " + WHITE + block.getType().toString() + GRAY + ".");
                    }
                }
            }
        }
    }
     
  2. I mean... you could do it like this for better compatibility,
    Code (Java):
    if (player.getHealth() == player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())
    however, that does not change the fact that your condition should trigger, and in fact does that for me.
    Therefore, I suppose that any other condition is not met (you either don't have max health or you are not clicking a leave e.t.c.)
     
    • Agree Agree x 1
  3. Thanks, it worked!
     
  4. The problem might be that health is not an exact value as hunger is.
     
    • Like Like x 1