PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
PlayerArmorDamagePacket.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
18
20 public const NETWORK_ID = ProtocolInfo::PLAYER_ARMOR_DAMAGE_PACKET;
21
22 private const FLAG_HEAD = 0;
23 private const FLAG_CHEST = 1;
24 private const FLAG_LEGS = 2;
25 private const FLAG_FEET = 3;
26 private const FLAG_BODY = 4;
27
28 private ?int $headSlotDamage;
29 private ?int $chestSlotDamage;
30 private ?int $legsSlotDamage;
31 private ?int $feetSlotDamage;
32 private ?int $bodySlotDamage;
33
37 public static function create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage, ?int $bodySlotDamage) : self{
38 $result = new self;
39 $result->headSlotDamage = $headSlotDamage;
40 $result->chestSlotDamage = $chestSlotDamage;
41 $result->legsSlotDamage = $legsSlotDamage;
42 $result->feetSlotDamage = $feetSlotDamage;
43 $result->bodySlotDamage = $bodySlotDamage;
44 return $result;
45 }
46
47 public function getHeadSlotDamage() : ?int{ return $this->headSlotDamage; }
48
49 public function getChestSlotDamage() : ?int{ return $this->chestSlotDamage; }
50
51 public function getLegsSlotDamage() : ?int{ return $this->legsSlotDamage; }
52
53 public function getFeetSlotDamage() : ?int{ return $this->feetSlotDamage; }
54
55 public function getBodySlotDamage() : ?int{ return $this->bodySlotDamage; }
56
57 private function maybeReadDamage(int $flags, int $flag, PacketSerializer $in) : ?int{
58 if(($flags & (1 << $flag)) !== 0){
59 return $in->getVarInt();
60 }
61 return null;
62 }
63
64 protected function decodePayload(PacketSerializer $in) : void{
65 $flags = $in->getByte();
66
67 $this->headSlotDamage = $this->maybeReadDamage($flags, self::FLAG_HEAD, $in);
68 $this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in);
69 $this->legsSlotDamage = $this->maybeReadDamage($flags, self::FLAG_LEGS, $in);
70 $this->feetSlotDamage = $this->maybeReadDamage($flags, self::FLAG_FEET, $in);
71 $this->bodySlotDamage = $this->maybeReadDamage($flags, self::FLAG_BODY, $in);
72 }
73
74 private function composeFlag(?int $field, int $flag) : int{
75 return $field !== null ? (1 << $flag) : 0;
76 }
77
78 private function maybeWriteDamage(?int $field, PacketSerializer $out) : void{
79 if($field !== null){
80 $out->putVarInt($field);
81 }
82 }
83
84 protected function encodePayload(PacketSerializer $out) : void{
85 $out->putByte(
86 $this->composeFlag($this->headSlotDamage, self::FLAG_HEAD) |
87 $this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) |
88 $this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) |
89 $this->composeFlag($this->feetSlotDamage, self::FLAG_FEET) |
90 $this->composeFlag($this->bodySlotDamage, self::FLAG_BODY)
91 );
92
93 $this->maybeWriteDamage($this->headSlotDamage, $out);
94 $this->maybeWriteDamage($this->chestSlotDamage, $out);
95 $this->maybeWriteDamage($this->legsSlotDamage, $out);
96 $this->maybeWriteDamage($this->feetSlotDamage, $out);
97 $this->maybeWriteDamage($this->bodySlotDamage, $out);
98 }
99
100 public function handle(PacketHandlerInterface $handler) : bool{
101 return $handler->handlePlayerArmorDamage($this);
102 }
103}
static create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage, ?int $bodySlotDamage)