|
14 | 14 |
|
15 | 15 | namespace pocketmine\network\mcpe\protocol; |
16 | 16 |
|
17 | | -use pmmp\encoding\Byte; |
18 | 17 | use pmmp\encoding\ByteBufferReader; |
19 | 18 | use pmmp\encoding\ByteBufferWriter; |
20 | 19 | use pmmp\encoding\VarInt; |
| 20 | +use pocketmine\network\mcpe\protocol\types\ArmorSlotAndDamagePair; |
| 21 | +use function count; |
21 | 22 |
|
22 | 23 | class PlayerArmorDamagePacket extends DataPacket implements ClientboundPacket{ |
23 | 24 | public const NETWORK_ID = ProtocolInfo::PLAYER_ARMOR_DAMAGE_PACKET; |
24 | 25 |
|
25 | | - private const FLAG_HEAD = 0; |
26 | | - private const FLAG_CHEST = 1; |
27 | | - private const FLAG_LEGS = 2; |
28 | | - private const FLAG_FEET = 3; |
29 | | - private const FLAG_BODY = 4; |
30 | | - |
31 | | - private ?int $headSlotDamage; |
32 | | - private ?int $chestSlotDamage; |
33 | | - private ?int $legsSlotDamage; |
34 | | - private ?int $feetSlotDamage; |
35 | | - private ?int $bodySlotDamage; |
| 26 | + /** |
| 27 | + * @var ArmorSlotAndDamagePair[] |
| 28 | + * @phpstan-var list<ArmorSlotAndDamagePair> |
| 29 | + */ |
| 30 | + private array $armorSlotAndDamagePairs = []; |
36 | 31 |
|
37 | 32 | /** |
38 | 33 | * @generate-create-func |
| 34 | + * @param ArmorSlotAndDamagePair[] $armorSlotAndDamagePairs |
| 35 | + * @phpstan-param list<ArmorSlotAndDamagePair> $armorSlotAndDamagePairs |
39 | 36 | */ |
40 | | - public static function create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage, ?int $bodySlotDamage) : self{ |
| 37 | + public static function create(array $armorSlotAndDamagePairs) : self{ |
41 | 38 | $result = new self; |
42 | | - $result->headSlotDamage = $headSlotDamage; |
43 | | - $result->chestSlotDamage = $chestSlotDamage; |
44 | | - $result->legsSlotDamage = $legsSlotDamage; |
45 | | - $result->feetSlotDamage = $feetSlotDamage; |
46 | | - $result->bodySlotDamage = $bodySlotDamage; |
| 39 | + $result->armorSlotAndDamagePairs = $armorSlotAndDamagePairs; |
47 | 40 | return $result; |
48 | 41 | } |
49 | 42 |
|
50 | | - public function getHeadSlotDamage() : ?int{ return $this->headSlotDamage; } |
51 | | - |
52 | | - public function getChestSlotDamage() : ?int{ return $this->chestSlotDamage; } |
53 | | - |
54 | | - public function getLegsSlotDamage() : ?int{ return $this->legsSlotDamage; } |
55 | | - |
56 | | - public function getFeetSlotDamage() : ?int{ return $this->feetSlotDamage; } |
57 | | - |
58 | | - public function getBodySlotDamage() : ?int{ return $this->bodySlotDamage; } |
59 | | - |
60 | | - private function maybeReadDamage(int $flags, int $flag, ByteBufferReader $in) : ?int{ |
61 | | - if(($flags & (1 << $flag)) !== 0){ |
62 | | - return VarInt::readSignedInt($in); |
63 | | - } |
64 | | - return null; |
65 | | - } |
66 | | - |
67 | 43 | protected function decodePayload(ByteBufferReader $in) : void{ |
68 | | - $flags = Byte::readUnsigned($in); |
69 | | - |
70 | | - $this->headSlotDamage = $this->maybeReadDamage($flags, self::FLAG_HEAD, $in); |
71 | | - $this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in); |
72 | | - $this->legsSlotDamage = $this->maybeReadDamage($flags, self::FLAG_LEGS, $in); |
73 | | - $this->feetSlotDamage = $this->maybeReadDamage($flags, self::FLAG_FEET, $in); |
74 | | - $this->bodySlotDamage = $this->maybeReadDamage($flags, self::FLAG_BODY, $in); |
75 | | - } |
76 | | - |
77 | | - private function composeFlag(?int $field, int $flag) : int{ |
78 | | - return $field !== null ? (1 << $flag) : 0; |
79 | | - } |
80 | | - |
81 | | - private function maybeWriteDamage(?int $field, ByteBufferWriter $out) : void{ |
82 | | - if($field !== null){ |
83 | | - VarInt::writeSignedInt($out, $field); |
| 44 | + for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){ |
| 45 | + $this->armorSlotAndDamagePairs[] = ArmorSlotAndDamagePair::read($in); |
84 | 46 | } |
85 | 47 | } |
86 | 48 |
|
87 | 49 | protected function encodePayload(ByteBufferWriter $out) : void{ |
88 | | - Byte::writeUnsigned($out, |
89 | | - $this->composeFlag($this->headSlotDamage, self::FLAG_HEAD) | |
90 | | - $this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) | |
91 | | - $this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) | |
92 | | - $this->composeFlag($this->feetSlotDamage, self::FLAG_FEET) | |
93 | | - $this->composeFlag($this->bodySlotDamage, self::FLAG_BODY) |
94 | | - ); |
95 | | - |
96 | | - $this->maybeWriteDamage($this->headSlotDamage, $out); |
97 | | - $this->maybeWriteDamage($this->chestSlotDamage, $out); |
98 | | - $this->maybeWriteDamage($this->legsSlotDamage, $out); |
99 | | - $this->maybeWriteDamage($this->feetSlotDamage, $out); |
100 | | - $this->maybeWriteDamage($this->bodySlotDamage, $out); |
| 50 | + VarInt::writeUnsignedInt($out, count($this->armorSlotAndDamagePairs)); |
| 51 | + foreach($this->armorSlotAndDamagePairs as $pair){ |
| 52 | + $pair->write($out); |
| 53 | + } |
101 | 54 | } |
102 | 55 |
|
103 | 56 | public function handle(PacketHandlerInterface $handler) : bool{ |
|
0 commit comments