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..
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.
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); } } }
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
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.
Not sure, but there is a way to know if the server shut downs PHP: public function onDisable(){ if(!$this->getServer()->isRunning()){ // code {}
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.
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.
Great idea. But that still wouldn't solve the issue where not all mobs get cleared(foreaching all worlds)...
Are entities saved as a nbt data on the world? If so, then we can just remove the entities data as a whole.