If I despawn entities, they don't despawn correctly :/ They only despawn for the player who joinen first. All other players see them. My code: PHP: foreach ($player->getLevel()->getEntities() as $entity) {if(!$entity instanceof Player) {$entity->close();// kill() makes no difference}}
Try a different way: PHP: foreach($player->level->getEntities() as $entity){if($entity instanceof Player) continue;$entity->close();}
Its something with the despawn. I tried this to kill all entity (also players). And the mobs don't despawn
I tested something around. It takes only Player(1) as $event->getPlayer(). Has someone an Idea how to fix that?
Schedule a delayed task to run 5+ seconds later after plugin startup. Then in the task run this: PHP: $level = $this->yourPluginBaseClass->getServer()->getLevelByName("level name here");foreach($level->getEntities() as $entity){ if(!($entity instanceof \pocketmine\entity\Human)){ $entity->close(); }}
why dont you just disable EntitySpawnEvent? its less lagg PHP: public function onEntitySpawn(EntitySpawnEvent $event){ if($event->getEntity() instanceof \pocketmine\Player) return; $event->setCancelled(); // you can check to disable at ceratin worlds too!}
But if he wants to run a piece of code that removes entities every 1 second it will reduce the performence, meanwhile you can run this code once to remove all entities and then disable the event
That is only if you run a repeating task, but a delayed task won't hurt. In fact repeating tasks have the best performance.