Getting and setting player health.

Discussion in 'Plugin Development' started by darknesschaos, Jan 17, 2011.

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

    darknesschaos

    so I'm trying to code a little silly plugin here and was wondering why this doesn't work"

    Code:
    health = event.getPlayer().getHealth();
    health = health / 2;
    event.getPlayer().setHealth(health);
    I realize this should work too but I just wanted to be sure.

    Code:
    health = event.getPlayer().getHealth();
    event.getPlayer().setHealth(health / 2);
    There are a few problems here, First, when I test to see what the health number for the player just joined, I get 0, when it is down one heart it says 20 and degrades by 2 for each heart after that. Another problem is that it will not set the player health, and when the player's health increases the get health number does not.

    Any ideas on how to fix this? Or is it an issue with bukkit?
     
  2. Offline

    DjDCH

    This is quite strange. In witch event do you use this code ?

    Also, try to cast the var health when it com from "event.getPlayer().getHealth();" because it possibly could a string.
     
  3. Offline

    darknesschaos

    the get PLAYER_COMMAND event, and in the docs it says that it is a string. not to mention when I wanted that to be the only thing that printed out, i got an error saying that it wanted to print a string not a var.
     
  4. Offline

    DjDCH

    I just read the doc. getHealth() return a int and setHealth() expect a int. So no need to cast here, just declare health as a int. Otherwise I cannot see where is the problem. Try to make more test, with an other event (like onEntityDamage) and print out the health before and after modification.
     
  5. Offline

    Snowl

    I would be also interested in how to fix this. I can't get setHealth(); to set the health ;(
     
  6. Offline

    Silence

    Because I don't have a proper mapping, the following is hypothetical and a shot in the wild:
    In EntityLiving the field 'ba' is set, when health is updated through setHealth() function.
    However, the packet 8 (update health) is send with the value that is put in field az (ba and az are definied in EntityLiving and EntityLiving ends up beeing inherited by EntityPlayerMP, which then sends the packet 8).

    Edit: This was fixed in latest build, it was 'ba' <-> 'az'
     
Thread Status:
Not open for further replies.

Share This Page