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