pocketmine\level\format\mcregion\Chunk::fromBinary PHP Метод

fromBinary() публичный статический Метод

public static fromBinary ( string $data, pocketmine\level\format\LevelProvider $provider = null ) : Chunk
$data string
$provider pocketmine\level\format\LevelProvider
Результат Chunk
    public static function fromBinary($data, LevelProvider $provider = null)
    {
        $nbt = new NBT(NBT::BIG_ENDIAN);
        try {
            $nbt->readCompressed($data, ZLIB_ENCODING_DEFLATE);
            $chunk = $nbt->getData();
            if (!isset($chunk->Level) or !$chunk->Level instanceof CompoundTag) {
                return null;
            }
            return new Chunk($provider instanceof LevelProvider ? $provider : McRegion::class, $chunk->Level);
        } catch (Throwable $e) {
            return null;
        }
    }

Usage Example

Пример #1
0
 public function readChunk($x, $z, $generate = true, $forward = false)
 {
     $index = self::getChunkOffset($x, $z);
     if ($index < 0 or $index >= 4096) {
         //Regenerate chunk due to corruption
         $this->locationTable[$index][0] = 0;
         $this->locationTable[$index][1] = 1;
     }
     if (!$this->isChunkGenerated($index)) {
         if ($generate === true) {
             //Allocate space
             $this->locationTable[$index][0] = ++$this->lastSector;
             $this->locationTable[$index][1] = 1;
             fseek($this->filePointer, $this->locationTable[$index][0] << 12);
             fwrite($this->filePointer, str_pad(Binary::writeInt(-1) . chr(self::COMPRESSION_ZLIB), 4096, "", STR_PAD_RIGHT));
             $this->writeLocationIndex($index);
         } else {
             return false;
         }
     }
     fseek($this->filePointer, $this->locationTable[$index][0] << 12);
     $length = Binary::readInt(fread($this->filePointer, 4));
     $compression = ord(fgetc($this->filePointer));
     if ($length <= 0) {
         //Not yet generated
         $this->generateChunk($x, $z);
         fseek($this->filePointer, $this->locationTable[$index][0] << 12);
         $length = Binary::readInt(fread($this->filePointer, 4));
         $compression = ord(fgetc($this->filePointer));
     }
     if ($length > $this->locationTable[$index][1] << 12) {
         //Invalid chunk, bigger than defined number of sectors
         MainLogger::getLogger()->error("Corrupted bigger chunk detected");
         $this->locationTable[$index][1] = $length >> 12;
         $this->writeLocationIndex($index);
     } elseif ($compression !== self::COMPRESSION_ZLIB and $compression !== self::COMPRESSION_GZIP) {
         MainLogger::getLogger()->error("Invalid compression type");
         return false;
     }
     $chunk = Chunk::fromBinary(fread($this->filePointer, $length - 1), $this->levelProvider);
     if ($chunk instanceof Chunk) {
         return $chunk;
     } elseif ($forward === false) {
         MainLogger::getLogger()->error("Corrupted chunk detected");
         $this->generateChunk($x, $z);
         return $this->readChunk($x, $z, $generate, true);
     } else {
         return null;
     }
 }
All Usage Examples Of pocketmine\level\format\mcregion\Chunk::fromBinary