/** * @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; } }
/** * @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; } }