pocketmine\level\Level::setChunk PHP Method

setChunk() public method

public setChunk ( integer $chunkX, integer $chunkZ, pocketmine\level\format\FullChunk $chunk = null, boolean $unload = true )
$chunkX integer
$chunkZ integer
$chunk pocketmine\level\format\FullChunk
$unload boolean
    public function setChunk(int $chunkX, int $chunkZ, FullChunk $chunk = null, bool $unload = true)
    {
        if ($chunk === null) {
            return;
        }
        $index = Level::chunkHash($chunkX, $chunkZ);
        $oldChunk = $this->getChunk($chunkX, $chunkZ, false);
        if ($unload and $oldChunk !== null) {
            $this->unloadChunk($chunkX, $chunkZ, false, false);
            $this->provider->setChunk($chunkX, $chunkZ, $chunk);
            $this->chunks[$index] = $chunk;
        } else {
            $oldEntities = $oldChunk !== null ? $oldChunk->getEntities() : [];
            $oldTiles = $oldChunk !== null ? $oldChunk->getTiles() : [];
            $this->provider->setChunk($chunkX, $chunkZ, $chunk);
            $this->chunks[$index] = $chunk;
            foreach ($oldEntities as $entity) {
                $chunk->addEntity($entity);
                $entity->chunk = $chunk;
            }
            foreach ($oldTiles as $tile) {
                $chunk->addTile($tile);
                $tile->chunk = $chunk;
            }
        }
        unset($this->chunkCache[$index]);
        $chunk->setChanged();
        if (!$this->isChunkInUse($chunkX, $chunkZ)) {
            $this->unloadChunkRequest($chunkX, $chunkZ);
        } else {
            foreach ($this->getChunkLoaders($chunkX, $chunkZ) as $loader) {
                $loader->onChunkChanged($chunk);
            }
        }
    }

Usage Example

Example #1
0
 public function generateChunk($chunkX, $chunkZ)
 {
     $shape = $this->getShape($chunkX << 4, $chunkZ << 4);
     $chunk = $this->level->getChunk($chunkX, $chunkZ);
     $chunk->setGenerated();
     $c = Biome::getBiome(1)->getColor();
     $R = $c >> 16;
     $G = $c >> 8 & 0xff;
     $B = $c & 0xff;
     $bottomBlockId = $this->bottomBlock->getId();
     $bottomBlockMeta = $this->bottomBlock->getDamage();
     $plotFillBlockId = $this->plotFillBlock->getId();
     $plotFillBlockMeta = $this->plotFillBlock->getDamage();
     $plotFloorBlockId = $this->plotFloorBlock->getId();
     $plotFloorBlockMeta = $this->plotFloorBlock->getDamage();
     $roadBlockId = $this->roadBlock->getId();
     $roadBlockMeta = $this->roadBlock->getDamage();
     $wallBlockId = $this->wallBlock->getId();
     $wallBlockMeta = $this->wallBlock->getDamage();
     $groundHeight = $this->groundHeight;
     for ($Z = 0; $Z < 16; ++$Z) {
         for ($X = 0; $X < 16; ++$X) {
             $chunk->setBiomeId($X, $Z, 1);
             $chunk->setBiomeColor($X, $Z, $R, $G, $B);
             $chunk->setBlock($X, 0, $Z, $bottomBlockId, $bottomBlockMeta);
             for ($y = 1; $y < $groundHeight; ++$y) {
                 $chunk->setBlock($X, $y, $Z, $plotFillBlockId, $plotFillBlockMeta);
             }
             $type = $shape[$Z << 4 | $X];
             if ($type === self::PLOT) {
                 $chunk->setBlock($X, $groundHeight, $Z, $plotFloorBlockId, $plotFloorBlockMeta);
             } elseif ($type === self::ROAD) {
                 $chunk->setBlock($X, $groundHeight, $Z, $roadBlockId, $roadBlockMeta);
             } else {
                 $chunk->setBlock($X, $groundHeight, $Z, $roadBlockId, $roadBlockMeta);
                 $chunk->setBlock($X, $groundHeight + 1, $Z, $wallBlockId, $wallBlockMeta);
             }
         }
     }
     $chunk->setX($chunkX);
     $chunk->setZ($chunkZ);
     $this->level->setChunk($chunkX, $chunkZ, $chunk);
 }
Level