PocketMine-MP 5.27.1 git-9af3cde03fabbe4129c79e46dc87ffa0fff446e6
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
18use pocketmine\network\mcpe\protocol\types\OverrideUpdateType;
19
21 public const NETWORK_ID = ProtocolInfo::PLAYER_UPDATE_ENTITY_OVERRIDES_PACKET;
22
23 private int $actorRuntimeId;
24 private int $propertyIndex;
25 private OverrideUpdateType $updateType;
26 private ?int $intOverrideValue;
27 private ?float $floatOverrideValue;
28
32 private static function create(int $actorRuntimeId, int $propertyIndex, OverrideUpdateType $updateType, ?int $intOverrideValue, ?float $floatOverrideValue) : self{
33 $result = new self;
34 $result->actorRuntimeId = $actorRuntimeId;
35 $result->propertyIndex = $propertyIndex;
36 $result->updateType = $updateType;
37 $result->intOverrideValue = $intOverrideValue;
38 $result->floatOverrideValue = $floatOverrideValue;
39 return $result;
40 }
41
42 public static function createIntOverride(int $actorRuntimeId, int $propertyIndex, int $value) : self{
43 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::SET_INT_OVERRIDE, $value, null);
44 }
45
46 public static function createFloatOverride(int $actorRuntimeId, int $propertyIndex, float $value) : self{
47 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::SET_FLOAT_OVERRIDE, null, $value);
48 }
49
50 public static function createClearOverrides(int $actorRuntimeId, int $propertyIndex) : self{
51 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::CLEAR_OVERRIDES, null, null);
52 }
53
54 public static function createRemoveOverride(int $actorRuntimeId, int $propertyIndex) : self{
55 return self::create($actorRuntimeId, $propertyIndex, OverrideUpdateType::REMOVE_OVERRIDE, null, null);
56 }
57
58 public function getActorRuntimeId() : int{ return $this->actorRuntimeId; }
59
60 public function getPropertyIndex() : int{ return $this->propertyIndex; }
61
62 public function getUpdateType() : OverrideUpdateType{ return $this->updateType; }
63
64 public function getIntOverrideValue() : ?int{ return $this->intOverrideValue; }
65
66 public function getFloatOverrideValue() : ?float{ return $this->floatOverrideValue; }
67
68 protected function decodePayload(PacketSerializer $in) : void{
69 $this->actorRuntimeId = $in->getActorRuntimeId();
70 $this->propertyIndex = $in->getUnsignedVarInt();
71 $this->updateType = OverrideUpdateType::fromPacket($in->getByte());
72 if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){
73 $this->intOverrideValue = $in->getLInt();
74 }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){
75 $this->floatOverrideValue = $in->getLFloat();
76 }
77 }
78
79 protected function encodePayload(PacketSerializer $out) : void{
80 $out->putActorRuntimeId($this->actorRuntimeId);
81 $out->putUnsignedVarInt($this->propertyIndex);
82 $out->putByte($this->updateType->value);
83 if($this->updateType === OverrideUpdateType::SET_INT_OVERRIDE){
84 if($this->intOverrideValue === null){ // this should never be the case
85 throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_INT_OVERRIDE require an intOverrideValue to be provided");
86 }
87 $out->putLInt($this->intOverrideValue);
88 }elseif($this->updateType === OverrideUpdateType::SET_FLOAT_OVERRIDE){
89 if($this->floatOverrideValue === null){ // this should never be the case
90 throw new \LogicException("PlayerUpdateEntityOverridesPacket with type SET_INT_OVERRIDE require an intOverrideValue to be provided");
91 }
92 $out->putLFloat($this->floatOverrideValue);
93 }
94 }
95
96 public function handle(PacketHandlerInterface $handler) : bool{
97 return $handler->handlePlayerUpdateEntityOverridesPacket($this);
98 }
99}