PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
Loading...
Searching...
No Matches
PlayerLocationPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
19use pocketmine\network\mcpe\protocol\types\PlayerLocationType;
20
22 public const NETWORK_ID = ProtocolInfo::PLAYER_LOCATION_PACKET;
23
24 private PlayerLocationType $type;
25 private int $actorUniqueId;
26 private ?Vector3 $position;
27
31 private static function create(PlayerLocationType $type, int $actorUniqueId, ?Vector3 $position) : self{
32 $result = new self;
33 $result->type = $type;
34 $result->actorUniqueId = $actorUniqueId;
35 $result->position = $position;
36 return $result;
37 }
38
39 public static function createCoordinates(int $actorUniqueId, Vector3 $position) : self{
40 return self::create(PlayerLocationType::PLAYER_LOCATION_COORDINATES, $actorUniqueId, $position);
41 }
42
43 public static function createHide(int $actorUniqueId) : self{
44 return self::create(PlayerLocationType::PLAYER_LOCATION_HIDE, $actorUniqueId, null);
45 }
46
47 public function getType() : PlayerLocationType{ return $this->type; }
48
49 public function getActorUniqueId() : int{ return $this->actorUniqueId; }
50
51 public function getPosition() : ?Vector3{ return $this->position; }
52
53 protected function decodePayload(PacketSerializer $in) : void{
54 $this->type = PlayerLocationType::fromPacket($in->getLInt());
55 $this->actorUniqueId = $in->getActorUniqueId();
56
57 if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){
58 $this->position = $in->getVector3();
59 }
60 }
61
62 protected function encodePayload(PacketSerializer $out) : void{
63 $out->putLInt($this->type->value);
64 $out->putActorUniqueId($this->actorUniqueId);
65
66 if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){
67 if($this->position === null){ // this should never be the case
68 throw new \LogicException("PlayerLocationPacket with type PLAYER_LOCATION_COORDINATES require a position to be provided");
69 }
70 $out->putVector3($this->position);
71 }
72 }
73
74 public function handle(PacketHandlerInterface $handler) : bool{
75 return $handler->handlePlayerLocation($this);
76 }
77}