PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
MoveActorDeltaPacket.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\DataDecodeException;
20use pmmp\encoding\LE;
22
24 public const NETWORK_ID = ProtocolInfo::MOVE_ACTOR_DELTA_PACKET;
25
26 public const FLAG_HAS_X = 0x01;
27 public const FLAG_HAS_Y = 0x02;
28 public const FLAG_HAS_Z = 0x04;
29 public const FLAG_HAS_PITCH = 0x08;
30 public const FLAG_HAS_YAW = 0x10;
31 public const FLAG_HAS_HEAD_YAW = 0x20;
32 public const FLAG_GROUND = 0x40;
33 public const FLAG_TELEPORT = 0x80;
34 public const FLAG_FORCE_MOVE_LOCAL_ENTITY = 0x100;
35
36 public int $actorRuntimeId;
37 public int $flags;
38 public float $xPos = 0;
39 public float $yPos = 0;
40 public float $zPos = 0;
41 public float $pitch = 0.0;
42 public float $yaw = 0.0;
43 public float $headYaw = 0.0;
44
46 private function maybeReadCoord(int $flag, ByteBufferReader $in) : float{
47 if(($this->flags & $flag) !== 0){
48 return LE::readFloat($in);
49 }
50 return 0;
51 }
52
54 private function maybeReadRotation(int $flag, ByteBufferReader $in) : float{
55 if(($this->flags & $flag) !== 0){
56 return CommonTypes::getRotationByte($in);
57 }
58 return 0.0;
59 }
60
61 protected function decodePayload(ByteBufferReader $in) : void{
62 $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
63 $this->flags = LE::readUnsignedShort($in);
64 $this->xPos = $this->maybeReadCoord(self::FLAG_HAS_X, $in);
65 $this->yPos = $this->maybeReadCoord(self::FLAG_HAS_Y, $in);
66 $this->zPos = $this->maybeReadCoord(self::FLAG_HAS_Z, $in);
67 $this->pitch = $this->maybeReadRotation(self::FLAG_HAS_PITCH, $in);
68 $this->yaw = $this->maybeReadRotation(self::FLAG_HAS_YAW, $in);
69 $this->headYaw = $this->maybeReadRotation(self::FLAG_HAS_HEAD_YAW, $in);
70 }
71
72 private function maybeWriteCoord(int $flag, float $val, ByteBufferWriter $out) : void{
73 if(($this->flags & $flag) !== 0){
74 LE::writeFloat($out, $val);
75 }
76 }
77
78 private function maybeWriteRotation(int $flag, float $val, ByteBufferWriter $out) : void{
79 if(($this->flags & $flag) !== 0){
80 CommonTypes::putRotationByte($out, $val);
81 }
82 }
83
84 protected function encodePayload(ByteBufferWriter $out) : void{
85 CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
86 LE::writeUnsignedShort($out, $this->flags);
87 $this->maybeWriteCoord(self::FLAG_HAS_X, $this->xPos, $out);
88 $this->maybeWriteCoord(self::FLAG_HAS_Y, $this->yPos, $out);
89 $this->maybeWriteCoord(self::FLAG_HAS_Z, $this->zPos, $out);
90 $this->maybeWriteRotation(self::FLAG_HAS_PITCH, $this->pitch, $out);
91 $this->maybeWriteRotation(self::FLAG_HAS_YAW, $this->yaw, $out);
92 $this->maybeWriteRotation(self::FLAG_HAS_HEAD_YAW, $this->headYaw, $out);
93 }
94
95 public function handle(PacketHandlerInterface $handler) : bool{
96 return $handler->handleMoveActorDelta($this);
97 }
98}