Doing something when a players health is at a certain amount

Discussion in 'Plugin Development' started by Phishy, Apr 3, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Phishy

    The Title says it all, when a players health is a at certain amount, let's say half a heart, for example it would say something to the player, how would I go about getting the players health?

    Thanks.
     
  2. Offline

    SuperOriginal

    Player#getHealth()
     
  3. Offline

    Phishy

    if (player.getHealth() == (insert number)) {
    DoSomething

    }

    @SuperOriginal ?
     
  4. Offline

    SuperOriginal

    Yes,
    20.0 = 10 Hearts
    1.0 = Half a heart
    ect
     
    CodePlaysMinecraft likes this.
  5. @Phishy Make sure you aren't using CraftBukkit. #getHealth() is ambiguous in CraftBukkit.
     
  6. Wrong.
    Player has 20 half hearts.
    10.0 = 10 full hearts
    1.0 = 1 full heart
    0.5 = 1 half heart
    It is possible:
    Code:java
    1. ((Damageable)player).getHealth()
     
  7. Offline

    Phishy

    I'll try it out.

    Edit: I've tried that, here is the code

    if(((Damageable)player).getHealth() == 10.0) {
    event.setCancelled(true);

    }

    Doesn't seem to be working. Am I doing something wrong?
     
    Last edited: Apr 3, 2015
  8. That makes no sense what so ever.
    Must I draw you a picture? A player has 10 hearts (ingame). #getHealth and #setHealth returns a double. 1.0 is half a heart. 5.0 is 2 and a half hearts ingame. 10.0 is 5 hearts ingame. 20.0 is 10 hearts ingame. Please, check your math before you post.

    While you could do:
    Code:
    ((Damageable)player).getHealth();
    It is recommended you just use Bukkit instead.

    @Phishy Please post the full code. I'm not sure what event you're using.
     
    Last edited: Apr 3, 2015
  9. Offline

    Phishy

    @CodePlaysMinecraft is this right?

    if(player.getHealth() == 10.0) {
    event.setCancelled(true);

    }
     
  10. @Phishy
     
  11. Offline

    Phishy

    @EventHandler
    public void JesusPlayers(EntityDamageEvent event)
    {
    if(event.getEntity() instanceof Player){
    Player player = (Player) event.getEntity();
    if(Jesus.jesusplayers.contains(player.getName())) {
    if(player.getHealth() == 10.0) {
    event.setCancelled(true);

    }
    }
    }
    }
     
  12. Offline

    Konato_K

    @Phishy No, it's not because doubles are doubles, and floating point stuff it's weird as hell.

    In any case you want to directly compare you should cast it to integer, otherwise you need to remember that 10.0000001 is not 9.999999
     
  13. Offline

    Phishy

    So I should do...?
     
  14. Offline

    Konato_K

    @Phishy Cast it to integer or don't directly compare but use 'less than' and 'more than' comparision?
     
  15. Offline

    SuperOriginal

    Did you even bother checking to make sure you were correct?
     
    ferrybig and CodePlaysMinecraft like this.
Thread Status:
Not open for further replies.

Share This Page