write() публичный Метод

public write ( ) : string | boolean
Результат string | boolean
Пример #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();
 }
 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);
 }
Пример #3
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;
 }
Пример #4
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;
 }
Пример #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);
     return true;
 }
Пример #6
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;
 }
Пример #7
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;
 }
Пример #8
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
 }
Пример #9
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;
 }
Пример #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));
 }
Пример #11
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);
 }
Пример #12
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));
 }
Пример #13
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;
 }
Пример #14
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;
 }
Пример #15
0
 private function upgrade_From1_To2()
 {
     MainLogger::getLogger()->notice("Old PMF Level format version #1 detected, upgrading to version #2");
     $nbt = new Compound("", [new Enum("Entities", []), new Enum("TileEntities", [])]);
     $nbt->Entities->setTagType(NBT::TAG_Compound);
     $nbt->TileEntities->setTagType(NBT::TAG_Compound);
     $nbtCodec = new NBT(NBT::BIG_ENDIAN);
     $nbtCodec->setData($nbt);
     $namedtag = $nbtCodec->write();
     $namedtag = Binary::writeInt(strlen($namedtag)) . $namedtag;
     foreach (glob(dirname($this->file) . "/chunks/*/*.*.pmc") as $chunkFile) {
         $oldChunk = zlib_decode(file_get_contents($chunkFile));
         $newChunk = substr($oldChunk, 0, 5);
         $newChunk .= $namedtag;
         $newChunk .= str_repeat("", 256);
         //Biome indexes (all Plains)
         $newChunk .= substr($oldChunk, 5);
         file_put_contents($chunkFile, zlib_encode($newChunk, self::ZLIB_ENCODING, self::ZLIB_LEVEL));
     }
     $this->levelData["version"] = 2;
 }
Пример #16
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);
 }
Пример #17
0
 public function toFastBinary()
 {
     $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->write();
 }
Пример #18
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;
 }
Пример #19
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]);
 }