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