PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
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
27 private ?int $headSlotDamage;
28 private ?int $chestSlotDamage;
29 private ?int $legsSlotDamage;
30 private ?int $feetSlotDamage;
31
35 public static function create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage) : self{
36 $result = new self;
37 $result->headSlotDamage = $headSlotDamage;
38 $result->chestSlotDamage = $chestSlotDamage;
39 $result->legsSlotDamage = $legsSlotDamage;
40 $result->feetSlotDamage = $feetSlotDamage;
41 return $result;
42 }
43
44 public function getHeadSlotDamage() : ?int{ return $this->headSlotDamage; }
45
46 public function getChestSlotDamage() : ?int{ return $this->chestSlotDamage; }
47
48 public function getLegsSlotDamage() : ?int{ return $this->legsSlotDamage; }
49
50 public function getFeetSlotDamage() : ?int{ return $this->feetSlotDamage; }
51
52 private function maybeReadDamage(int $flags, int $flag, PacketSerializer $in) : ?int{
53 if(($flags & (1 << $flag)) !== 0){
54 return $in->getVarInt();
55 }
56 return null;
57 }
58
59 protected function decodePayload(PacketSerializer $in) : void{
60 $flags = $in->getByte();
61
62 $this->headSlotDamage = $this->maybeReadDamage($flags, self::FLAG_HEAD, $in);
63 $this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in);
64 $this->legsSlotDamage = $this->maybeReadDamage($flags, self::FLAG_LEGS, $in);
65 $this->feetSlotDamage = $this->maybeReadDamage($flags, self::FLAG_FEET, $in);
66 }
67
68 private function composeFlag(?int $field, int $flag) : int{
69 return $field !== null ? (1 << $flag) : 0;
70 }
71
72 private function maybeWriteDamage(?int $field, PacketSerializer $out) : void{
73 if($field !== null){
74 $out->putVarInt($field);
75 }
76 }
77
78 protected function encodePayload(PacketSerializer $out) : void{
79 $out->putByte(
80 $this->composeFlag($this->headSlotDamage, self::FLAG_HEAD) |
81 $this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) |
82 $this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) |
83 $this->composeFlag($this->feetSlotDamage, self::FLAG_FEET)
84 );
85
86 $this->maybeWriteDamage($this->headSlotDamage, $out);
87 $this->maybeWriteDamage($this->chestSlotDamage, $out);
88 $this->maybeWriteDamage($this->legsSlotDamage, $out);
89 $this->maybeWriteDamage($this->feetSlotDamage, $out);
90 }
91
92 public function handle(PacketHandlerInterface $handler) : bool{
93 return $handler->handlePlayerArmorDamage($this);
94 }
95}
static create(?int $headSlotDamage, ?int $chestSlotDamage, ?int $legsSlotDamage, ?int $feetSlotDamage)