Solved Kill Mobs

Discussion in 'Development' started by yBriisMC, Jul 3, 2022.

  1. yBriisMC

    yBriisMC Silverfish

    Messages:
    17
    I'm trying to make a simple system of when to kill the entity and earn MobCoins
    But I made a code and it didn't catch can you help me and see what's wrong? It doesn't give any error on the console, it just doesn't catch

    NO CONSOLE ERROR. IT JUST DOESN'T CATCH

    PHP:
    <?php

    namespace MasterFeio\Events;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\entity\EntityDeathEvent;
    use 
    pocketmine\event\entity\{EntityDamageEventEntityDamageByEntityEvent};
    use 
    MasterFeio\Main;

    class 
    MobCoins implements Listener {
        private 
    $owner;
        public function 
    __construct(Main $owner){
            
    $this->owner $owner;
          
    $this->mobc $this->owner->getServer()->getPluginManager()->getPlugin("MobCoins");
        }
        public function 
    onDeath(EntityDeathEvent $e){
            
    $p $e->getEntity();
            
            if(
    $p->getLastDamageCause() instanceof EntityDamageByEntityEvent){
            
    $killer $p->getLastDamageCause();
            if(
    $killer instanceof Player){
                
    $killer->sendMessage("test");
               
    $this->mobc->addmobc($killer1);
            }
        }
    }
    }
     
  2. IvanCraft623

    IvanCraft623 Baby Zombie

    Messages:
    102
    GitHub:
    IvanCraft623
    Are u registering the listener?
     
  3. yBriisMC

    yBriisMC Silverfish

    Messages:
    17
    it is working
    With getEntity it doesn't get it, with getPlayer it gets it normally
     
  4. TacoError

    TacoError Spider

    Messages:
    7
    GitHub:
    TacoError
    It would be a lot easier to handle this inside of the Entity's class itself. If you are implementing the entity ofcourse... Otherwise you could listen for EntityDamageByEntityEvent and make sure the event->getEntity() is not an instance of a player and check if the difference between the entities health and the events final damage is less than 0.1. Than just run the code because the entity is dead.

    Psuedocode:
    PHP:
    public function onDamage(EntityDamageByEntityEvent $event) {
         
    $damager $event->getDamager();
         if (
    $damager instanceof Player) {
              
    $entity $event->getEntity();
              if (!
    $entity instanceof Player) {
                   if ((
    $entity->getHealth() - $event->getFinalDamage()) < 0.1) {
                        
    // Give MobCoins Here to $damager
                   
    }
              }
         }
    }
     
  5. TacoError

    TacoError Spider

    Messages:
    7
    GitHub:
    TacoError
    I think this code might actually work if you just paste it in lol... Haven't done PHP or Pocketmine in awhile tho...
     

Share This Page

  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.