private static function writeEnchantList(EnchantmentList $list, BinaryStream $stream) { $stream->putByte($list->getSize()); for ($i = 0; $i < $list->getSize(); ++$i) { $entry = $list->getSlot($i); $stream->putInt($entry->getCost()); $stream->putByte(count($entry->getEnchantments())); foreach ($entry->getEnchantments() as $enchantment) { $stream->putInt($enchantment->getId()); $stream->putInt($enchantment->getLevel()); } $stream->putString($entry->getRandomName()); } return CraftingDataPacket::ENTRY_ENCHANT_LIST; }
public function toBinary() { $nbt = clone $this->getNBT(); $nbt->xPos = new Int("xPos", $this->x); $nbt->zPos = new Int("zPos", $this->z); $nbt->Sections = new Enum("Sections", []); $nbt->Sections->setTagType(NBT::TAG_Compound); foreach ($this->getSections() as $section) { if ($section instanceof EmptyChunkSection) { continue; } $nbt->Sections[$section->getY()] = new Compound(\null, ["Y" => new Byte("Y", $section->getY()), "Blocks" => new ByteArray("Blocks", $section->getIdArray()), "Data" => new ByteArray("Data", $section->getDataArray()), "BlockLight" => new ByteArray("BlockLight", $section->getLightArray()), "SkyLight" => new ByteArray("SkyLight", $section->getSkyLightArray())]); } $nbt->BiomeColors = new IntArray("BiomeColors", $this->getBiomeColorArray()); $nbt->HeightMap = new IntArray("HeightMap", $this->getHeightMapArray()); $entities = []; foreach ($this->getEntities() as $entity) { if (!$entity instanceof Player and !$entity->closed) { $entity->saveNBT(); $entities[] = $entity->namedtag; } } $nbt->Entities = new Enum("Entities", $entities); $nbt->Entities->setTagType(NBT::TAG_Compound); $tiles = []; foreach ($this->getTiles() as $tile) { $tile->saveNBT(); $tiles[] = $tile->namedtag; } $nbt->TileEntities = new Enum("TileEntities", $tiles); $nbt->TileEntities->setTagType(NBT::TAG_Compound); $extraData = new BinaryStream(); $extraData->putInt(\count($this->getBlockExtraDataArray())); foreach ($this->getBlockExtraDataArray() as $key => $value) { $extraData->putInt($key); $extraData->putShort($value); } $nbt->ExtraData = new ByteArray("ExtraData", $extraData->getBuffer()); $writer = new NBT(NBT::BIG_ENDIAN); $nbt->setName("Level"); $writer->setData(new Compound("", ["Level" => $nbt])); return $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); }
public function toBinary() { $nbt = clone $this->getNBT(); $nbt->xPos = new IntTag("xPos", $this->x); $nbt->zPos = new IntTag("zPos", $this->z); if ($this->isGenerated()) { $nbt->Blocks = new ByteArrayTag("Blocks", $this->getBlockIdArray()); $nbt->Data = new ByteArrayTag("Data", $this->getBlockDataArray()); $nbt->SkyLight = new ByteArrayTag("SkyLight", $this->getBlockSkyLightArray()); $nbt->BlockLight = new ByteArrayTag("BlockLight", $this->getBlockLightArray()); $nbt->BiomeColors = new IntArrayTag("BiomeColors", $this->getBiomeColorArray()); $nbt->HeightMap = new IntArrayTag("HeightMap", $this->getHeightMapArray()); } $entities = []; foreach ($this->getEntities() as $entity) { if (!$entity instanceof Player and !$entity->closed) { $entity->saveNBT(); $entities[] = $entity->namedtag; } } $nbt->Entities = new ListTag("Entities", $entities); $nbt->Entities->setTagType(NBT::TAG_Compound); $tiles = []; foreach ($this->getTiles() as $tile) { $tile->saveNBT(); $tiles[] = $tile->namedtag; } $nbt->TileEntities = new ListTag("TileEntities", $tiles); $nbt->TileEntities->setTagType(NBT::TAG_Compound); $extraData = new BinaryStream(); $extraData->putInt(count($this->getBlockExtraDataArray())); foreach ($this->getBlockExtraDataArray() as $key => $value) { $extraData->putInt($key); $extraData->putShort($value); } $nbt->ExtraData = new ByteArrayTag("ExtraData", $extraData->getBuffer()); $writer = new NBT(NBT::BIG_ENDIAN); $nbt->setName("Level"); $writer->setData(new CompoundTag("", ["Level" => $nbt])); return $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); }
public function toBinary($saveExtra = \false) { $chunkIndex = LevelDB::chunkIndex($this->getX(), $this->getZ()); $provider = $this->getProvider(); if ($saveExtra and $provider instanceof LevelDB) { $nbt = new NBT(NBT::LITTLE_ENDIAN); $entities = []; foreach ($this->getEntities() as $entity) { if (!$entity instanceof Player and !$entity->closed) { $entity->saveNBT(); $entities[] = $entity->namedtag; } } if (\count($entities) > 0) { $nbt->setData($entities); $provider->getDatabase()->put($chunkIndex . LevelDB::ENTRY_ENTITIES, $nbt->write()); } else { $provider->getDatabase()->delete($chunkIndex . LevelDB::ENTRY_ENTITIES); } $tiles = []; foreach ($this->getTiles() as $tile) { if (!$tile->closed) { $tile->saveNBT(); $tiles[] = $tile->namedtag; } } if (\count($tiles) > 0) { $nbt->setData($tiles); $provider->getDatabase()->put($chunkIndex . LevelDB::ENTRY_TILES, $nbt->write()); } else { $provider->getDatabase()->delete($chunkIndex . LevelDB::ENTRY_TILES); } if (\count($this->getBlockExtraDataArray()) > 0) { $extraData = new BinaryStream(); $extraData->putInt(\count($this->getBlockExtraDataArray())); foreach ($this->getBlockExtraDataArray() as $key => $value) { $extraData->putInt($key); $extraData->putShort($value); } $provider->getDatabase()->put($chunkIndex . LevelDB::ENTRY_EXTRA_DATA, $extraData->getBuffer()); } else { $provider->getDatabase()->delete($chunkIndex . LevelDB::ENTRY_EXTRA_DATA); } } $heightmap = \pack("C*", ...$this->getHeightMapArray()); $biomeColors = \pack("N*", ...$this->getBiomeColorArray()); return $chunkIndex . $this->getBlockIdArray() . $this->getBlockDataArray() . $this->getBlockSkyLightArray() . $this->getBlockLightArray() . $heightmap . $biomeColors . \chr(($this->isLightPopulated() ? 0x4 : 0) | ($this->isPopulated() ? 0x2 : 0) | ($this->isGenerated() ? 0x1 : 0)); }
/** * @param Compound $nbt * @return NBT */ protected function prepareChunkBinaryWriter(Compound $nbt) { $entities = []; foreach ($this->getEntities() as $entity) { if (!$entity instanceof Player and !$entity->closed) { $entity->saveNBT(); $entities[] = $entity->namedtag; } } $nbt->Entities = new Enum("Entities", $entities); $nbt->Entities->setTagType(NBT::TAG_Compound); $tiles = []; foreach ($this->getTiles() as $tile) { $tile->saveNBT(); $tiles[] = $tile->namedtag; } $nbt->TileEntities = new Enum("TileEntities", $tiles); $nbt->TileEntities->setTagType(NBT::TAG_Compound); $extraData = new BinaryStream(); $extraData->putInt(count($this->getBlockExtraDataArray())); foreach ($this->getBlockExtraDataArray() as $key => $value) { $extraData->putInt($key); $extraData->putShort($value); } $nbt->ExtraData = new ByteArray("ExtraData", $extraData->getBuffer()); $writer = new NBT(NBT::BIG_ENDIAN); $nbt->setName("Level"); $writer->setData(new Compound("", ["Level" => $nbt])); return $writer; }