pocketmine\level\weather\Weather::calcWeather PHP Method

calcWeather() public method

public calcWeather ( $currentTick )
    public function calcWeather($currentTick)
    {
        if ($this->canCalculate()) {
            $tickDiff = $currentTick - $this->lastUpdate;
            $this->duration -= $tickDiff;
            if ($this->duration <= 0) {
                $duration = mt_rand(min($this->level->getServer()->weatherRandomDurationMin, $this->level->getServer()->weatherRandomDurationMax), max($this->level->getServer()->weatherRandomDurationMin, $this->level->getServer()->weatherRandomDurationMax));
                if ($this->weatherNow === self::SUNNY) {
                    $weather = $this->randomWeatherData[array_rand($this->randomWeatherData)];
                    $this->setWeather($weather, $duration);
                } else {
                    $weather = self::SUNNY;
                    $this->setWeather($weather, $duration);
                }
            }
            if ($this->weatherNow >= self::RAINY_THUNDER and $this->level->getServer()->lightningTime > 0 and is_int($this->duration / $this->level->getServer()->lightningTime)) {
                $players = $this->level->getPlayers();
                if (count($players) > 0) {
                    $p = $players[array_rand($players)];
                    $x = $p->x + mt_rand(-64, 64);
                    $z = $p->z + mt_rand(-64, 64);
                    $y = $this->level->getHighestBlockAt($x, $z);
                    $this->level->spawnLightning($this->temporalVector->setComponents($x, $y, $z));
                }
            }
        }
        $this->lastUpdate = $currentTick;
    }

Usage Example

Example #1
0
 /**
  * WARNING: Do not use this, it's only for internal use.
  * Changes to this function won't be recorded on the version.
  *
  * @param int $currentTick
  *
  * @return bool
  */
 public function doTick(int $currentTick)
 {
     $this->timings->doTick->startTiming();
     $this->checkTime();
     if (++$this->sendTimeTicker === 280) {
         $this->sendTime();
         $this->sendTimeTicker = 0;
     }
     $this->weather->calcWeather($currentTick);
     $this->unloadChunks();
     //Do block updates
     $this->timings->doTickPending->startTiming();
     if ($this->updateQueue->count() > 0 and $this->updateQueue->current()["priority"] <= $currentTick) {
         $block = $this->getBlock($this->updateQueue->extract()["data"]);
         unset($this->updateQueueIndex[Level::blockHash($block->x, $block->y, $block->z)]);
         $block->onUpdate(self::BLOCK_UPDATE_SCHEDULED);
     }
     $this->timings->doTickPending->stopTiming();
     $this->timings->entityTick->startTiming();
     //Update entities that need update
     Timings::$tickEntityTimer->startTiming();
     foreach ($this->updateEntities as $id => $entity) {
         if ($entity->closed or !$entity->onUpdate($currentTick)) {
             unset($this->updateEntities[$id]);
         }
     }
     Timings::$tickEntityTimer->stopTiming();
     $this->timings->entityTick->stopTiming();
     $this->timings->tileEntityTick->startTiming();
     Timings::$tickTileEntityTimer->startTiming();
     //Update tiles that need update
     if (count($this->updateTiles) > 0) {
         foreach ($this->updateTiles as $id => $tile) {
             if ($tile->onUpdate() !== true) {
                 unset($this->updateTiles[$id]);
             }
         }
     }
     Timings::$tickTileEntityTimer->stopTiming();
     $this->timings->tileEntityTick->stopTiming();
     $this->timings->doTickTiles->startTiming();
     if ($currentTick % 2 === 0) {
         $this->tickChunks();
     }
     $this->timings->doTickTiles->stopTiming();
     if (count($this->changedBlocks) > 0) {
         if (count($this->players) > 0) {
             foreach ($this->changedBlocks as $index => $blocks) {
                 unset($this->chunkCache[$index]);
                 Level::getXZ($index, $chunkX, $chunkZ);
                 if (count($blocks) > 512) {
                     $chunk = $this->getChunk($chunkX, $chunkZ);
                     foreach ($this->getChunkPlayers($chunkX, $chunkZ) as $p) {
                         $p->onChunkChanged($chunk);
                     }
                 } else {
                     $this->sendBlocks($this->getChunkPlayers($chunkX, $chunkZ), $blocks, UpdateBlockPacket::FLAG_ALL);
                 }
             }
         } else {
             $this->chunkCache = [];
         }
         $this->changedBlocks = [];
     }
     $this->processChunkRequest();
     if ($this->sleepTicks > 0 and --$this->sleepTicks <= 0) {
         $this->checkSleep();
     }
     foreach ($this->moveToSend as $index => $entry) {
         Level::getXZ($index, $chunkX, $chunkZ);
         foreach ($entry as $e) {
             $this->addChunkPacket($chunkX, $chunkZ, $e);
         }
     }
     $this->moveToSend = [];
     foreach ($this->motionToSend as $index => $entry) {
         Level::getXZ($index, $chunkX, $chunkZ);
         foreach ($entry as $entity) {
             $pk = new SetEntityMotionPacket();
             $pk->eid = $entity[0];
             $pk->motionX = $entity[1];
             $pk->motionY = $entity[2];
             $pk->motionZ = $entity[3];
             $this->addChunkPacket($chunkX, $chunkZ, $pk);
         }
     }
     $this->motionToSend = [];
     foreach ($this->chunkPackets as $index => $entries) {
         Level::getXZ($index, $chunkX, $chunkZ);
         $chunkPlayers = $this->getChunkPlayers($chunkX, $chunkZ);
         if (count($chunkPlayers) > 0) {
             foreach ($entries as $pk) {
                 Server::broadcastPacket($chunkPlayers, $pk);
             }
         }
     }
     $this->chunkPackets = [];
     $this->timings->doTick->stopTiming();
 }