PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
22use pocketmine\network\mcpe\protocol\types\PlayerLocationType;
23
25 public const NETWORK_ID = ProtocolInfo::PLAYER_LOCATION_PACKET;
26
27 private PlayerLocationType $type;
28 private int $actorUniqueId;
29 private ?Vector3 $position;
30
34 private static function create(PlayerLocationType $type, int $actorUniqueId, ?Vector3 $position) : self{
35 $result = new self;
36 $result->type = $type;
37 $result->actorUniqueId = $actorUniqueId;
38 $result->position = $position;
39 return $result;
40 }
41
42 public static function createCoordinates(int $actorUniqueId, Vector3 $position) : self{
43 return self::create(PlayerLocationType::PLAYER_LOCATION_COORDINATES, $actorUniqueId, $position);
44 }
45
46 public static function createHide(int $actorUniqueId) : self{
47 return self::create(PlayerLocationType::PLAYER_LOCATION_HIDE, $actorUniqueId, null);
48 }
49
50 public function getType() : PlayerLocationType{ return $this->type; }
51
52 public function getActorUniqueId() : int{ return $this->actorUniqueId; }
53
54 public function getPosition() : ?Vector3{ return $this->position; }
55
56 protected function decodePayload(ByteBufferReader $in) : void{
57 $this->type = PlayerLocationType::fromPacket(LE::readUnsignedInt($in));
58 $this->actorUniqueId = CommonTypes::getActorUniqueId($in);
59
60 if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){
61 $this->position = CommonTypes::getVector3($in);
62 }
63 }
64
65 protected function encodePayload(ByteBufferWriter $out) : void{
66 LE::writeUnsignedInt($out, $this->type->value);
67 CommonTypes::putActorUniqueId($out, $this->actorUniqueId);
68
69 if($this->type === PlayerLocationType::PLAYER_LOCATION_COORDINATES){
70 if($this->position === null){ // this should never be the case
71 throw new \LogicException("PlayerLocationPacket with type PLAYER_LOCATION_COORDINATES require a position to be provided");
72 }
73 CommonTypes::putVector3($out, $this->position);
74 }
75 }
76
77 public function handle(PacketHandlerInterface $handler) : bool{
78 return $handler->handlePlayerLocation($this);
79 }
80}