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

Solved Clear mobs on shut down

Discussion in 'Development' started by minijaham, Jun 14, 2021.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Hey! I've been trying to close entities on server shut down. But I think it was getting called too late, and it wasn't working.

    Here's my attempt:

    PHP:
    public function removeMobs()
        {
            
    $i 0;
            foreach(
    $this->getServer()->getLevels() as $level) {
                foreach(
    $level->getEntities() as $entity) {
                    if(
    $entity instanceof Animal || $entity instanceof Monster) {
                        
    $entity->close();
                        
    $i++;
                    }
                }
            }
        }

    public function 
    onDisable() : void
        
    {
            
    $this->removeMobs();
        }

    I also tried it onEnable but doesn't work as well..
     
  2. Axon

    Axon Zombie

    Messages:
    276
    Correct me If I'm wrong, but I think the chunk must be active to detect or remove a mob?
    And pocketmine kicks all the players before calling the onDisable() function.
     
    Agent and minijaham like this.
  3. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Is that so?
    Hmm...do you have any alternatives to do this? :/
     
  4. Axon

    Axon Zombie

    Messages:
    276
    Hmm, You could implement your own stop command.
    First make it clear all the entities, then shuts the server down.
    I use this to unregister commands:
    PHP:
    public function unregister(string ...$commands){
          
    $map Server::getInstance()->getCommandMap();
          foreach (
    $commands as $cmd) {
              
    $command $map->getCommand($cmd);
              if (
    $command !== null) {
                  
    $command->setLabel("old_".$cmd);
                  
    $map->unregister($command);
              }
          }
        }
     
    Agent and minijaham like this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    This is actually a great idea. Thank you!
     
    Axon likes this.
  6. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Actually another bit of a problem.

    What if the server is forcefully shut down?
     
  7. Axon

    Axon Zombie

    Messages:
    276
    Hmm, I thought maybe we can control the forcefully shutdown, but I don't think we can.
     
    Agent likes this.
  8. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Yeah... :/

    Perhaps having the chunks loaded is possible?
     
  9. Axon

    Axon Zombie

    Messages:
    276
    That would be possible but wouldn't it affect the performance? We wouldn't know which chunk to load.
    Unless we store that data in memory when onPlayerQuit is called.

    and I just hit 123 messages :)
     
    Agent likes this.
  10. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Yeah that's what I was worried about. Hmm...I'll go sit in a corner and think about methods on how to do this. Please reply if you come up with a good way to do it! Thank you for your help Erik! Appreciate it.
     
  11. Axon

    Axon Zombie

    Messages:
    276
    Is it possible to detect if a server is going to forcefully crash?
     
    Agent likes this.
  12. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Not sure, but there is a way to know if the server shut downs

    PHP:
    public function onDisable(){
       if(!
    $this->getServer()->isRunning()){
            
    // code
       
    {
    }
     
  13. Axon

    Axon Zombie

    Messages:
    276
    This detects if /stop is used. Or proper shutting down.
    I don't think it's possible to do something when it's going to forcefully crash. Since all threads will just cancel without any warning.
     
    Agent and minijaham like this.
  14. Axon

    Axon Zombie

    Messages:
    276
    Maybe we can clear the entity onEnable?
     
    Agent and minijaham like this.
  15. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    I've tried it. Same issue with onDisable.
    Since the chunks aren't loaded, the entities won't clear
     
  16. Axon

    Axon Zombie

    Messages:
    276
    Maybe we run it after a first player joins?
     
    Agent likes this.
  17. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    How should that work?
     
  18. Axon

    Axon Zombie

    Messages:
    276
    We'll declare a private variable $IsFirstJoined = false;
    When a player joins, If $IsFirstJoined is setted to false then clear the entities, then set isFirstJoined to true.
     
    Agent likes this.
  19. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Great idea. But that still wouldn't solve the issue where not all mobs get cleared(foreaching all worlds)...
     
  20. Axon

    Axon Zombie

    Messages:
    276
    Are entities saved as a nbt data on the world?
    If so, then we can just remove the entities data as a whole.
     
    Last edited: Jun 14, 2021
    Agent likes 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.