Solved Health Change (Event?)

Discussion in 'Plugin Development' started by deathknife, Oct 6, 2013.

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

    deathknife

    How would I go about event that gets called when player's health changes. Including damage, regen or set via another plugin. I have searched the javadocs but haven't found any events like that.
    If there's no event, would there be any way to call code when player's health changes?
     
  2. Offline

    MrSparkzz

    deathknife
    Regen - EntityRegainHealthEvent
    Damage - EntityDamageEvent

    For the last one, I'm not sure. I don't think there is a way, you'd probably have to create a custom event.
     
  3. Offline

    deathknife

    MrSparkzz
    Will those event get called with beacon/potions as well?
     
  4. Offline

    MrSparkzz

    deathknife
    With the beacon it will, potions... I'm not sure. But try it, if it doesn't work, you could always create an on right click event with the throwable potions an have the event set to cancelled. And for the drinkables, I'm not sure how you'd do that.
     
  5. Offline

    deathknife

    MrSparkzz
    Done some testing and it seems that beacon and potions do call regen event.
    Now I'm wondering, if there would be any way to detect change when plugin uses .setHealth();
     
  6. Offline

    MrSparkzz

    deathknife
    I'm not sure on how to do that, I mean the only way I could think of is to create a custom event that checks a players health every second or so, then if it isn't equal to what it was, set it back to what it was. But I'm sure that would cause a lot of lag. Good Luck!
     
  7. Offline

    deathknife

    MrSparkzz
    Alright. Thank you, that shouldn't be a big of a problem though. But I came across a problem with those events, when I get player's health in any of those events, it gets the health they had before it regened/damaged.
     
  8. Offline

    MrSparkzz

  9. Offline

    deathknife

    Code:java
    1. public class CookiesListener implements Listener {
    2.  
    3.  
    4.  
    5. @EventHandler
    6. public void onHeal (EntityRegainHealthEvent event) {
    7. if(!(event.getEntity() instanceof Player)) {
    8. return;
    9. }
    10. Player player = (Player) event.getEntity();
    11.  
    12. Damageable damag = player;
    13. double health = damag.getHealth();
    14. player.sendMessage("HP:" + health);
    15.  
    16. }
    17.  
    18. @EventHandler
    19. public void onDamage (EntityDamageEvent event) {
    20. if(!(event.getEntity() instanceof Player)) {
    21. return;
    22. }
    23.  
    24.  
    25. Player player = (Player) event.getEntity();
    26.  
    27. Damageable damag = player;
    28. double health = damag.getHealth();
    29. player.sendMessage("HP:" + health);
    30.  
    31. }
    32. }


    If I have 20 health, and jump off a cliff and take damage, it shows 20.0, but I do get damaged. For regen the same. For example, i have 10 hearts and regen 1 heart. It will say 10 instead of 11.

    MrSparkzz
     
  10. Offline

    MrSparkzz

    deathknife
    add this
    Code:java
    1.  
    2. event.setCancelled(); // you might need to put `true` in the parentesis
    3.  

    under this
    Code:java
    1.  
    2. if(!(event.getEntity() instanceof Player)) {
    3. return;
    4. }
    5.  

    that will stop the player from receiving damage or health. Also, you can make that instanceof if statement into a simple if statement, like this
    Code:java
    1.  
    2. if (!(event.getEntity() instanceof Player)) return;
    3.  
     
  11. Offline

    deathknife

    MrSparkzz
    I don't want to cancel that event, I want to get player's health (when they regen/dmg), but it gets the health they had before they regened/dmged.
    For example, if player has 20 health and gets damaged 1 HP, It returns his HP as 20 instead of 19.
     
  12. Offline

    MrSparkzz

    deathknife
    Ohhh, I thought you wanted it not to damage/heal you. Well hmm... this should work, but I'm not sure if it takes armor and potion effects into account.
    Code:java
    1.  
    2. Player player = (Player) event.getEntity();
    3.  
    4. double health = player.getHealth - event.getDamage();
    5.  
    6. player.sendMessage("HP:" + health);
    7.  
     
    deathknife likes this.
  13. Offline

    deathknife

    MrSparkzz
    That works great! used event.getAmount() for regen. All works perfectly now! Thank you!
     
  14. Offline

    MrSparkzz

    deathknife
    if all is well and done, please set this thread to solved.
     
    deathknife likes this.
Thread Status:
Not open for further replies.

Share This Page