1818use pmmp \encoding \ByteBufferReader ;
1919use pmmp \encoding \ByteBufferWriter ;
2020use pmmp \encoding \VarInt ;
21+ use pocketmine \network \mcpe \protocol \types \ArmorSlot ;
22+ use pocketmine \network \mcpe \protocol \types \ArmorSlotAndDamagePair ;
23+ use function count ;
2124
2225class PlayerArmorDamagePacket extends DataPacket implements ClientboundPacket{
2326 public const NETWORK_ID = ProtocolInfo::PLAYER_ARMOR_DAMAGE_PACKET ;
@@ -28,35 +31,23 @@ class PlayerArmorDamagePacket extends DataPacket implements ClientboundPacket{
2831 private const FLAG_FEET = 3 ;
2932 private const FLAG_BODY = 4 ;
3033
31- private ? int $ headSlotDamage ;
32- private ? int $ chestSlotDamage ;
33- private ? int $ legsSlotDamage ;
34- private ? int $ feetSlotDamage ;
35- private ? int $ bodySlotDamage ;
34+ /**
35+ * @var ArmorSlotAndDamagePair[]
36+ * @phpstan-var list<ArmorSlotAndDamagePair>
37+ */
38+ private array $ armorSlotAndDamagePairs = [] ;
3639
3740 /**
3841 * @generate-create-func
42+ * @param ArmorSlotAndDamagePair[] $armorSlotAndDamagePairs
43+ * @phpstan-param list<ArmorSlotAndDamagePair> $armorSlotAndDamagePairs
3944 */
40- public static function create (? int $ headSlotDamage , ? int $ chestSlotDamage , ? int $ legsSlotDamage , ? int $ feetSlotDamage , ? int $ bodySlotDamage ) : self {
45+ public static function create (array $ armorSlotAndDamagePairs ) : self {
4146 $ result = new self ;
42- $ result ->headSlotDamage = $ headSlotDamage ;
43- $ result ->chestSlotDamage = $ chestSlotDamage ;
44- $ result ->legsSlotDamage = $ legsSlotDamage ;
45- $ result ->feetSlotDamage = $ feetSlotDamage ;
46- $ result ->bodySlotDamage = $ bodySlotDamage ;
47+ $ result ->armorSlotAndDamagePairs = $ armorSlotAndDamagePairs ;
4748 return $ result ;
4849 }
4950
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-
6051 private function maybeReadDamage (int $ flags , int $ flag , ByteBufferReader $ in ) : ?int {
6152 if (($ flags & (1 << $ flag )) !== 0 ){
6253 return VarInt::readSignedInt ($ in );
@@ -65,14 +56,35 @@ private function maybeReadDamage(int $flags, int $flag, ByteBufferReader $in) :
6556 }
6657
6758 protected function decodePayload (ByteBufferReader $ in , int $ protocolId ) : 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- if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ){
75- $ this ->bodySlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_BODY , $ in );
59+ if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_110_OLD ){
60+ $ length = VarInt::readUnsignedInt ($ in );
61+ for ($ i = 0 ; $ i < $ length ; ++$ i ){
62+ $ this ->armorSlotAndDamagePairs [$ i ] = ArmorSlotAndDamagePair::read ($ in );
63+ }
64+ }else {
65+ $ flags = Byte::readUnsigned ($ in );
66+
67+ if (($ headSlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_HEAD , $ in )) !== null ){
68+ $ this ->armorSlotAndDamagePairs [] = new ArmorSlotAndDamagePair (ArmorSlot::HEAD , $ headSlotDamage );
69+ }
70+
71+ if (($ chestSlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_CHEST , $ in )) !== null ){
72+ $ this ->armorSlotAndDamagePairs [] = new ArmorSlotAndDamagePair (ArmorSlot::TORSO , $ chestSlotDamage );
73+ }
74+
75+ if (($ legsSlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_LEGS , $ in )) !== null ){
76+ $ this ->armorSlotAndDamagePairs [] = new ArmorSlotAndDamagePair (ArmorSlot::LEGS , $ legsSlotDamage );
77+ }
78+
79+ if (($ feetSlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_FEET , $ in )) !== null ){
80+ $ this ->armorSlotAndDamagePairs [] = new ArmorSlotAndDamagePair (ArmorSlot::FEET , $ feetSlotDamage );
81+ }
82+
83+ if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ){
84+ if (($ bodySlotDamage = $ this ->maybeReadDamage ($ flags , self ::FLAG_BODY , $ in )) !== null ){
85+ $ this ->armorSlotAndDamagePairs [] = new ArmorSlotAndDamagePair (ArmorSlot::BODY , $ bodySlotDamage );
86+ }
87+ }
7688 }
7789 }
7890
@@ -86,21 +98,45 @@ private function maybeWriteDamage(?int $field, ByteBufferWriter $out) : void{
8698 }
8799 }
88100
101+ private function getDamageBySlot (ArmorSlot $ slot ) : ?int {
102+ foreach ($ this ->armorSlotAndDamagePairs as $ pair ){
103+ if ($ pair ->getSlot () === $ slot ){
104+ return $ pair ->getDamage ();
105+ }
106+ }
107+
108+ return null ;
109+ }
110+
89111 protected function encodePayload (ByteBufferWriter $ out , int $ protocolId ) : void {
90- Byte::writeUnsigned ($ out ,
91- $ this ->composeFlag ($ this ->headSlotDamage , self ::FLAG_HEAD ) |
92- $ this ->composeFlag ($ this ->chestSlotDamage , self ::FLAG_CHEST ) |
93- $ this ->composeFlag ($ this ->legsSlotDamage , self ::FLAG_LEGS ) |
94- $ this ->composeFlag ($ this ->feetSlotDamage , self ::FLAG_FEET ) |
95- ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ? $ this ->composeFlag ($ this ->bodySlotDamage , self ::FLAG_BODY ) : 0 )
96- );
97-
98- $ this ->maybeWriteDamage ($ this ->headSlotDamage , $ out );
99- $ this ->maybeWriteDamage ($ this ->chestSlotDamage , $ out );
100- $ this ->maybeWriteDamage ($ this ->legsSlotDamage , $ out );
101- $ this ->maybeWriteDamage ($ this ->feetSlotDamage , $ out );
102- if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ){
103- $ this ->maybeWriteDamage ($ this ->bodySlotDamage , $ out );
112+ if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_110_OLD ){
113+ VarInt::writeUnsignedInt ($ out , count ($ this ->armorSlotAndDamagePairs ));
114+ foreach ($ this ->armorSlotAndDamagePairs as $ pair ){
115+ $ pair ->write ($ out );
116+ }
117+ }else {
118+ $ headSlotDamage = $ this ->getDamageBySlot (ArmorSlot::HEAD );
119+ $ chestSlotDamage = $ this ->getDamageBySlot (ArmorSlot::TORSO );
120+ $ legsSlotDamage = $ this ->getDamageBySlot (ArmorSlot::LEGS );
121+ $ feetSlotDamage = $ this ->getDamageBySlot (ArmorSlot::FEET );
122+ $ bodySlotDamage = $ this ->getDamageBySlot (ArmorSlot::BODY );
123+
124+ Byte::writeUnsigned (
125+ $ out ,
126+ $ this ->composeFlag ($ headSlotDamage , self ::FLAG_HEAD ) |
127+ $ this ->composeFlag ($ chestSlotDamage , self ::FLAG_CHEST ) |
128+ $ this ->composeFlag ($ legsSlotDamage , self ::FLAG_LEGS ) |
129+ $ this ->composeFlag ($ feetSlotDamage , self ::FLAG_FEET ) |
130+ ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ? $ this ->composeFlag ($ bodySlotDamage , self ::FLAG_BODY ) : 0 )
131+ );
132+
133+ $ this ->maybeWriteDamage ($ headSlotDamage , $ out );
134+ $ this ->maybeWriteDamage ($ chestSlotDamage , $ out );
135+ $ this ->maybeWriteDamage ($ legsSlotDamage , $ out );
136+ $ this ->maybeWriteDamage ($ feetSlotDamage , $ out );
137+ if ($ protocolId >= ProtocolInfo::PROTOCOL_1_21_20 ){
138+ $ this ->maybeWriteDamage ($ bodySlotDamage , $ out );
139+ }
104140 }
105141 }
106142
0 commit comments