Пример #1
0
 private function writeChunk(Chunk $chunk)
 {
     $binary = $chunk->toBinary(true);
     $index = LevelDB::chunkIndex($chunk->getX(), $chunk->getZ());
     $this->db->put($index . self::ENTRY_TERRAIN, substr($binary, 8, -1));
     $this->db->put($index . self::ENTRY_FLAGS, substr($binary, -1));
     $this->db->put($index . self::ENTRY_VERSION, "");
 }
Пример #2
0
 private function writeChunk(Chunk $chunk)
 {
     $binary = $chunk->toBinary(true);
     $index = LevelDB::chunkIndex($chunk->getX(), $chunk->getZ());
     $this->db->put($index . "0", substr($binary, 8, -1));
     $this->db->put($index . "f", substr($binary, -1));
     $this->db->put($index . "v", "");
 }
Пример #3
0
 /**
  * @param string        $data
  * @param LevelProvider $provider
  *
  * @return Chunk
  */
 public static function fromBinary($data, LevelProvider $provider = null)
 {
     try {
         $chunkX = PHP_INT_SIZE === 8 ? unpack("V", substr($data, 0, 4))[1] << 32 >> 32 : unpack("V", substr($data, 0, 4))[1];
         $chunkZ = PHP_INT_SIZE === 8 ? unpack("V", substr($data, 4, 4))[1] << 32 >> 32 : unpack("V", substr($data, 4, 4))[1];
         $chunkData = substr($data, 8, -1);
         $flags = ord(substr($data, -1));
         $entities = null;
         $tiles = null;
         if ($provider instanceof LevelDB) {
             $nbt = new NBT(NBT::LITTLE_ENDIAN);
             $entityData = $provider->getDatabase()->get(substr($data, 0, 8) . "2");
             if ($entityData !== false and strlen($entityData) > 0) {
                 $nbt->read($entityData, true);
                 $entities = $nbt->getData();
                 if (!is_array($entities)) {
                     $entities = [$entities];
                 }
             }
             $tileData = $provider->getDatabase()->get(substr($data, 0, 8) . "1");
             if ($tileData !== false and strlen($tileData) > 0) {
                 $nbt->read($tileData, true);
                 $tiles = $nbt->getData();
                 if (!is_array($tiles)) {
                     $tiles = [$tiles];
                 }
             }
         }
         $chunk = new Chunk($provider instanceof LevelProvider ? $provider : LevelDB::class, $chunkX, $chunkZ, $chunkData, $entities, $tiles);
         if ($flags & 0x1) {
             $chunk->setGenerated();
         }
         if ($flags & 0x2) {
             $chunk->setPopulated();
         }
         return $chunk;
     } catch (\Exception $e) {
         return null;
     }
 }
Пример #4
0
 /**
  * @param string        $data
  * @param LevelProvider $provider
  *
  * @return Chunk
  */
 public static function fromBinary($data, LevelProvider $provider = \null)
 {
     try {
         $chunkX = \unpack("V", \substr($data, 0, 4))[1];
         $chunkZ = \unpack("V", \substr($data, 4, 4))[1];
         $chunkData = \substr($data, 8, -1);
         $flags = \ord(\substr($data, -1));
         $entities = \null;
         $tiles = \null;
         $extraData = [];
         if ($provider instanceof LevelDB) {
             $nbt = new NBT(NBT::LITTLE_ENDIAN);
             $entityData = $provider->getDatabase()->get(\substr($data, 0, 8) . LevelDB::ENTRY_ENTITIES);
             if ($entityData !== \false and \strlen($entityData) > 0) {
                 $nbt->read($entityData, \true);
                 $entities = $nbt->getData();
                 if (!\is_array($entities)) {
                     $entities = [$entities];
                 }
             }
             $tileData = $provider->getDatabase()->get(\substr($data, 0, 8) . LevelDB::ENTRY_TILES);
             if ($tileData !== \false and \strlen($tileData) > 0) {
                 $nbt->read($tileData, \true);
                 $tiles = $nbt->getData();
                 if (!\is_array($tiles)) {
                     $tiles = [$tiles];
                 }
             }
             $tileData = $provider->getDatabase()->get(\substr($data, 0, 8) . LevelDB::ENTRY_EXTRA_DATA);
             if ($tileData !== \false and \strlen($tileData) > 0) {
                 $stream = new BinaryStream($tileData);
                 $count = $stream->getInt();
                 for ($i = 0; $i < $count; ++$i) {
                     $key = $stream->getInt();
                     $value = $stream->getShort(\false);
                     $extraData[$key] = $value;
                 }
             }
         }
         $chunk = new Chunk($provider instanceof LevelProvider ? $provider : LevelDB::class, $chunkX, $chunkZ, $chunkData, $entities, $tiles);
         if ($flags & 0x1) {
             $chunk->setGenerated();
         }
         if ($flags & 0x2) {
             $chunk->setPopulated();
         }
         if ($flags & 0x4) {
             $chunk->setLightPopulated();
         }
         return $chunk;
     } catch (\Exception $e) {
         return \null;
     }
 }