Example #1
0
 public function encode()
 {
     $this->putVarInt($this->eid);
     $this->putString($this->uuid);
     $this->putInt(intval($this->x * 32));
     $this->putInt(intval($this->y * 32));
     $this->putInt(intval($this->z * 32));
     $this->putByte($this->yaw / 360 << 8);
     $this->putByte($this->pitch / 360 << 8);
     $this->putShort($this->item);
     $this->put(Binary::writeMetadata($this->metadata));
 }
Example #2
0
 public function encode()
 {
     $this->putVarInt($this->eid);
     $this->putByte($this->type);
     $this->putInt(intval($this->x * 32));
     $this->putInt(intval($this->y * 32));
     $this->putInt(intval($this->z * 32));
     $this->putByte($this->yaw / 360 << 8);
     $this->putByte($this->pitch / 360 << 8);
     $this->putByte($this->headPitch / 360 << 8);
     $this->putShort($this->velocityX * 8000);
     $this->putShort($this->velocityY * 8000);
     $this->putShort($this->velocityZ * 8000);
     $this->put(Binary::writeMetadata($this->metadata));
 }
 public function encode()
 {
     $this->putVarInt($this->eid);
     $this->putLong(substr($this->uuid, 0, 16));
     //UUID
     $this->putLong(substr($this->uuid, 16, 16));
     $this->putInt(intval($this->x * 32));
     $this->putInt(intval($this->y * 32));
     $this->putInt(intval($this->z * 32));
     $this->putByte($this->yaw / 360 << 8);
     $this->putByte($this->pitch / 360 << 8);
     $this->putShort($this->item);
     $meta = Binary::writeMetadata($this->metadata);
     $this->put($meta);
 }
Example #4
0
 protected function closeSession($id)
 {
     $this->thread->pushThreadToMainPacket(chr(self::PACKET_CLOSE_SESSION) . Binary::writeInt($id));
 }
 public function encode()
 {
     $this->putVarInt($this->eid);
     $this->put(Binary::writeMetadata($this->metadata));
 }
Example #6
0
 public function write()
 {
     $this->buffer = "";
     $this->offset = 0;
     $this->encode();
     return Binary::writeVarInt($this->pid()) . $this->buffer;
 }
Example #7
0
 public function bigBrother_processAuthentication(BigBrother $plugin, EncryptionResponsePacket $packet)
 {
     $this->bigBrother_secret = $plugin->decryptBinary($packet->sharedSecret);
     $token = $plugin->decryptBinary($packet->verifyToken);
     $this->interface->enableEncryption($this, $this->bigBrother_secret);
     if ($token !== $this->bigBrother_checkToken) {
         $this->close("", "Invalid check token");
     } else {
         $task = new AuthenticateOnline($this->clientID, $this->bigBrother_username, Binary::sha1("" . $this->bigBrother_secret . $plugin->getASN1PublicKey()));
         $this->server->getScheduler()->scheduleAsyncTask($task);
     }
 }
 public function process()
 {
     if (count($this->identifiers) > 0) {
         foreach ($this->identifiers as $id => $player) {
             $player->handleACK($id);
         }
     }
     while (strlen($buffer = $this->thread->readThreadToMainPacket()) > 0) {
         $offset = 1;
         $pid = ord($buffer[0]);
         if ($pid === ServerManager::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             if (isset($this->sessionsPlayers[$id])) {
                 $payload = substr($buffer, $offset);
                 try {
                     $this->handlePacket($this->sessionsPlayers[$id], $payload);
                 } catch (\Exception $e) {
                     if (\pocketmine\DEBUG > 1) {
                         $logger = $this->server->getLogger();
                         if ($logger instanceof MainLogger) {
                             $logger->debug("DesktopPacket 0x" . bin2hex($payload));
                             $logger->logException($e);
                         }
                     }
                 }
             }
         } elseif ($pid === ServerManager::PACKET_OPEN_SESSION) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             if (isset($this->sessionsPlayers[$id])) {
                 continue;
             }
             $len = ord($buffer[$offset++]);
             $address = substr($buffer, $offset, $len);
             $offset += $len;
             $port = Binary::readShort(substr($buffer, $offset, 2));
             $identifier = "{$id}:{$address}:{$port}";
             $player = new DesktopPlayer($this, $identifier, $address, $port, $this->plugin);
             $this->sessions->attach($player, $id);
             $this->sessionsPlayers[$id] = $player;
             $this->plugin->getServer()->addPlayer($identifier, $player);
         } elseif ($pid === ServerManager::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             $flag = Binary::readInt(substr($buffer, $offset, 4));
             if (isset($this->sessionsPlayers[$id])) {
                 if ($flag === 0) {
                     $this->close($this->sessionsPlayers[$id]);
                 } else {
                     $this->closeSession($id);
                 }
             }
         }
     }
     return true;
 }
Example #9
0
 public function process()
 {
     $length = Binary::readVarIntSession($this);
     if ($length === false or $this->status === -1) {
         $this->close("Connection closed");
         return;
     } elseif ($length <= 0 or $length > 131070) {
         $this->close("Invalid length");
         return;
     }
     $offset = 0;
     $buffer = $this->read($length);
     if ($this->threshold !== null) {
         $dataLength = Binary::readVarInt($buffer, $offset);
         if ($dataLength !== 0) {
             if ($dataLength < $this->threshold) {
                 $this->close("Invalid compression threshold");
             } else {
                 $buffer = zlib_decode(substr($buffer, $offset));
                 $offset = 0;
             }
         } else {
             $buffer = substr($buffer, $offset);
             $offset = 0;
         }
     }
     if ($this->status === 2) {
         //Login
         $this->manager->sendPacket($this->identifier, $buffer);
     } elseif ($this->status === 1) {
         $pid = Binary::readVarInt($buffer, $offset);
         if ($pid === 0x0) {
             $sample = [];
             foreach ($this->manager->sample as $id => $name) {
                 $sample[] = ["name" => $name, "id" => $id];
             }
             $data = ["version" => ["name" => Info::VERSION, "protocol" => Info::PROTOCOL], "players" => ["max" => $this->manager->getServerData()["MaxPlayers"], "online" => $this->manager->getServerData()["OnlinePlayers"], "sample" => $sample], "description" => json_decode(TextFormat::toJSON($this->manager->description))];
             if ($this->manager->favicon !== null) {
                 $data["favicon"] = $this->manager->favicon;
             }
             $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
             $data = Binary::writeVarInt(0x0) . Binary::writeVarInt(strlen($data)) . $data;
             $this->writeRaw($data);
         } elseif ($pid === 0x1) {
             $packet = new PingPacket();
             $packet->read($buffer, $offset);
             $this->writePacket($packet);
             $this->status = -1;
         }
     } elseif ($this->status === 0) {
         $pid = Binary::readVarInt($buffer, $offset);
         if ($pid === 0x0) {
             $protocol = Binary::readVarInt($buffer, $offset);
             $len = Binary::readVarInt($buffer, $offset);
             $hostname = substr($buffer, $offset, $len);
             $offset += $len;
             $serverPort = Binary::readShort(substr($buffer, $offset, 2));
             $offset += 2;
             $nextState = Binary::readVarInt($buffer, $offset);
             if ($nextState === 1) {
                 $this->status = 1;
             } elseif ($nextState === 2) {
                 $this->status = -1;
                 if ($protocol < Info::PROTOCOL) {
                     $packet = new LoginDisconnectPacket();
                     $packet->reson = TextFormat::toJSON(TextFormat::BOLD . "Outdated client!" . TextFormat::RESET . "\n\nPlease use " . Info::VERSION);
                     $this->writePacket($packet);
                 } elseif ($protocol > Info::PROTOCOL) {
                     $packet = new LoginDisconnectPacket();
                     $packet->reson = TextFormat::toJSON(TextFormat::BOLD . "Outdated server!" . TextFormat::RESET . "\n\nI'm using " . Info::VERSION);
                     $this->writePacket($packet);
                 } else {
                     $this->manager->openSession($this);
                     $this->status = 2;
                 }
             } else {
                 $this->close();
             }
         } else {
             $this->close("Unexpected packet {$pid}");
         }
     }
 }
Example #10
0
 public function serverToInterface(DesktopPlayer $player, DataPacket $packet)
 {
     switch ($packet->pid()) {
         case Info::UPDATE_BLOCK_PACKET:
             $pk = new BlockChangePacket();
             $pk->x = $packet->x;
             $pk->y = $packet->y;
             $pk->z = $packet->z;
             $pk->blockId = $packet->block;
             $pk->blockMeta = $packet->meta;
             return $pk;
         case Info::START_GAME_PACKET:
             $packets = [];
             $pk = new JoinGamePacket();
             $pk->eid = $packet->eid;
             $pk->gamemode = $player->getGamemode();
             $pk->dimension = 0;
             $pk->difficulty = $player->getServer()->getDifficulty();
             $pk->maxPlayers = $player->getServer()->getMaxPlayers();
             $pk->levelType = "default";
             $packets[] = $pk;
             $pk = new PlayerAbilitiesPacket();
             $pk->flyingSpeed = 0.05;
             $pk->walkingSpeed = 0.1;
             $pk->canFly = ($player->getGamemode() & 0x1) > 0;
             $pk->damageDisabled = ($player->getGamemode() & 0x1) > 0;
             $pk->isFlying = false;
             $pk->isCreative = ($player->getGamemode() & 0x1) > 0;
             if ($player->spawned === true) {
                 $packets = [$pk];
                 $pk = new ChangeGameStatePacket();
                 $pk->reason = 3;
                 $pk->value = $player->getGamemode();
                 $packets[] = $pk;
                 return $packets;
             } else {
                 $packets[] = $pk;
             }
             $pk = new SpawnPositionPacket();
             $pk->spawnX = $packet->spawnX;
             $pk->spawnY = $packet->spawnY;
             $pk->spawnZ = $packet->spawnZ;
             $packets[] = $pk;
             $pk = new PositionAndLookPacket();
             $pk->x = $packet->x;
             $pk->y = $packet->y;
             $pk->z = $packet->z;
             $pk->yaw = $player->yaw;
             $pk->pitch = $player->pitch;
             $pk->onGround = $player->isOnGround();
             $packets[] = $pk;
             return $packets;
         case Info::SET_HEALTH_PACKET:
             $pk = new UpdateHealthPacket();
             $pk->health = $packet->health;
             $pk->food = 20;
             $pk->saturation = 5;
             return $pk;
         case Info::TEXT_PACKET:
             $pk = new STCChatPacket();
             $pk->message = TextFormat::toJSON($packet->message);
             return $pk;
         case Info::SET_TIME_PACKET:
             $pk = new TimeUpdatePacket();
             $pk->age = $packet->time;
             $pk->time = $packet->time;
             //TODO: calculate offset from MCPE
             return $pk;
         case Info::SET_SPAWN_POSITION_PACKET:
             $pk = new SpawnPositionPacket();
             $pk->spawnX = $packet->x;
             $pk->spawnY = $packet->y;
             $pk->spawnZ = $packet->z;
             return $pk;
         case Info::REMOVE_ENTITY_PACKET:
         case Info::REMOVE_PLAYER_PACKET:
             $pk = new DestroyEntitiesPacket();
             $pk->ids[] = $packet->eid;
             return $pk;
         case Info::MOVE_PLAYER_PACKET:
             if ($packet->eid === 0) {
                 $pk = new PositionAndLookPacket();
                 $pk->x = $packet->x;
                 $pk->y = $packet->y;
                 $pk->z = $packet->z;
                 $pk->yaw = $packet->yaw;
                 $pk->pitch = $packet->pitch;
                 $pk->onGround = $player->isOnGround();
                 return $pk;
             } else {
                 $packets = [];
                 $pk = new EntityTeleportPacket();
                 $pk->eid = $packet->eid;
                 $pk->x = $packet->x;
                 $pk->y = $packet->y;
                 $pk->z = $packet->z;
                 $pk->yaw = $packet->yaw;
                 $pk->pitch = $packet->pitch;
                 $packets[] = $pk;
                 $pk = new EntityHeadLookPacket();
                 $pk->eid = $packet->eid;
                 $pk->yaw = $packet->yaw;
                 $packets[] = $pk;
                 return $packets;
             }
         case Info::MOVE_ENTITY_PACKET:
             $packets = [];
             foreach ($packet->entities as $d) {
                 $pk = new EntityTeleportPacket();
                 $pk->eid = $d[0];
                 $pk->x = $d[1];
                 $pk->y = $d[2];
                 $pk->z = $d[3];
                 $pk->yaw = $d[4];
                 $pk->pitch = $d[5];
                 $packets[] = $pk;
                 $pk = new EntityHeadLookPacket();
                 $pk->eid = $d[0];
                 $pk->yaw = $d[4];
                 $packets[] = $pk;
             }
             return $packets;
         case Info::SET_ENTITY_MOTION_PACKET:
             $packets = [];
             foreach ($packet->entities as $d) {
                 $pk = new EntityVelocityPacket();
                 $pk->eid = $d[0];
                 $pk->velocityX = $d[1];
                 $pk->velocityY = $d[2];
                 $pk->velocityZ = $d[3];
                 $packets[] = $pk;
             }
             return $packets;
             /*
             case Info::CONTAINER_CLOSE_PACKET:
             	$pk = new STCCloseWindowPacket();
             	$pk->windowID = $packet->windowid;
             	return $pk;
             
             case Info::CONTAINER_OPEN_PACKET:
             	$pk = new OpenWindowPacket();
             	$pk->windowID = $packet->windowid;
             	$pk->inventoryType = $packet->type;
             	$pk->windowTitle = "";
             	$pk->slots = $packet->slots;
             	return $pk;
             
             case Info::CONTAINER_SET_SLOT_PACKET:
             	$pk = new SetSlotPacket();
             	$pk->windowID = $packet->windowid;
             	if($pk->windowID === 0){
             		$pk->slot = $packet->slot + 9;
             	}elseif($pk->windowID === 0x78){
             		$pk->windowID = 0;
             		$pk->slot = $packet->slot + 5;
             	}else{
             		$pk->slot = $packet->slot;
             	}
             	$pk->item = $packet->item;
             	return $pk;
             
             case Info::CONTAINER_SET_CONTENT_PACKET:
             	$pk = new WindowItemsPacket();
             	$pk->windowID = $packet->windowid;
             	if($pk->windowID === 0 or $pk->windowID === 0x78){
             		$pk->windowID = 0;
             		for($i = 0; $i < 5; ++$i){
             			$pk->items[] = Item::get(Item::AIR, 0, 0);
             		}
             		$pk->items[] = $player->getInventory()->getHelmet();
             		$pk->items[] = $player->getInventory()->getChestplate();
             		$pk->items[] = $player->getInventory()->getLeggings();
             		$pk->items[] = $player->getInventory()->getBoots();
             		$slots = $player->getInventory()->getSize();
             		for($i = 0; $i < $slots; ++$i){
             			$pk->items[] = $player->getInventory()->getItem($i);
             		}
             	}else{
             		$pk->items = $packet->slots;
             	}
             
             	return $pk;
             */
         /*
         case Info::CONTAINER_CLOSE_PACKET:
         	$pk = new STCCloseWindowPacket();
         	$pk->windowID = $packet->windowid;
         	return $pk;
         
         case Info::CONTAINER_OPEN_PACKET:
         	$pk = new OpenWindowPacket();
         	$pk->windowID = $packet->windowid;
         	$pk->inventoryType = $packet->type;
         	$pk->windowTitle = "";
         	$pk->slots = $packet->slots;
         	return $pk;
         
         case Info::CONTAINER_SET_SLOT_PACKET:
         	$pk = new SetSlotPacket();
         	$pk->windowID = $packet->windowid;
         	if($pk->windowID === 0){
         		$pk->slot = $packet->slot + 9;
         	}elseif($pk->windowID === 0x78){
         		$pk->windowID = 0;
         		$pk->slot = $packet->slot + 5;
         	}else{
         		$pk->slot = $packet->slot;
         	}
         	$pk->item = $packet->item;
         	return $pk;
         
         case Info::CONTAINER_SET_CONTENT_PACKET:
         	$pk = new WindowItemsPacket();
         	$pk->windowID = $packet->windowid;
         	if($pk->windowID === 0 or $pk->windowID === 0x78){
         		$pk->windowID = 0;
         		for($i = 0; $i < 5; ++$i){
         			$pk->items[] = Item::get(Item::AIR, 0, 0);
         		}
         		$pk->items[] = $player->getInventory()->getHelmet();
         		$pk->items[] = $player->getInventory()->getChestplate();
         		$pk->items[] = $player->getInventory()->getLeggings();
         		$pk->items[] = $player->getInventory()->getBoots();
         		$slots = $player->getInventory()->getSize();
         		for($i = 0; $i < $slots; ++$i){
         			$pk->items[] = $player->getInventory()->getItem($i);
         		}
         	}else{
         		$pk->items = $packet->slots;
         	}
         
         	return $pk;
         */
         case Info::ADD_ITEM_ENTITY_PACKET:
             $packets = [];
             $pk = new SpawnObjectPacket();
             $pk->eid = $packet->eid;
             $pk->type = 2;
             $pk->x = $packet->x;
             $pk->y = $packet->y;
             $pk->z = $packet->z;
             $pk->yaw = $packet->yaw;
             $pk->pitch = $packet->pitch;
             $packets[] = $pk;
             $pk = new EntityMetadataPacket();
             $pk->eid = $packet->eid;
             $pk->metadata = $pk->metadata = [0 => ["type" => 0, "value" => 0], 10 => ["type" => 5, "value" => $packet->item]];
             $packets[] = $pk;
             return $packets;
         case Info::ADD_PLAYER_PACKET:
             $packets = [];
             $pk = new SpawnPlayerPacket();
             $pk->name = $packet->username;
             $pk->eid = $packet->eid;
             $pk->uuid = Binary::UUIDtoString("00000000000030008000000000000000");
             $pk->x = $packet->x;
             $pk->z = $packet->y;
             $pk->y = $packet->z;
             $pk->yaw = $packet->yaw;
             $pk->pitch = $packet->pitch;
             $pk->item = 0;
             $pk->metadata = $packet->metadata;
             $packets[] = $pk;
             $pk = new EntityTeleportPacket();
             $pk->eid = $packet->eid;
             $pk->x = $packet->x;
             $pk->y = $packet->y;
             $pk->z = $packet->z;
             $pk->yaw = $packet->yaw;
             $pk->pitch = $packet->pitch;
             $packets[] = $pk;
             return $packets;
         default:
             return null;
     }
 }