PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
FireworkRocketExplosion.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\item;
25
26use pocketmine\block\utils\DyeColor;
33use function array_key_first;
34use function chr;
35use function count;
36use function ord;
37use function strlen;
38
40
41 protected const TAG_TYPE = "FireworkType"; //TAG_Byte
42 protected const TAG_COLORS = "FireworkColor"; //TAG_ByteArray
43 protected const TAG_FADE_COLORS = "FireworkFade"; //TAG_ByteArray
44 protected const TAG_TWINKLE = "FireworkFlicker"; //TAG_Byte
45 protected const TAG_TRAIL = "FireworkTrail"; //TAG_Byte
46
50 public static function fromCompoundTag(CompoundTag $tag) : self{
51 $colors = self::decodeColors($tag->getByteArray(self::TAG_COLORS));
52 if(count($colors) === 0){
53 throw new SavedDataLoadingException("Colors list cannot be empty");
54 }
55
56 return new self(
57 FireworkRocketTypeIdMap::getInstance()->fromId($tag->getByte(self::TAG_TYPE)) ?? throw new SavedDataLoadingException("Invalid firework type"),
58 $colors,
59 self::decodeColors($tag->getByteArray(self::TAG_FADE_COLORS)),
60 $tag->getByte(self::TAG_TWINKLE, 0) !== 0,
61 $tag->getByte(self::TAG_TRAIL, 0) !== 0
62 );
63 }
64
70 protected static function decodeColors(string $colorsBytes) : array{
71 $colors = [];
72
73 $dyeColorIdMap = DyeColorIdMap::getInstance();
74 for($i = 0, $len = strlen($colorsBytes); $i < $len; $i++){
75 $colorByte = ord($colorsBytes[$i]);
76 $color = $dyeColorIdMap->fromInvertedId($colorByte);
77 if($color !== null){
78 $colors[] = $color;
79 }else{
80 throw new SavedDataLoadingException("Unknown color $colorByte");
81 }
82 }
83
84 return $colors;
85 }
86
90 protected static function encodeColors(array $colors) : string{
91 $colorsBytes = "";
92
93 $dyeColorIdMap = DyeColorIdMap::getInstance();
94 foreach($colors as $color){
95 $colorsBytes .= chr($dyeColorIdMap->toInvertedId($color));
96 }
97
98 return $colorsBytes;
99 }
100
107 public function __construct(
108 protected FireworkRocketType $type,
109 protected array $colors,
110 protected array $fadeColors = [],
111 protected bool $twinkle = false,
112 protected bool $trail = false
113 ){
114 if(count($colors) === 0){
115 throw new \InvalidArgumentException("Colors list cannot be empty");
116 }
117
118 $colorsValidator = function(DyeColor $_) : void{};
119
120 Utils::validateArrayValueType($colors, $colorsValidator);
121 Utils::validateArrayValueType($fadeColors, $colorsValidator);
122 }
123
124 public function getType() : FireworkRocketType{
125 return $this->type;
126 }
127
134 public function getColors() : array{
135 return $this->colors;
136 }
137
141 public function getFlashColor() : DyeColor{
142 return $this->colors[array_key_first($this->colors)];
143 }
144
148 public function getColorMix() : Color{
150 $colors = [];
151 foreach($this->colors as $dyeColor){
152 $colors[] = $dyeColor->getRgbValue();
153 }
154 return Color::mix(...$colors);
155 }
156
164 public function getFadeColors() : array{
165 return $this->fadeColors;
166 }
167
171 public function willTwinkle() : bool{
172 return $this->twinkle;
173 }
174
178 public function getTrail() : bool{
179 return $this->trail;
180 }
181
182 public function toCompoundTag() : CompoundTag{
183 return CompoundTag::create()
184 ->setByte(self::TAG_TYPE, FireworkRocketTypeIdMap::getInstance()->toId($this->type))
185 ->setByteArray(self::TAG_COLORS, self::encodeColors($this->colors))
186 ->setByteArray(self::TAG_FADE_COLORS, self::encodeColors($this->fadeColors))
187 ->setByte(self::TAG_TWINKLE, $this->twinkle ? 1 : 0)
188 ->setByte(self::TAG_TRAIL, $this->trail ? 1 : 0);
189 }
190}
__construct(protected FireworkRocketType $type, protected array $colors, protected array $fadeColors=[], protected bool $twinkle=false, protected bool $trail=false)