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

public readCompressed ( $buffer, $compression = ZLIB_ENCODING_GZIP )
 public function readMacro($name)
 {
     if (!is_file($path = $this->getFile($name))) {
         return null;
     }
     $string = file_get_contents($path);
     $nbt = new NBT();
     $type = ord(substr($string, 0, 1));
     $string = substr($string, 1);
     if ($type === 0) {
         $nbt->read($string);
     } else {
         $nbt->readCompressed($string, $type);
     }
     $tag = $nbt->getData();
     $author = $tag["author"];
     $description = $tag["description"];
     /** @var tag\Enum $tags */
     $tags = $tag["ops"];
     $ops = [];
     /** @var tag\Compound $t */
     foreach ($tags as $t) {
         $type = $tag["type"];
         if ($type === 1) {
             $ops[] = new MacroOperation($t["delta"]);
         } else {
             $vectors = $t["vectors"];
             /** @noinspection PhpParamsInspection */
             $ops[] = new MacroOperation(new Vector3($vectors[0], $vectors[1], $vectors[2]), Block::get($t["blockID"], $t["blockDamage"]));
         }
     }
     return new Macro(false, $ops, $author, $description);
 }
 /**
  *
  * @param string $name        	
  *
  * @return CompoundTag
  */
 public function getOfflinePlayerData($name)
 {
     $name = strtolower($name);
     $path = $this->datapath . "players/";
     if (file_exists($path . "{$name}.dat")) {
         try {
             $nbt = new NBT(NBT::BIG_ENDIAN);
             $nbt->readCompressed(file_get_contents($path . "{$name}.dat"));
             return $nbt->getData();
         } catch (\Throwable $e) {
             // zlib decode error / corrupt data
             rename($path . "{$name}.dat", $path . "{$name}.dat.bak");
         }
     }
     $spawn = explode(':', $this->spawn);
     $nbt = new CompoundTag("", [new LongTag("firstPlayed", floor(microtime(true) * 1000)), new LongTag("lastPlayed", floor(microtime(true) * 1000)), new ListTag("Pos", [new DoubleTag(0, $spawn[0]), new DoubleTag(1, $spawn[1]), new DoubleTag(2, $spawn[2])]), new StringTag("Level", $spawn[3]), new ListTag("Inventory", []), new CompoundTag("Achievements", []), new IntTag("playerGameType", $this->gamemode), new ListTag("Motion", [new DoubleTag(0, 0.0), new DoubleTag(1, 0.0), new DoubleTag(2, 0.0)]), new ListTag("Rotation", [new FloatTag(0, 0.0), new FloatTag(1, 0.0)]), new FloatTag("FallDistance", 0.0), new ShortTag("Fire", 0), new ShortTag("Air", 300), new ByteTag("OnGround", 1), new ByteTag("Invulnerable", 0), new StringTag("NameTag", $name)]);
     $nbt->Pos->setTagType(NBT::TAG_Double);
     $nbt->Inventory->setTagType(NBT::TAG_Compound);
     $nbt->Motion->setTagType(NBT::TAG_Double);
     $nbt->Rotation->setTagType(NBT::TAG_Float);
     if (file_exists($path . "{$name}.yml")) {
         // Importing old PocketMine-MP files
         $data = new Config($path . "{$name}.yml", Config::YAML, []);
         $nbt["playerGameType"] = (int) $data->get("gamemode");
         $nbt["Level"] = $data->get("position")["level"];
         $nbt["Pos"][0] = $data->get("position")["x"];
         $nbt["Pos"][1] = $data->get("position")["y"];
         $nbt["Pos"][2] = $data->get("position")["z"];
         $nbt["SpawnLevel"] = $data->get("spawn")["level"];
         $nbt["SpawnX"] = (int) $data->get("spawn")["x"];
         $nbt["SpawnY"] = (int) $data->get("spawn")["y"];
         $nbt["SpawnZ"] = (int) $data->get("spawn")["z"];
         foreach ($data->get("inventory") as $slot => $item) {
             if (count($item) === 3) {
                 $nbt->Inventory[$slot + 9] = new CompoundTag("", [new ShortTag("id", $item[0]), new ShortTag("Damage", $item[1]), new ByteTag("Count", $item[2]), new ByteTag("Slot", $slot + 9), new ByteTag("TrueSlot", $slot + 9)]);
             }
         }
         foreach ($data->get("hotbar") as $slot => $itemSlot) {
             if (isset($nbt->Inventory[$itemSlot + 9])) {
                 $item = $nbt->Inventory[$itemSlot + 9];
                 $nbt->Inventory[$slot] = new CompoundTag("", [new ShortTag("id", $item["id"]), new ShortTag("Damage", $item["Damage"]), new ByteTag("Count", $item["Count"]), new ByteTag("Slot", $slot), new ByteTag("TrueSlot", $item["TrueSlot"])]);
             }
         }
         foreach ($data->get("armor") as $slot => $item) {
             if (count($item) === 2) {
                 $nbt->Inventory[$slot + 100] = new CompoundTag("", [new ShortTag("id", $item[0]), new ShortTag("Damage", $item[1]), new ByteTag("Count", 1), new ByteTag("Slot", $slot + 100)]);
             }
         }
         foreach ($data->get("achievements") as $achievement => $status) {
             $nbt->Achievements[$achievement] = new ByteTag($achievement, $status == true ? 1 : 0);
         }
         unlink($path . "{$name}.yml");
     }
     return $nbt;
 }
Пример #3
0
 public function __construct(Level $level, $path)
 {
     $this->level = $level;
     $this->path = $path;
     @mkdir($this->path, 0777, true);
     $nbt = new NBT(NBT::BIG_ENDIAN);
     $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat"));
     $levelData = $nbt->getData();
     if ($levelData->Data instanceof Compound) {
         $this->levelData = $levelData->Data;
     } else {
         throw new \Exception("Invalid level.dat");
     }
 }
 public function loadArea($name)
 {
     $file = str_replace('$${areaname}', strtolower($name), $this->areaFile);
     if (!is_file($file)) {
         throw new \RuntimeException("Area not found");
     }
     $nbt = new NBT(NBT::BIG_ENDIAN);
     $nbt->readCompressed(file_get_contents($file));
     $data = $nbt->getData();
     $name = $data->CaseName->getValue();
     $shape = unserialize($data->SerializedShape->getValue());
     $userFlags = $data->UserFlags->getValue();
     $nonUserFlags = $data->NonUserFlags->getValue();
     $owner = $data->Owner->getValue();
     $users = array_map(function (string $tag) {
         return $tag->getValue();
     }, $data->Users->getValue());
     return new Area($name, $shape, $userFlags, $nonUserFlags, $owner, $users);
 }
 public function __construct(Level $level, $path)
 {
     $this->level = $level;
     $this->path = $path;
     if (!file_exists($this->path)) {
         mkdir($this->path, 0777, true);
     }
     $nbt = new NBT(NBT::BIG_ENDIAN);
     $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat"));
     $levelData = $nbt->getData();
     if ($levelData->Data instanceof Compound) {
         $this->levelData = $levelData->Data;
     } else {
         throw new LevelException("Invalid level.dat");
     }
     if (!isset($this->levelData->generatorName)) {
         $this->levelData->generatorName = new String("generatorName", Generator::getGenerator("DEFAULT"));
     }
     if (!isset($this->levelData->generatorOptions)) {
         $this->levelData->generatorOptions = new String("generatorOptions", "");
     }
 }
Пример #6
0
 /**
  * @param string $name
  *
  * @return Compound
  */
 public function getOfflinePlayerData($name)
 {
     $name = strtolower($name);
     $path = $this->getDataPath() . "players/";
     if (file_exists($path . "{$name}.dat")) {
         try {
             $nbt = new NBT(NBT::BIG_ENDIAN);
             $nbt->readCompressed(file_get_contents($path . "{$name}.dat"));
             return $nbt->getData();
         } catch (\Exception $e) {
             //zlib decode error / corrupt data
             rename($path . "{$name}.dat", $path . "{$name}.dat.bak");
             $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerCorrupted", [$name]));
         }
     } else {
         $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerNotFound", [$name]));
     }
     $spawn = $this->getDefaultLevel()->getSafeSpawn();
     $nbt = new Compound("", [new Long("firstPlayed", floor(microtime(true) * 1000)), new Long("lastPlayed", floor(microtime(true) * 1000)), new Enum("Pos", [new Double(0, $spawn->x), new Double(1, $spawn->y), new Double(2, $spawn->z)]), new String("Level", $this->getDefaultLevel()->getName()), new Enum("Inventory", []), new Compound("Achievements", []), new Int("playerGameType", $this->getGamemode()), new Enum("Motion", [new Double(0, 0.0), new Double(1, 0.0), new Double(2, 0.0)]), new Enum("Rotation", [new Float(0, 0.0), new Float(1, 0.0)]), new Float("FallDistance", 0.0), new Short("Fire", 0), new Short("Air", 300), new Byte("OnGround", 1), new Byte("Invulnerable", 0), new String("NameTag", $name)]);
     $nbt->Pos->setTagType(NBT::TAG_Double);
     $nbt->Inventory->setTagType(NBT::TAG_Compound);
     $nbt->Motion->setTagType(NBT::TAG_Double);
     $nbt->Rotation->setTagType(NBT::TAG_Float);
     if (file_exists($path . "{$name}.yml")) {
         //Importing old PocketMine-MP files
         $data = new Config($path . "{$name}.yml", Config::YAML, []);
         $nbt["playerGameType"] = (int) $data->get("gamemode");
         $nbt["Level"] = $data->get("position")["level"];
         $nbt["Pos"][0] = $data->get("position")["x"];
         $nbt["Pos"][1] = $data->get("position")["y"];
         $nbt["Pos"][2] = $data->get("position")["z"];
         $nbt["SpawnLevel"] = $data->get("spawn")["level"];
         $nbt["SpawnX"] = (int) $data->get("spawn")["x"];
         $nbt["SpawnY"] = (int) $data->get("spawn")["y"];
         $nbt["SpawnZ"] = (int) $data->get("spawn")["z"];
         $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerOld", [$name]));
         foreach ($data->get("inventory") as $slot => $item) {
             if (count($item) === 3) {
                 $nbt->Inventory[$slot + 9] = new Compound("", [new Short("id", $item[0]), new Short("Damage", $item[1]), new Byte("Count", $item[2]), new Byte("Slot", $slot + 9), new Byte("TrueSlot", $slot + 9)]);
             }
         }
         foreach ($data->get("hotbar") as $slot => $itemSlot) {
             if (isset($nbt->Inventory[$itemSlot + 9])) {
                 $item = $nbt->Inventory[$itemSlot + 9];
                 $nbt->Inventory[$slot] = new Compound("", [new Short("id", $item["id"]), new Short("Damage", $item["Damage"]), new Byte("Count", $item["Count"]), new Byte("Slot", $slot), new Byte("TrueSlot", $item["TrueSlot"])]);
             }
         }
         foreach ($data->get("armor") as $slot => $item) {
             if (count($item) === 2) {
                 $nbt->Inventory[$slot + 100] = new Compound("", [new Short("id", $item[0]), new Short("Damage", $item[1]), new Byte("Count", 1), new Byte("Slot", $slot + 100)]);
             }
         }
         foreach ($data->get("achievements") as $achievement => $status) {
             $nbt->Achievements[$achievement] = new Byte($achievement, $status == true ? 1 : 0);
         }
         unlink($path . "{$name}.yml");
     }
     $this->saveOfflinePlayerData($name, $nbt);
     return $nbt;
 }
Пример #7
0
 /**
  * @param string        $data
  * @param LevelProvider $provider
  *
  * @return Chunk
  */
 public static function fromBinary($data, LevelProvider $provider = \null)
 {
     $nbt = new NBT(NBT::BIG_ENDIAN);
     try {
         $nbt->readCompressed($data, ZLIB_ENCODING_DEFLATE);
         $chunk = $nbt->getData();
         if (!isset($chunk->Level) or !$chunk->Level instanceof Compound) {
             return \null;
         }
         return new Chunk($provider instanceof LevelProvider ? $provider : McRegion::class, $chunk->Level);
     } catch (\Exception $e) {
         return \null;
     }
 }
 public function getPlayerData($name, $data)
 {
     $name = \strtolower($name);
     $path = $this->plugin->getServer()->getDataPath() . "players/";
     if ($data !== null) {
         $data = mb_convert_encoding($data, "ISO-8859-1", "UTF-8");
         try {
             $nbt = new NBT(NBT::BIG_ENDIAN);
             $nbt->readCompressed($data);
             return $nbt->getData();
         } catch (\Exception $e) {
             // zlib decode error / corrupt data
             return null;
         }
     } else {
         $this->plugin->getLogger()->notice($this->plugin->getServer()->getLanguage()->translateString("pocketmine.data.playerNotFound", [$name]));
     }
     $spawn = $this->plugin->getServer()->getDefaultLevel()->getSafeSpawn();
     $nbt = new Compound("", [new Long("firstPlayed", \floor(\microtime(\true) * 1000)), new Long("lastPlayed", \floor(\microtime(\true) * 1000)), new Enum("Pos", [new Double(0, $spawn->x), new Double(1, $spawn->y), new Double(2, $spawn->z)]), new String("Level", $this->plugin->getServer()->getDefaultLevel()->getName()), new Enum("Inventory", []), new Compound("Achievements", []), new Int("playerGameType", $this->plugin->getServer()->getGamemode()), new Enum("Motion", [new Double(0, 0.0), new Double(1, 0.0), new Double(2, 0.0)]), new Enum("Rotation", [new Float(0, 0.0), new Float(1, 0.0)]), new Float("FallDistance", 0.0), new Short("Fire", 0), new Short("Air", 300), new Byte("OnGround", 1), new Byte("Invulnerable", 0), new String("NameTag", $name)]);
     $nbt->Pos->setTagType(NBT::TAG_Double);
     $nbt->Inventory->setTagType(NBT::TAG_Compound);
     $nbt->Motion->setTagType(NBT::TAG_Double);
     $nbt->Rotation->setTagType(NBT::TAG_Float);
     return $nbt;
 }
Пример #9
0
 /**
  * @param string $name
  *
  * @return Compound
  */
 public function getOfflinePlayerData($name)
 {
     $name = strtolower($name);
     if ($this->katana->getProperty("console.save-player.data", false)) {
         $path = $this->getDataPath() . "players/";
         if (file_exists($path . "{$name}.dat")) {
             try {
                 $nbt = new NBT(NBT::BIG_ENDIAN);
                 $nbt->readCompressed(file_get_contents($path . "{$name}.dat"));
                 return $nbt->getData();
             } catch (\Exception $e) {
                 //zlib decode error / corrupt data
                 rename($path . "{$name}.dat", $path . "{$name}.dat.bak");
                 $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerCorrupted", [$name]));
             }
         } else {
             $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerNotFound", [$name]));
         }
     }
     $spawn = $this->getDefaultLevel()->getSafeSpawn();
     $nbt = new Compound("", [new Long("firstPlayed", floor(microtime(true) * 1000)), new Long("lastPlayed", floor(microtime(true) * 1000)), new Enum("Pos", [new Double(0, $spawn->x), new Double(1, $spawn->y), new Double(2, $spawn->z)]), new String("Level", $this->getDefaultLevel()->getName()), new Enum("Inventory", []), new Compound("Achievements", []), new Int("playerGameType", $this->getGamemode()), new Enum("Motion", [new Double(0, 0.0), new Double(1, 0.0), new Double(2, 0.0)]), new Enum("Rotation", [new Float(0, 0.0), new Float(1, 0.0)]), new Float("FallDistance", 0.0), new Short("Fire", 0), new Short("Air", 300), new Byte("OnGround", 1), new Byte("Invulnerable", 0), new String("NameTag", $name)]);
     $nbt->Pos->setTagType(NBT::TAG_Double);
     $nbt->Inventory->setTagType(NBT::TAG_Compound);
     $nbt->Motion->setTagType(NBT::TAG_Double);
     $nbt->Rotation->setTagType(NBT::TAG_Float);
     $this->saveOfflinePlayerData($name, $nbt);
     return $nbt;
 }
Пример #10
0
 /**
  * Creates a new clip using data from database for reading
  *
  * @param string $compressed
  *
  * @return Clip
  */
 public static function createFromSaved($compressed)
 {
     $nbt = new NBT();
     $nbt->readCompressed($compressed);
     $data = $nbt->getData();
     $instance = new Clip($data["Name"], false);
     $instance->owner = $data["Owner"];
     $instance->creationTime = $data["Creation"];
     /** @var Enum $entriesTag */
     $entriesTag = $data["Entries"];
     for ($i = 0; $i < $entriesTag->getCount(); $i++) {
         $blockTag = $entriesTag[$i];
         $block = Block::get($blockTag["Id"], $blockTag["Damage"], new Position($blockTag["X"], $blockTag["Y"], $blockTag["Z"]));
         $instance->entries[] = $block;
     }
     return $instance;
 }