setData() public method

public setData ( CompoundTag | array $data )
$data pocketmine\nbt\tag\CompoundTag | array
Esempio n. 1
0
 private static function writeCompoundTag(CompoundTag $tag) : string
 {
     if (self::$cachedParser === null) {
         self::$cachedParser = new NBT(NBT::LITTLE_ENDIAN);
     }
     self::$cachedParser->setData($tag);
     return self::$cachedParser->write();
 }
Esempio n. 2
0
 public function generateChunk($x, $z)
 {
     $nbt = new Compound("Level", []);
     $nbt->xPos = new Int("xPos", $this->getX() * 32 + $x);
     $nbt->zPos = new Int("zPos", $this->getZ() * 32 + $z);
     $nbt->LastUpdate = new Long("LastUpdate", 0);
     $nbt->LightPopulated = new Byte("LightPopulated", 0);
     $nbt->TerrainPopulated = new Byte("TerrainPopulated", 0);
     $nbt->V = new Byte("V", self::VERSION);
     $nbt->InhabitedTime = new Long("InhabitedTime", 0);
     $biomes = str_repeat(Binary::writeByte(-1), 256);
     $nbt->Biomes = new ByteArray("Biomes", $biomes);
     $nbt->BiomeColors = new IntArray("BiomeColors", array_fill(0, 156, Binary::readInt("…²J")));
     $nbt->HeightMap = new IntArray("HeightMap", array_fill(0, 256, 127));
     $nbt->Sections = new Enum("Sections", []);
     $nbt->Sections->setTagType(NBT::TAG_Compound);
     $nbt->Entities = new Enum("Entities", []);
     $nbt->Entities->setTagType(NBT::TAG_Compound);
     $nbt->TileEntities = new Enum("TileEntities", []);
     $nbt->TileEntities->setTagType(NBT::TAG_Compound);
     $nbt->TileTicks = new Enum("TileTicks", []);
     $nbt->TileTicks->setTagType(NBT::TAG_Compound);
     $writer = new NBT(NBT::BIG_ENDIAN);
     $nbt->setName("Level");
     $writer->setData(new Compound("", ["Level" => $nbt]));
     $chunkData = $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL);
     $this->saveChunk($x, $z, $chunkData);
 }
 public function saveMacro($name, Macro $macro)
 {
     $tag = new tag\Compound();
     $tag["author"] = new tag\String("author", $macro->getAuthor());
     $tag["description"] = new tag\String("description", $macro->getDescription());
     $tag["ops"] = new tag\Enum("ops");
     foreach ($macro->getOperations() as $i => $log) {
         $tag["ops"][$i] = $log->toTag();
     }
     $nbt = new NBT();
     $nbt->setData($tag);
     $file = $this->getFile($name);
     $stream = fopen($file, "wb");
     if (!is_resource($stream)) {
         throw new \RuntimeException("Unable to open stream. Maybe the macro name is not a valid filename?");
     }
     $compression = $this->getMain()->getConfig()->getAll()["data providers"]["macro"]["mcr"]["compression"];
     if ($compression === 0) {
         $data = $nbt->write();
     } else {
         $data = $nbt->writeCompressed($compression);
     }
     fwrite($stream, chr($compression) . $data);
     fclose($stream);
 }
Esempio n. 4
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     if (count($chunk->getTiles()) > 0) {
         $nbt = new NBT(NBT::LITTLE_ENDIAN);
         $list = [];
         foreach ($chunk->getTiles() as $tile) {
             if ($tile instanceof Spawnable) {
                 $list[] = $tile->getSpawnCompound();
             }
         }
         $nbt->setData($list);
         $tiles = $nbt->write(true);
     }
     $extraData = new BinaryStream();
     $extraData->putLInt(count($chunk->getBlockExtraDataArray()));
     foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
         $extraData->putLInt($key);
         $extraData->putLShort($value);
     }
     $ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . pack("C*", ...$chunk->getHeightMapArray()) . pack("N*", ...$chunk->getBiomeColorArray()) . $extraData->getBuffer() . $tiles;
     $this->getLevel()->chunkRequestCallback($x, $z, $ordered, FullChunkDataPacket::ORDER_LAYERED);
     return null;
 }
Esempio n. 5
0
 public function spawnTo(Player $player)
 {
     if ($this->closed) {
         return \false;
     }
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     $nbt->setData($this->getSpawnCompound());
     $pk = new TileEntityDataPacket();
     $pk->x = $this->x;
     $pk->y = $this->y;
     $pk->z = $this->z;
     $pk->namedtag = $nbt->write();
     $player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
     return \true;
 }
Esempio n. 6
0
 public function spawnTo(Player $player)
 {
     if ($this->closed) {
         return false;
     }
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     $nbt->setData($this->getSpawnCompound());
     $pk = new TileEntityDataPacket();
     $pk->x = $this->x;
     $pk->y = $this->y;
     $pk->z = $this->z;
     $pk->namedtag = $nbt->write();
     $player->dataPacket($pk);
     return true;
 }
Esempio n. 7
0
 public function __construct(Level $level, Chunk $chunk)
 {
     $this->levelId = $level->getId();
     $this->chunk = $chunk->toFastBinary();
     $this->chunkX = $chunk->getX();
     $this->chunkZ = $chunk->getZ();
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $this->tiles = $tiles;
 }
Esempio n. 8
0
 public function __construct(Anvil $level, $levelId, $chunkX, $chunkZ)
 {
     $this->levelId = $levelId;
     $this->chunkX = $chunkX;
     $this->chunkZ = $chunkZ;
     $chunk = $level->getChunk($chunkX, $chunkZ, false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $this->biomeIds = $chunk->getBiomeIdArray();
     $this->biomeColors = $chunk->getBiomeColorArray();
     $this->sections = $chunk->getSections();
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $this->tiles = $tiles;
     $this->compressionLevel = Level::$COMPRESSION_LEVEL;
 }
Esempio n. 9
0
 public function onRun($currentTicks)
 {
     $this->getOwner()->updateVars();
     foreach ($this->getOwner()->getServer()->getLevels() as $lv) {
         if (count($lv->getPlayers()) == 0) {
             continue;
         }
         foreach ($lv->getTiles() as $tile) {
             if (!$tile instanceof Sign) {
                 continue;
             }
             $sign = $tile->getText();
             $text = $this->getOwner()->getLiveSign($sign);
             if ($text == null) {
                 continue;
             }
             $pk = new TileEntityDataPacket();
             $data = $tile->getSpawnCompound();
             $data->Text1 = new String("Text1", $text[0]);
             $data->Text2 = new String("Text2", $text[1]);
             $data->Text3 = new String("Text3", $text[2]);
             $data->Text4 = new String("Text4", $text[3]);
             $nbt = new NBT(NBT::LITTLE_ENDIAN);
             $nbt->setData($data);
             $pk->x = $tile->getX();
             $pk->y = $tile->getY();
             $pk->z = $tile->getZ();
             $pk->namedtag = $nbt->write();
             foreach ($lv->getPlayers() as $pl) {
                 $pl->dataPacket($pk);
             }
             //foreach Players
         }
         //foreach Tiles
     }
     // foreach Levels
 }
Esempio n. 10
0
 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();
                 $nbt->setData($entity->namedtag);
                 $entities[] = $nbt->write();
             }
         }
         if (count($entities) > 0) {
             $provider->getDatabase()->put($chunkIndex . "2", implode($entities));
         } else {
             $provider->getDatabase()->delete($chunkIndex . "2");
         }
         $tiles = [];
         foreach ($this->getTiles() as $tile) {
             if (!$tile->closed) {
                 $tile->saveNBT();
                 $nbt->setData($tile->namedtag);
                 $tiles[] = $nbt->write();
             }
         }
         if (count($tiles) > 0) {
             $provider->getDatabase()->put($chunkIndex . "1", implode($tiles));
         } else {
             $provider->getDatabase()->delete($chunkIndex . "1");
         }
     }
     $biomeColors = pack("N*", ...$this->getBiomeColorArray());
     return $chunkIndex . $this->getBlockIdArray() . $this->getBlockDataArray() . $this->getBlockSkyLightArray() . $this->getBlockLightArray() . $this->getBiomeIdArray() . $biomeColors . chr(($this->isPopulated() ? 0x2 : 0) | ($this->isGenerated() ? 0x1 : 0));
 }
Esempio n. 11
0
 private function updateTile($tile)
 {
     $sign = $tile->getText();
     $sn = $this->texts[$sign[0]];
     $upd = [$sign[0], $sign[1], $sign[2], $sign[3]];
     switch ($sn) {
         case "stats":
             $lv = $tile->getLevel();
             foreach ($lv->getPlayers() as $pl) {
                 $score = $this->dbm->getScore($pl->getName());
                 if ($score == null) {
                     continue;
                 }
                 $money = $this->getMoney($pl->getName());
                 $data = $tile->getSpawnCompound();
                 $data->Text1 = new String("Text1", $sign[0]);
                 $data->Text2 = new String("Text2", "Level: " . $score["level"]);
                 $data->Text3 = new String("Text3", "Kills: " . $score["kills"]);
                 $data->Text4 = new String("Text4", "Points: " . $money);
                 $nbt = new NBT(NBT::LITTLE_ENDIAN);
                 $nbt->setData($data);
                 $pk = new EntityDataPacket();
                 $pk->x = $tile->getX();
                 $pk->y = $tile->getY();
                 $pk->z = $tile->getZ();
                 $pk->namedtag = $nbt->write();
                 $pl->dataPacket($pk);
             }
             break;
         case "rankings":
         case "onlineranks":
             $res = $this->getRankings(3, $sn == "onlineranks");
             if ($res == null) {
                 $upd[1] = "Not Available";
                 $upd[2] = "insufficient";
                 $upd[3] = "players on-line";
                 break;
             }
             $upd[1] = "";
             $upd[2] = "";
             $upd[3] = "";
             $i = 1;
             foreach ($res as $r) {
                 $upd[$i] = implode(" ", [$r["player"], $r["kills"]]);
                 ++$i;
             }
             break;
         default:
             return;
     }
     if ($upd[0] == $sign[0] && $upd[2] == $sign[2] && $upd[1] == $sign[1] && $upd[3] == $sign[3]) {
         return;
     }
     $tile->setText($upd[0], $upd[1], $upd[2], $upd[3]);
 }
 public function saveLevelData()
 {
     $nbt = new NBT(NBT::BIG_ENDIAN);
     $nbt->setData(new Compound("", ["Data" => $this->levelData]));
     $buffer = $nbt->writeCompressed();
     file_put_contents($this->getPath() . "level.dat", $buffer);
 }
Esempio n. 13
0
 private function updateSign($pl, $tile, $text)
 {
     $pk = new TileEntityDataPacket();
     $data = $tile->getSpawnCompound();
     $data->Text1 = new String("Text1", $text[0]);
     $data->Text2 = new String("Text2", $text[1]);
     $data->Text3 = new String("Text3", $text[2]);
     $data->Text4 = new String("Text4", $text[3]);
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     $nbt->setData($data);
     $pk->x = $tile->getX();
     $pk->y = $tile->getY();
     $pk->z = $tile->getZ();
     $pk->namedtag = $nbt->write();
     $pl->dataPacket($pk);
 }
Esempio n. 14
0
 public function generateChunk($x, $z)
 {
     $nbt = new Compound("Level", []);
     $nbt->xPos = new Int("xPos", $this->getX() * 32 + $x);
     $nbt->zPos = new Int("zPos", $this->getZ() * 32 + $z);
     $nbt->LastUpdate = new Long("LastUpdate", 0);
     $nbt->LightPopulated = new Byte("LightPopulated", 0);
     $nbt->TerrainPopulated = new Byte("TerrainPopulated", 0);
     $nbt->V = new Byte("V", self::VERSION);
     $nbt->InhabitedTime = new Long("InhabitedTime", 0);
     $nbt->Biomes = new ByteArray("Biomes", \str_repeat(\chr(-1), 256));
     $nbt->HeightMap = new IntArray("HeightMap", \array_fill(0, 256, 127));
     $nbt->BiomeColors = new IntArray("BiomeColors", \array_fill(0, 256, \PHP_INT_SIZE === 8 ? \unpack("N", "…²J")[1] << 32 >> 32 : \unpack("N", "…²J")[1]));
     $nbt->Blocks = new ByteArray("Blocks", \str_repeat("", 32768));
     $nbt->Data = new ByteArray("Data", $half = \str_repeat("", 16384));
     $nbt->SkyLight = new ByteArray("SkyLight", $half);
     $nbt->BlockLight = new ByteArray("BlockLight", $half);
     $nbt->Entities = new Enum("Entities", []);
     $nbt->Entities->setTagType(NBT::TAG_Compound);
     $nbt->TileEntities = new Enum("TileEntities", []);
     $nbt->TileEntities->setTagType(NBT::TAG_Compound);
     $nbt->TileTicks = new Enum("TileTicks", []);
     $nbt->TileTicks->setTagType(NBT::TAG_Compound);
     $writer = new NBT(NBT::BIG_ENDIAN);
     $nbt->setName("Level");
     $writer->setData(new Compound("", ["Level" => $nbt]));
     $chunkData = $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, self::$COMPRESSION_LEVEL);
     if ($chunkData !== \false) {
         $this->saveChunk($x, $z, $chunkData);
     }
 }
 /**
  * Called when the user logs out
  *
  * @param PlayerQuitEvent $event        	
  */
 public function onQuit(PlayerQuitEvent $event)
 {
     if (isset($this->standbyAuth[strtolower($event->getPlayer()->getName())])) {
         unset($this->standbyAuth[strtolower($event->getPlayer()->getName())]);
         return;
     }
     if (isset($this->needAuth[strtolower($event->getPlayer()->getName())])) {
         unset($this->needAuth[strtolower($event->getPlayer()->getName())]);
         return;
     }
     if ($this->plugin->getConfig()->get("servermode", null) != "slave") {
         return;
     }
     $nbt = new NBT(NBT::BIG_ENDIAN);
     try {
         $nbt->setData($event->getPlayer()->namedtag);
         $nbtFile = mb_convert_encoding($nbt->writeCompressed(), "UTF-8", "ISO-8859-1");
         // itemSyncro
         // slave->master = [passcode, itemSyncro, username, itemData]
         $data = [$this->plugin->getConfig()->get("passcode"), "itemSyncro", $event->getPlayer()->getName(), $nbtFile];
         CPAPI::sendPacket(new DataPacket($this->plugin->getConfig()->get("masterip"), $this->plugin->getConfig()->get("masterport"), json_encode($data)));
     } catch (\Exception $e) {
         $this->plugin->getLogger()->critical($this->plugin->getServer()->getLanguage()->translateString("pocketmine.data.saveError", [$event->getPlayer()->getName(), $e->getMessage()]));
         if (\pocketmine\DEBUG > 1 and $this->plugin->getServer()->getLogger() instanceof MainLogger) {
             $this->plugin->getServer()->getLogger()->logException($e);
         }
     }
     // logoutRequest
     // slave->master = [passcode, logoutRequest, username, IP, isUserGenerate]
     $data = [$this->plugin->getConfig()->get("passcode"), "logoutRequest", $event->getPlayer()->getName(), $event->getPlayer()->getAddress(), false];
     CPAPI::sendPacket(new DataPacket($this->plugin->getConfig()->get("masterip"), $this->plugin->getConfig()->get("masterport"), json_encode($data)));
 }
Esempio n. 16
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, \false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $heightmap = \pack("C*", ...$chunk->getHeightMapArray());
     $biomeColors = \pack("N*", ...$chunk->getBiomeColorArray());
     $extraData = new BinaryStream();
     $extraData->putLInt(\count($chunk->getBlockExtraDataArray()));
     foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
         $extraData->putLInt($key);
         $extraData->putLShort($value);
     }
     $ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . $heightmap . $biomeColors . $extraData->getBuffer() . $tiles;
     $this->getLevel()->chunkRequestCallback($x, $z, $ordered);
     return \null;
 }
Esempio n. 17
0
 public function toBinary()
 {
     $data = new Compound($this->name);
     // we will have trouble updating these NBT tags to PHP 7 :(
     // But hopefully it will be as simple as search & replace with regex:
     // /(String|Int|Byte|blah)/   ->    $1Tag
     // depends if shoghicp does even more destruction :P
     $data->Name = new String("Name", $this->name);
     // unique name of the WorldEditSession
     $data->Owner = new String("Owner", $this->owner);
     // OK, I know that this will no longer work after 2038 January, but who will use this plugin until 2038?
     $data->Creation = new Int("Creation", $this->creationTime);
     $entries = new Enum("Entries", array_map(function (Block $block) {
         $compound = new Compound();
         $compound->X = new Int("X", $block->x);
         $compound->Y = new Int("Y", $block->y);
         $compound->Z = new Int("Z", $block->z);
         $compound->Id = new Byte("Id", $block->getId());
         $compound->Damage = new Byte("Damage", $block->getDamage());
         return $compound;
     }, $this->entries));
     $data->Entries = $entries;
     $nbt = new NBT();
     $nbt->setData($data);
     return $nbt->writeCompressed();
 }
Esempio n. 18
0
 /**
  * @param string   $name
  * @param Compound $nbtTag
  */
 public function saveOfflinePlayerData($name, Compound $nbtTag)
 {
     $nbt = new NBT(NBT::BIG_ENDIAN);
     $nbt->setData($nbtTag);
     file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
 }
Esempio n. 19
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, \false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $biomeColors = \pack("N*", ...$chunk->getBiomeColorArray());
     $ordered = \zlib_encode(\pack("V", $x) . \pack("V", $z) . $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . $chunk->getBiomeIdArray() . $biomeColors . $tiles, ZLIB_ENCODING_DEFLATE, Level::$COMPRESSION_LEVEL);
     $this->getLevel()->chunkRequestCallback($x, $z, $ordered);
     return \null;
 }
Esempio n. 20
0
 public function saveChunk($X, $Z, $force = false)
 {
     $X = (int) $X;
     $Z = (int) $Z;
     if (!$this->isChunkLoaded($X, $Z)) {
         return false;
     }
     $index = self::getIndex($X, $Z);
     if ($force !== true and (!isset($this->chunkChange[$index]) or $this->chunkChange[$index][-1] === false)) {
         //No changes in chunk
         return true;
     }
     $path = $this->getChunkPath($X, $Z);
     if (!file_exists(dirname($path))) {
         @mkdir(dirname($path), 0755);
     }
     $bitmap = 0;
     $this->cleanChunk($X, $Z);
     for ($Y = 0; $Y < 8; ++$Y) {
         if ($this->chunks[$index][$Y] !== false and (isset($this->chunkChange[$index][$Y]) and $this->chunkChange[$index][$Y] === 0 or !$this->isMiniChunkEmpty($X, $Z, $Y))) {
             $bitmap |= 1 << $Y;
         } else {
             $this->chunks[$index][$Y] = false;
         }
         $this->chunkChange[$index][$Y] = 0;
     }
     $this->chunkInfo[$index][0] = $bitmap;
     $this->chunkChange[$index][-1] = false;
     $chunk = "";
     $chunk .= chr($bitmap);
     $chunk .= Binary::writeInt($this->chunkInfo[$index][1]);
     $namedtag = new NBT(NBT::BIG_ENDIAN);
     $namedtag->setData($this->chunkInfo[$index][2]);
     $namedtag = $namedtag->write();
     $chunk .= Binary::writeInt(strlen($namedtag)) . $namedtag;
     $chunk .= $this->chunkInfo[$index][3];
     //biomes
     for ($Y = 0; $Y < 8; ++$Y) {
         $t = 1 << $Y;
         if (($bitmap & $t) === $t) {
             $chunk .= $this->chunks[$index][$Y];
         }
     }
     file_put_contents($path, zlib_encode($chunk, self::ZLIB_ENCODING, self::ZLIB_LEVEL));
     return true;
 }
Esempio n. 21
0
 /**
  * @param string   $name
  * @param Compound $nbtTag
  * @param bool $async
  */
 public function saveOfflinePlayerData($name, Compound $nbtTag, $async = false)
 {
     $nbt = new NBT(NBT::BIG_ENDIAN);
     try {
         $nbt->setData($nbtTag);
         if ($async) {
             $this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()));
         } else {
             file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
         }
     } catch (\Exception $e) {
         $this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()]));
         if (\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger) {
             $this->logger->logException($e);
         }
     }
 }
Esempio n. 22
0
 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);
 }
Esempio n. 23
0
 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));
 }
Esempio n. 24
0
 private function updateSign($pl, $tile, $text)
 {
     switch ($this->api) {
         case 10:
             $pk = new EntityDataPacket();
             break;
         case 12:
             $pk = new TileEntityDataPacket();
             break;
         default:
             return;
     }
     $data = $tile->getSpawnCompound();
     $data->Text1 = new String("Text1", $text[0]);
     $data->Text2 = new String("Text2", $text[1]);
     $data->Text3 = new String("Text3", $text[2]);
     $data->Text4 = new String("Text4", $text[3]);
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     $nbt->setData($data);
     $pk->x = $tile->getX();
     $pk->y = $tile->getY();
     $pk->z = $tile->getZ();
     $pk->namedtag = $nbt->write();
     $pl->dataPacket($pk);
 }
Esempio n. 25
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $biomeColors = pack("N*", ...$chunk->getBiomeColorArray());
     $ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . $chunk->getBiomeIdArray() . $biomeColors . $tiles;
     $this->getLevel()->chunkRequestCallback($x, $z, $ordered);
     return null;
 }
Esempio n. 26
0
 public function toBinary()
 {
     $nbt = clone $this->getNBT();
     $nbt->xPos = new Int("xPos", $this->x);
     $nbt->zPos = new Int("zPos", $this->z);
     if ($this->isGenerated()) {
         $nbt->Blocks = new ByteArray("Blocks", $this->getBlockIdArray());
         $nbt->Data = new ByteArray("Data", $this->getBlockDataArray());
         $nbt->SkyLight = new ByteArray("SkyLight", $this->getBlockSkyLightArray());
         $nbt->BlockLight = new ByteArray("BlockLight", $this->getBlockLightArray());
         $nbt->Biomes = new ByteArray("Biomes", $this->getBiomeIdArray());
         $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);
     $writer = new NBT(NBT::BIG_ENDIAN);
     $nbt->setName("Level");
     $writer->setData(new Compound("", ["Level" => $nbt]));
     return $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL);
 }
Esempio n. 27
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $extraData = new BinaryStream();
     $extraData->putLInt(count($chunk->getBlockExtraDataArray()));
     foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
         $extraData->putLInt($key);
         $extraData->putLShort($value);
     }
     $ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . pack("C*", ...$chunk->getHeightMapArray()) . pack("N*", ...$chunk->getBiomeColorArray()) . $extraData->getBuffer() . $tiles;
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $x;
     $pk->chunkZ = $z;
     $pk->order = FullChunkDataPacket::ORDER_COLUMNS;
     $pk->data = $ordered;
     $pk->encode();
     $str = "";
     $str .= Binary::writeInt(strlen($pk->buffer)) . $pk->buffer;
     $this->getLevel()->chunkRequestCallback($x, $z, zlib_encode($str, ZLIB_ENCODING_DEFLATE, 7));
     return null;
 }
Esempio n. 28
0
 /**
  * @param string   $name
  * @param Compound $nbtTag
  */
 public function saveOfflinePlayerData($name, Compound $nbtTag)
 {
     $nbt = new NBT(NBT::BIG_ENDIAN);
     try {
         $nbt->setData($nbtTag);
         file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
     } catch (\Exception $e) {
         $this->logger->critical("Could not save player " . $name . ": " . $e->getMessage());
         if (\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger) {
             $this->logger->logException($e);
         }
     }
 }
Esempio n. 29
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;
 }