PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
BossEventPacket.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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
23
25 public const NETWORK_ID = ProtocolInfo::BOSS_EVENT_PACKET;
26
28 public const TYPE_SHOW = 0;
30 public const TYPE_REGISTER_PLAYER = 1;
32 public const TYPE_HIDE = 2;
34 public const TYPE_UNREGISTER_PLAYER = 3;
36 public const TYPE_HEALTH_PERCENT = 4;
38 public const TYPE_TITLE = 5;
40 public const TYPE_PROPERTIES = 6;
42 public const TYPE_TEXTURE = 7;
44 public const TYPE_QUERY = 8;
45
46 public int $bossActorUniqueId;
47 public int $eventType;
48
49 public int $playerActorUniqueId;
50 public float $healthPercent;
51 public string $title;
52 public string $filteredTitle;
53 public bool $darkenScreen;
54 public int $color;
55 public int $overlay;
56
57 private static function base(int $bossActorUniqueId, int $eventId) : self{
58 $result = new self;
59 $result->bossActorUniqueId = $bossActorUniqueId;
60 $result->eventType = $eventId;
61 return $result;
62 }
63
64 public static function show(int $bossActorUniqueId, string $title, float $healthPercent, bool $darkenScreen = false, int $color = BossBarColor::PURPLE, int $overlay = 0) : self{
65 $result = self::base($bossActorUniqueId, self::TYPE_SHOW);
66 $result->title = $title;
67 $result->filteredTitle = $title;
68 $result->healthPercent = $healthPercent;
69 $result->darkenScreen = $darkenScreen;
70 $result->color = $color;
71 $result->overlay = $overlay;
72 return $result;
73 }
74
75 public static function hide(int $bossActorUniqueId) : self{
76 return self::base($bossActorUniqueId, self::TYPE_HIDE);
77 }
78
79 public static function registerPlayer(int $bossActorUniqueId, int $playerActorUniqueId) : self{
80 $result = self::base($bossActorUniqueId, self::TYPE_REGISTER_PLAYER);
81 $result->playerActorUniqueId = $playerActorUniqueId;
82 return $result;
83 }
84
85 public static function unregisterPlayer(int $bossActorUniqueId, int $playerActorUniqueId) : self{
86 $result = self::base($bossActorUniqueId, self::TYPE_UNREGISTER_PLAYER);
87 $result->playerActorUniqueId = $playerActorUniqueId;
88 return $result;
89 }
90
91 public static function healthPercent(int $bossActorUniqueId, float $healthPercent) : self{
92 $result = self::base($bossActorUniqueId, self::TYPE_HEALTH_PERCENT);
93 $result->healthPercent = $healthPercent;
94 return $result;
95 }
96
97 public static function title(int $bossActorUniqueId, string $title) : self{
98 $result = self::base($bossActorUniqueId, self::TYPE_TITLE);
99 $result->title = $title;
100 $result->filteredTitle = $title;
101 return $result;
102 }
103
104 public static function properties(int $bossActorUniqueId, bool $darkenScreen, int $color = BossBarColor::PURPLE, int $overlay = 0) : self{
105 $result = self::base($bossActorUniqueId, self::TYPE_PROPERTIES);
106 $result->darkenScreen = $darkenScreen;
107 $result->color = $color;
108 $result->overlay = $overlay;
109 return $result;
110 }
111
112 public static function query(int $bossActorUniqueId, int $playerActorUniqueId) : self{
113 $result = self::base($bossActorUniqueId, self::TYPE_QUERY);
114 $result->playerActorUniqueId = $playerActorUniqueId;
115 return $result;
116 }
117
118 protected function decodePayload(ByteBufferReader $in) : void{
119 $this->bossActorUniqueId = CommonTypes::getActorUniqueId($in);
120 $this->eventType = VarInt::readUnsignedInt($in);
121 switch($this->eventType){
122 case self::TYPE_REGISTER_PLAYER:
123 case self::TYPE_UNREGISTER_PLAYER:
124 case self::TYPE_QUERY:
125 $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in);
126 break;
128 case self::TYPE_SHOW:
129 $this->title = CommonTypes::getString($in);
130 $this->filteredTitle = CommonTypes::getString($in);
131 $this->healthPercent = LE::readFloat($in);
133 case self::TYPE_PROPERTIES:
134 $this->darkenScreen = match($raw = LE::readUnsignedShort($in)){
135 0 => false,
136 1 => true,
137 default => throw new PacketDecodeException("Invalid darkenScreen value $raw"),
138 };
139 case self::TYPE_TEXTURE:
140 $this->color = VarInt::readUnsignedInt($in);
141 $this->overlay = VarInt::readUnsignedInt($in);
142 break;
143 case self::TYPE_HEALTH_PERCENT:
144 $this->healthPercent = LE::readFloat($in);
145 break;
146 case self::TYPE_TITLE:
147 $this->title = CommonTypes::getString($in);
148 $this->filteredTitle = CommonTypes::getString($in);
149 break;
150 default:
151 break;
152 }
153 }
154
155 protected function encodePayload(ByteBufferWriter $out) : void{
156 CommonTypes::putActorUniqueId($out, $this->bossActorUniqueId);
157 VarInt::writeUnsignedInt($out, $this->eventType);
158 switch($this->eventType){
159 case self::TYPE_REGISTER_PLAYER:
160 case self::TYPE_UNREGISTER_PLAYER:
161 case self::TYPE_QUERY:
162 CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId);
163 break;
165 case self::TYPE_SHOW:
166 CommonTypes::putString($out, $this->title);
167 CommonTypes::putString($out, $this->filteredTitle);
168 LE::writeFloat($out, $this->healthPercent);
170 case self::TYPE_PROPERTIES:
171 LE::writeUnsignedShort($out, $this->darkenScreen ? 1 : 0);
172 case self::TYPE_TEXTURE:
173 VarInt::writeUnsignedInt($out, $this->color);
174 VarInt::writeUnsignedInt($out, $this->overlay);
175 break;
176 case self::TYPE_HEALTH_PERCENT:
177 LE::writeFloat($out, $this->healthPercent);
178 break;
179 case self::TYPE_TITLE:
180 CommonTypes::putString($out, $this->title);
181 CommonTypes::putString($out, $this->filteredTitle);
182 break;
183 default:
184 break;
185 }
186 }
187
188 public function handle(PacketHandlerInterface $handler) : bool{
189 return $handler->handleBossEvent($this);
190 }
191}