Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

setHealth()

Comments in 'Plugin Development' started by deot, Nov 2, 2015.

  1. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    I wanted to set back full health when player join :)
    But.. as we all knew, setHealth() doesn't work.. Anybody have solution for this?

    My current code:
    PHP:
    public function onJoin(PlayerJoinEvent $e){
         
    $p $e->getPlayer();
         
    $p->setHealth(20);
    }
  2. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    So? heal()? What's the difference between heal and setHealth?
    Code:
    public function onJoin(PlayerJoinEvent $e){
         $p = $e->getPlayer();
         $p->heal(20);
    }
    If im not wrong, is it heal() adding the hearts with specified number, and setHealth() is setting the hearts to a specified number, right?
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    No, heal() and attack() is not used like that. Read the docs on the class Player.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/entity/Entity.php#L597
    PHP:
    public function heal($amountEntityRegainHealthEvent $source){
      
    $this->server->getPluginManager()->callEvent($source);
      if(
    $source->isCancelled()){
        return;
      }
      
    $this->setHealth($this->getHealth() + $source->getAmount());
    }
    There is no difference between $e->heal(20) and $e->setHealth($e->getHealth() + 20), except that heal() dispatches an EntityRegainHealthEvent for plugins.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    But setHealth() & heal() does not work :p
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It works...
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    setHealth????
  8. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power

Share This Page

Advertisement