PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
CorrectPlayerMovePredictionPacket.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
20
22 public const NETWORK_ID = ProtocolInfo::CORRECT_PLAYER_MOVE_PREDICTION_PACKET;
23
24 public const PREDICTION_TYPE_PLAYER = 0;
25 public const PREDICTION_TYPE_VEHICLE = 1;
26
27 private Vector3 $position;
28 private Vector3 $delta;
29 private bool $onGround;
30 private int $tick;
31 private int $predictionType;
32 private ?Vector2 $vehicleRotation;
33 private ?float $vehicleAngularVelocity;
34
38 private static function internalCreate(
39 Vector3 $position,
40 Vector3 $delta,
41 bool $onGround,
42 int $tick,
43 int $predictionType,
44 ?Vector2 $vehicleRotation,
45 ?float $vehicleAngularVelocity,
46 ) : self{
47 $result = new self;
48 $result->position = $position;
49 $result->delta = $delta;
50 $result->onGround = $onGround;
51 $result->tick = $tick;
52 $result->predictionType = $predictionType;
53 $result->vehicleRotation = $vehicleRotation;
54 $result->vehicleAngularVelocity = $vehicleAngularVelocity;
55 return $result;
56 }
57
58 public static function create(Vector3 $position, Vector3 $delta, bool $onGround, int $tick, int $predictionType, ?Vector2 $vehicleRotation, ?float $vehicleAngularVelocity) : self{
59 if($predictionType === self::PREDICTION_TYPE_VEHICLE && $vehicleRotation === null){
60 throw new \LogicException("CorrectPlayerMovePredictionPackets with type VEHICLE require a vehicleRotation to be provided");
61 }
62
63 return self::internalCreate($position, $delta, $onGround, $tick, $predictionType, $vehicleRotation, $vehicleAngularVelocity);
64 }
65
66 public function getPosition() : Vector3{ return $this->position; }
67
68 public function getDelta() : Vector3{ return $this->delta; }
69
70 public function isOnGround() : bool{ return $this->onGround; }
71
72 public function getTick() : int{ return $this->tick; }
73
74 public function getPredictionType() : int{ return $this->predictionType; }
75
76 public function getVehicleRotation() : ?Vector2{ return $this->vehicleRotation; }
77
78 public function getVehicleAngularVelocity() : ?float{ return $this->vehicleAngularVelocity; }
79
80 protected function decodePayload(PacketSerializer $in) : void{
81 $this->predictionType = $in->getByte();
82 $this->position = $in->getVector3();
83 $this->delta = $in->getVector3();
84 if($this->predictionType === self::PREDICTION_TYPE_VEHICLE){
85 $this->vehicleRotation = new Vector2($in->getFloat(), $in->getFloat());
86 $this->vehicleAngularVelocity = $in->readOptional($in->getFloat(...));
87 }
88 $this->onGround = $in->getBool();
89 $this->tick = $in->getUnsignedVarLong();
90 }
91
92 protected function encodePayload(PacketSerializer $out) : void{
93 $out->putByte($this->predictionType);
94 $out->putVector3($this->position);
95 $out->putVector3($this->delta);
96 if($this->predictionType === self::PREDICTION_TYPE_VEHICLE){
97 if($this->vehicleRotation === null){ // this should never be the case
98 throw new \LogicException("CorrectPlayerMovePredictionPackets with type VEHICLE require a vehicleRotation to be provided");
99 }
100
101 $out->putFloat($this->vehicleRotation->getX());
102 $out->putFloat($this->vehicleRotation->getY());
103 $out->writeOptional($this->vehicleAngularVelocity, $out->putFloat(...));
104 }
105 $out->putBool($this->onGround);
106 $out->putUnsignedVarLong($this->tick);
107 }
108
109 public function handle(PacketHandlerInterface $handler) : bool{
110 return $handler->handleCorrectPlayerMovePrediction($this);
111 }
112}