PocketMine-MP 5.44.4 git-6a7cc02e9dff59b69241aa0bcffdb9903ce86beb
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\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
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 = 0;
50 public float $healthPercent = 0.0;
51 public string $title = "";
52 public string $filteredTitle = "";
53 public int $color = BossBarColor::PINK;
54 public int $overlay = 0;
55
56 private static function base(int $bossActorUniqueId, int $eventId) : self{
57 $result = new self;
58 $result->bossActorUniqueId = $bossActorUniqueId;
59 $result->eventType = $eventId;
60 return $result;
61 }
62
63 public static function show(int $bossActorUniqueId, string $title, float $healthPercent, int $color = BossBarColor::PURPLE, int $overlay = 0) : self{
64 $result = self::base($bossActorUniqueId, self::TYPE_SHOW);
65 $result->title = $title;
66 $result->filteredTitle = $title;
67 $result->healthPercent = $healthPercent;
68 $result->color = $color;
69 $result->overlay = $overlay;
70 return $result;
71 }
72
73 public static function hide(int $bossActorUniqueId) : self{
74 return self::base($bossActorUniqueId, self::TYPE_HIDE);
75 }
76
77 public static function registerPlayer(int $bossActorUniqueId, int $playerActorUniqueId) : self{
78 $result = self::base($bossActorUniqueId, self::TYPE_REGISTER_PLAYER);
79 $result->playerActorUniqueId = $playerActorUniqueId;
80 return $result;
81 }
82
83 public static function unregisterPlayer(int $bossActorUniqueId, int $playerActorUniqueId) : self{
84 $result = self::base($bossActorUniqueId, self::TYPE_UNREGISTER_PLAYER);
85 $result->playerActorUniqueId = $playerActorUniqueId;
86 return $result;
87 }
88
89 public static function healthPercent(int $bossActorUniqueId, float $healthPercent) : self{
90 $result = self::base($bossActorUniqueId, self::TYPE_HEALTH_PERCENT);
91 $result->healthPercent = $healthPercent;
92 return $result;
93 }
94
95 public static function title(int $bossActorUniqueId, string $title) : self{
96 $result = self::base($bossActorUniqueId, self::TYPE_TITLE);
97 $result->title = $title;
98 $result->filteredTitle = $title;
99 return $result;
100 }
101
102 public static function properties(int $bossActorUniqueId, int $color = BossBarColor::PURPLE, int $overlay = 0) : self{
103 $result = self::base($bossActorUniqueId, self::TYPE_PROPERTIES);
104 $result->color = $color;
105 $result->overlay = $overlay;
106 return $result;
107 }
108
109 public static function query(int $bossActorUniqueId, int $playerActorUniqueId) : self{
110 $result = self::base($bossActorUniqueId, self::TYPE_QUERY);
111 $result->playerActorUniqueId = $playerActorUniqueId;
112 return $result;
113 }
114
115 protected function decodePayload(ByteBufferReader $in) : void{
116 $this->bossActorUniqueId = CommonTypes::getActorUniqueId($in);
117 $this->playerActorUniqueId = CommonTypes::getActorUniqueId($in);
118 $this->eventType = Byte::readUnsigned($in);
119 $this->title = CommonTypes::getString($in);
120 $this->filteredTitle = CommonTypes::getString($in);
121 $this->healthPercent = LE::readFloat($in);
122 $this->color = Byte::readUnsigned($in);
123 $this->overlay = Byte::readUnsigned($in);
124 }
125
126 protected function encodePayload(ByteBufferWriter $out) : void{
127 CommonTypes::putActorUniqueId($out, $this->bossActorUniqueId);
128 CommonTypes::putActorUniqueId($out, $this->playerActorUniqueId);
129 Byte::writeUnsigned($out, $this->eventType);
130 CommonTypes::putString($out, $this->title);
131 CommonTypes::putString($out, $this->filteredTitle);
132 LE::writeFloat($out, $this->healthPercent);
133 Byte::writeUnsigned($out, $this->color);
134 Byte::writeUnsigned($out, $this->overlay);
135 }
136
137 public function handle(PacketHandlerInterface $handler) : bool{
138 return $handler->handleBossEvent($this);
139 }
140}