Inheritance: extends ChunkEvent, implements pocketmine\event\Cancellable
 public function onUnloadChunk(ChunkUnloadEvent $event)
 {
     if ($event->getLevel()->getName() !== "world_pvp") {
         return;
     }
     $chunk = $event->getChunk();
     $hash = Level::chunkHash($chunk->getX(), $chunk->getZ());
     if (isset($this->modelLocs[$hash])) {
         foreach ($this->modelLocs[$hash] as $id => $model) {
             if (isset($this->models[$id])) {
                 $this->models[$id]->_kill();
                 unset($this->models[$id]);
             }
         }
     }
     if (isset($this->shopLocs[$hash])) {
         foreach ($this->shopLocs[$hash] as $col => $loc) {
             if (isset($this->shops[$col])) {
                 $this->shops[$col]->_kill();
                 unset($this->shops[$col]);
             }
         }
     }
     if ($hash === $this->bowHash) {
         $this->bow->_kill();
         $this->bow = null;
     }
 }
 public function onUnload(ChunkUnloadEvent $event)
 {
     if ($event->getLevel() === $this->level) {
         foreach ($this->locs[Level::chunkHash($event->getChunk()->getX(), $event->getChunk()->getZ())] as $id => $spawn) {
             if (isset($this->spawns[$spawn->getId()])) {
                 $this->level->removeEntity($this->spawns[$spawn->getId()]);
             }
         }
     }
 }
Example #3
0
 public function unloadChunk($x, $z, $safe = true)
 {
     if ($safe === true and $this->isChunkInUse($x, $z)) {
         return false;
     }
     $this->timings->doChunkUnload->startTiming();
     $index = PHP_INT_SIZE === 8 ? ($x & 4294967295.0) << 32 | $z & 4294967295.0 : $x . ":" . $z;
     $chunk = $this->getChunk($x, $z);
     if ($chunk !== null) {
         $this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($chunk));
         if ($ev->isCancelled()) {
             return false;
         }
     }
     try {
         if ($chunk !== null and $this->getAutoSave()) {
             $this->provider->setChunk($x, $z, $chunk);
             $this->provider->saveChunk($x, $z);
         }
         $this->provider->unloadChunk($x, $z, $safe);
     } catch (\Exception $e) {
         $logger = $this->server->getLogger();
         $logger->error("Error when unloading a chunk: " . $e->getMessage());
         if ($logger instanceof MainLogger) {
             $logger->logException($e);
         }
     }
     unset($this->chunks[$index]);
     unset($this->usedChunks[$index]);
     unset($this->chunkTickList[$index]);
     Cache::remove("world:" . $this->getId() . ":{$index}");
     $this->timings->doChunkUnload->stopTiming();
     return true;
 }
Example #4
0
 public function unloadChunk($x, $z, $safe = true, $trySave = true)
 {
     if ($safe === true and $this->isChunkInUse($x, $z)) {
         return false;
     }
     if (!$this->isChunkLoaded($x, $z)) {
         return true;
     }
     $this->timings->doChunkUnload->startTiming();
     $index = Level::chunkHash($x, $z);
     $chunk = $this->getChunk($x, $z);
     if ($chunk !== null and $chunk->getProvider() !== null) {
         $this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($chunk));
         if ($ev->isCancelled()) {
             $this->timings->doChunkUnload->stopTiming();
             return false;
         }
     }
     try {
         if ($chunk !== null) {
             if ($trySave and $this->getAutoSave()) {
                 $entities = 0;
                 foreach ($chunk->getEntities() as $e) {
                     if ($e instanceof Player) {
                         continue;
                     }
                     ++$entities;
                 }
                 if ($chunk->hasChanged() or count($chunk->getTiles()) > 0 or $entities > 0) {
                     $this->provider->setChunk($x, $z, $chunk);
                     $this->provider->saveChunk($x, $z);
                 }
             }
             foreach ($this->getChunkLoaders($x, $z) as $loader) {
                 $loader->onChunkUnloaded($chunk);
             }
         }
         $this->provider->unloadChunk($x, $z, $safe);
     } catch (\Exception $e) {
         $logger = $this->server->getLogger();
         $logger->error($this->server->getLanguage()->translateString("pocketmine.level.chunkUnloadError", [$e->getMessage()]));
         if ($logger instanceof MainLogger) {
             $logger->logException($e);
         }
     }
     unset($this->chunks[$index]);
     unset($this->chunkTickList[$index]);
     unset($this->chunkCache[$index]);
     $this->timings->doChunkUnload->stopTiming();
     return true;
 }
Example #5
0
 public function unloadChunk($x, $z, $safe = true)
 {
     if ($safe === true and $this->isChunkInUse($x, $z)) {
         return false;
     }
     $index = "{$x}:{$z}";
     $chunk = $this->getChunkAt($x, $z);
     if ($chunk instanceof FullChunk) {
         $this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($chunk));
         if ($ev->isCancelled()) {
             return false;
         }
     }
     $this->timings->doChunkUnload->startTiming();
     if ($this->getAutoSave()) {
         if (isset($this->chunks[$index])) {
             $this->provider->setChunk($x, $z, $this->chunks[$index]);
         }
         $this->provider->saveChunk($x, $z);
     }
     unset($this->chunks[$index]);
     $this->provider->unloadChunk($x, $z, $safe);
     unset($this->usedChunks[$index]);
     Cache::remove("world:" . $this->getID() . ":{$x}:{$z}");
     $this->timings->doChunkUnload->stopTiming();
     return true;
 }