1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

HealthTip

Discussion in 'Development' started by abimalek, Nov 18, 2017.

  1. abimalek

    abimalek Witch

    Messages:
    74
    GitHub:
    megagastpvp
    I tried making a plugin where you hit someone it shows their health but for some reason it doesn't this is the code
    PHP:
    namespace HealthTip;
    use 
    pocketmine\plugin\PluginBase;
    use 
    poceketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityEvent;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\entity\Living;
    use 
    pocketmine\Player;
    use 
    pocketmine\entity\Monster;
    class 
    Main extends PluginBase implements Listener{
        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onDamage(EntityDamageEvent $e){
            
    $en $e->getEntiy();
            
    $d $e->getDamager();
            if(
    $d instanceof Player){
                if(
    $en instanceof Player){
                    
    $d->sendTip(c::GOLD $en->getName() . "'s" c::RESET c::DARK_RED "Health is" $en->getHealth());
                }
                if(
    $en instanceof Monster){
                    
    $d->sendTip(c::DARK_RED "Monsters health is" $en->getHealth());
                }
            }
        }
    }
     
  2. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    For one, it should be:
    PHP:
    $en $e->getEntity();
    For two, not all damage events are caused by other entities, so $d = $e->getDamager() would cause an error.


    The easiest fix for this is to use pocketmine\event\entity\EntityDamageByEntityEvent and then test if $e is an instance of EntityDamageByEntityEvent, like so:

    PHP:
    public function onDamage(EntityDamageEvent $e) {
        
    $en $e->getEntity();
        if(
    $e instanceof EntityDamageByEntityEvent) {
            
    $d $e->getDamager();
            if(
    $d instanceof Player) {
                
    /*
                P.S. You missed a space after "is" on both strings and a space after "'s",
                so if it worked it would've looked like this for a player: RandomPlayer123'sHealth is14.5(random health value)
                and like this for a monster: Monsters health is14.5(again, random health value)
                */
                
    if($en instanceof Player) {
                    
    $d->sendTip(c::GOLD $en->getName() . "'s" c::RESET c::DARK_RED "Health is" $en->getHealth());
                }
                if(
    $en instanceof Monster) {
                    
    $d->sendTip(c::DARK_RED "Monsters health is" $en->getHealth());
                }
            }
        }
    }

    Also, it should be "Monster's health" and not "Monsters health", as you're trying to show that the health is in possession of the monster, but this isn't an English class, so you do you. ;D

    My last note is that if you don't find that the sendTip function is satisfactory for you, there's always the sendPopup function! :)
     
    AlicanCopur and jasonwynn10 like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.