PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
PlayerUpdateEntityOverridesPacket.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\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
21use pmmp\encoding\VarInt;
23use pocketmine\network\mcpe\protocol\types\OverrideUpdateType;
24
26 public const NETWORK_ID = ProtocolInfo::PLAYER_UPDATE_ENTITY_OVERRIDES_PACKET;
27
28 private int $actorRuntimeId;
29 private int $propertyIndex;
30 private OverrideUpdateType $updateType;
31 private ?int $intOverrideValue;
32 private ?float $floatOverrideValue;
33
37 private static function create(int $actorRuntimeId, int $propertyIndex, OverrideUpdateType $updateType, ?int $intOverrideValue, ?float $floatOverrideValue) : self{
38 $result = new self;
39 $result->actorRuntimeId = $actorRuntimeId;
40 $result->propertyIndex = $propertyIndex;
41 $result->updateType = $updateType;
42 $result->intOverrideValue = $intOverrideValue;
43 $result->floatOverrideValue = $floatOverrideValue;
44 return $result;
45 }
46
47 public static function createIntOverride(int $actorRuntimeId, int $propertyIndex, int $value) : self{
48 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::SET_INT_OVERRIDE, $value, null);
49 }
50
51 public static function createFloatOverride(int $actorRuntimeId, int $propertyIndex, float $value) : self{
52 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::SET_FLOAT_OVERRIDE, null, $value);
53 }
54
55 public static function createClearOverrides(int $actorRuntimeId, int $propertyIndex) : self{
56 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::CLEAR_OVERRIDES, null, null);
57 }
58
59 public static function createRemoveOverride(int $actorRuntimeId, int $propertyIndex) : self{
60 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::REMOVE_OVERRIDE, null, null);
61 }
62
63 public function getActorRuntimeId() : int{ return $this->actorRuntimeId; }
64
65 public function getPropertyIndex() : int{ return $this->propertyIndex; }
66
67 public function getUpdateType() : OverrideUpdateType{ return $this->updateType; }
68
69 public function getIntOverrideValue() : ?int{ return $this->intOverrideValue; }
70
71 public function getFloatOverrideValue() : ?float{ return $this->floatOverrideValue; }
72
73 protected function decodePayload(ByteBufferReader $in) : void{
74 $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
75 $this->propertyIndex = VarInt::readUnsignedInt($in);
76 $this->updateType = OverrideUpdateType::fromPacket(Byte::readUnsigned($in));
77 if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){
78 $this->intOverrideValue = LE::readSignedInt($in);
79 }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){
80 $this->floatOverrideValue = LE::readFloat($in);
81 }
82 }
83
84 protected function encodePayload(ByteBufferWriter $out) : void{
85 CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
86 VarInt::writeUnsignedInt($out, $this->propertyIndex);
87 Byte::writeUnsigned($out, $this->updateType->value);
88 if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){
89 if($this->intOverrideValue === null){ // this should never be the case
90 throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_INT_OVERRIDE requires intOverrideValue to be provided");
91 }
92 LE::writeSignedInt($out, $this->intOverrideValue);
93 }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){
94 if($this->floatOverrideValue === null){ // this should never be the case
95 throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_FLOAT_OVERRIDE requires floatOverrideValue to be provided");
96 }
97 LE::writeFloat($out, $this->floatOverrideValue);
98 }
99 }
100
101 public function handle(PacketHandlerInterface $handler) : bool{
102 return $handler->handlePlayerUpdateEntityOverrides($this);
103 }
104}