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

$entity->getLevel() returning null

Discussion in 'Development' started by Az928, Sep 30, 2017.

  1. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Hi, recently, I was making an advanced lag cleaner, but on my entity engine, it got error where "$entity->getLevel() returning null"
    Here is my code,
    any help is appreciated
    PHP:
    <?php
    namespace LagCleaner\engines;

    use 
    pocketmine\Player;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\entity\Item;

    use 
    pocketmine\level\Level;

    use 
    pocketmine\scheduler\PluginTask;

    use 
    LagCleaner\Processor;

    class 
    EntityEngine extends PluginTask{
       
        private 
    $entityData = [];
       
        public function 
    __construct(Processor $handler){
           
    parent::__construct($handler);
        }
       
        public function 
    onRun(Int $tick){
           
    $handler $this->getOwner();
           foreach(
    $handler->getServer()->getLevels() as $level){
             foreach(
    $level->getEntities() as $entity){
                if(!
    $entity instanceof Player && !$this->canBeSkipped($entity)){
                   if(
    $entity instanceof Item && $handler->getData("entity.count.item")){
                     
    $this->checkEntity($entity);
                   }elseif(!
    $entity instanceof Item && $handler->getData("entity.count.item") == false){
                      
    $this->checkEntity($entity);
                     }
                 }
             }
          }
          foreach(
    $handler->getServer()->getLevels() as $level){
             if(
    $this->getTotalEntityCount($level) > $handler->getData("entity.max.count"50)){
                foreach(
    $level->getEntities() as $entity){
                   if(!
    $entity instanceof Player){
                      if(
    $entity instanceof Item && $handler->getData("entity.count.item")){
                        
    $entity->close();
                        
    $handler->getLogger()->debug("§f[EntityEngine] §7Cleared entity with ID ".$entity->getId()." due to max entity count reached for world: ".$entity->getLevel()->getName()); // Logs "getName() on null on .....
                      
    }elseif(!$entity instanceof Item && $handler->getData("entity.count.item") == false && !$this->canBeSkipped($entity)){
                        
    $entity->close();
                        
    $handler->getLogger()->debug("§f[EntityEngine] §7Cleared entity with ID ".$entity->getId()." due to max entity count reached for world: ".$level->getName()); // This hack worked, but eh, $entity->getLevel() should work
                     
    }
                   }
                }
              }
          }
        }
       
        public function 
    checkEntity(Entity $entity){
           
    $r $this->getOwner()->getData("entity.clean.range"50);
           
    $time $this->getOwner()->getData("entity.clean.time"50);
           
    $canLive false;
           foreach(
    $entity->getLevel()->getNearbyEntities($entity->getBoundingBox()->grow($r$r$r)) as $ent){
             if(
    $ent instanceof Player){
                if(isset(
    $this->entityData[$entity->getId()])){
                  unset(
    $this->entityData[$entity->getId()]);
                 }
                
    $canLive true;
              break;
              }
           }
           if(!
    $canLive){
               if(!isset(
    $this->entityData[$entity->getId()])){
                  
    $this->entityData[$entity->getId()] = 0;
                }
              
    $this->entityData[$entity->getId()]++;
              if(
    $this->entityData[$entity->getId()] >= $time){
                 unset(
    $this->entityData[$entity->getId()]);
                 
    $entity->close();
                 
    $this->getOwner()->getLogger()->debug("§f[EntityEngine] §7Cleared entity with ID ".$entity->getId()." due to no player nearby");
                }
            }
        }
       
        public function 
    canBeSkipped(Entity $entity){
            
    $skips $this->getOwner()->getData("entity.clean.skip", []);
            
    $ref = new \ReflectionClass($entity);
            foreach(
    $skips as $name){
              if(
    $ref->getShortName() == $name){
                 return 
    true;
              }
          }
        return 
    false;
        }
       
        public function 
    getTotalEntityCount(Level $level){
           
    $count 0;
           foreach(
    $level->getEntities() as $entity){
             if(!
    $entity instanceof Player){
                
    $count++;
              }
          }
        return 
    $count;
        }
    }
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    That means that the entity is either dead, or closed.
     
    Kyd likes this.
  3. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    That entity is floating in front of me bruh
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    PHP:
    $entity->close();
                        
    $handler->getLogger()->debug("§f[EntityEngine] §7Cleared entity with ID ".$entity->getId()." due to max entity count reached for world: ".$entity->getLevel()->getName()); // Logs "getName() on null on .....
    PHP:
    $entity->close();
    PHP:
     $entity->getLevel()
     
    jasonwynn10 likes this.
  5. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Oh Lord, my bad ;-;
     
  6. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Please let me know if correcting that mistake doesn't fix it because I've been having a similar problem with PureEntitiesX and had to add extra null checks everywhere on getLevel(), even though isClosed() was just checked.
     
    jasonwynn10 likes this.
  7. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    An entity's level is set to null when close() is called. As the documentation warns, entities are unusable after close() is called.
     
    jasonwynn10 and Legoboy0215 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.