PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
item/FireworkRocket.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
29use pocketmine\entity\object\FireworkRocket as FireworkEntity;
36use function array_map;
37use function mt_rand;
38
39class FireworkRocket extends Item{
40
41 public const TAG_FIREWORK_DATA = "Fireworks"; //TAG_Compound
42 protected const TAG_FLIGHT_TIME_MULTIPLIER = "Flight"; //TAG_Byte
43 public const TAG_EXPLOSIONS = "Explosions"; //TAG_List
44
45 protected int $flightTimeMultiplier = 1;
46
48 protected array $explosions = [];
49
56 public function getFlightTimeMultiplier() : int{
57 return $this->flightTimeMultiplier;
58 }
59
68 public function setFlightTimeMultiplier(int $multiplier) : self{
69 if($multiplier < 1 || $multiplier > 127){
70 throw new \InvalidArgumentException("Flight time multiplier must be in range 1-127");
71 }
72 $this->flightTimeMultiplier = $multiplier;
73
74 return $this;
75 }
76
80 public function getExplosions() : array{
81 return $this->explosions;
82 }
83
89 public function setExplosions(array $explosions) : self{
90 Utils::validateArrayValueType($explosions, function(FireworkRocketExplosion $_) : void{});
91 $this->explosions = $explosions;
92
93 return $this;
94 }
95
96 public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, array &$returnedItems) : ItemUseResult{
97 //TODO: this would be nicer if Vector3::getSide() accepted floats for distance
98 $position = $blockClicked->getPosition()->addVector($clickVector)->addVector(Vector3::zero()->getSide($face)->multiply(0.15));
99
100 $randomDuration = (($this->flightTimeMultiplier + 1) * 10) + mt_rand(0, 12);
101
102 $entity = new FireworkEntity(Location::fromObject($position, $player->getWorld(), Utils::getRandomFloat() * 360, 90), $randomDuration, $this->explosions);
103 $entity->setOwningEntity($player);
104 $entity->setMotion(new Vector3(
105 (Utils::getRandomFloat() - Utils::getRandomFloat()) * 0.0023,
106 0.05,
107 (Utils::getRandomFloat() - Utils::getRandomFloat()) * 0.0023
108 ));
109 $entity->spawnToAll();
110
111 $this->pop();
112
113 return ItemUseResult::SUCCESS;
114 }
115
116 protected function deserializeCompoundTag(CompoundTag $tag) : void{
117 parent::deserializeCompoundTag($tag);
118
119 $fireworkData = $tag->getCompoundTag(self::TAG_FIREWORK_DATA);
120 if($fireworkData === null){
121 throw new SavedDataLoadingException("Missing firework data");
122 }
123
124 $this->setFlightTimeMultiplier($fireworkData->getByte(self::TAG_FLIGHT_TIME_MULTIPLIER, 1));
125
126 if(($explosions = $fireworkData->getListTag(self::TAG_EXPLOSIONS, CompoundTag::class)) !== null){
127 foreach($explosions as $explosion){
128 $this->explosions[] = FireworkRocketExplosion::fromCompoundTag($explosion);
129 }
130 }
131 }
132
133 protected function serializeCompoundTag(CompoundTag $tag) : void{
134 parent::serializeCompoundTag($tag);
135
136 $fireworkData = CompoundTag::create();
137 $fireworkData->setByte(self::TAG_FLIGHT_TIME_MULTIPLIER, $this->flightTimeMultiplier);
138 $fireworkData->setTag(self::TAG_EXPLOSIONS, new ListTag(array_map(fn(FireworkRocketExplosion $e) => $e->toCompoundTag(), $this->explosions)));
139
140 $tag->setTag(self::TAG_FIREWORK_DATA, $fireworkData);
141 }
142}
onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, array &$returnedItems)