From 42222386e6f18773b1d34f76c03b7eb4cfde922b Mon Sep 17 00:00:00 2001 From: Muqsit Date: Mon, 20 Apr 2020 14:41:03 -0500 Subject: [PATCH 01/19] Port to API 4.0.0 --- plugin.yml | 2 +- src/BlockHorizons/Fireworks/Loader.php | 12 ++--- .../Fireworks/entity/FireworksRocket.php | 18 ++++--- .../Fireworks/item/Fireworks.php | 51 +++++++++++-------- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/plugin.yml b/plugin.yml index c308afe..30f71cd 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: Fireworks main: BlockHorizons\Fireworks\Loader -api: 3.9.0 +api: 4.0.0 version: 0.0.3 diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index ba36efd..5db896a 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -7,18 +7,14 @@ use BlockHorizons\Fireworks\item\Fireworks; use BlockHorizons\Fireworks\entity\FireworksRocket; -use pocketmine\entity\Entity; -use pocketmine\item\Item; +use pocketmine\entity\EntityFactory; use pocketmine\item\ItemFactory; use pocketmine\plugin\PluginBase; class Loader extends PluginBase { - public function onEnable(): void { - ItemFactory::registerItem(new Fireworks()); - Item::initCreativeItems(); //will load firework rockets from pocketmine's resources folder - if(!Entity::registerEntity(FireworksRocket::class, false, ["FireworksRocket"])) { - $this->getLogger()->error("Failed to register FireworksRocket entity with savename 'FireworksRocket'"); - } + protected function onEnable(): void { + ItemFactory::register(new Fireworks(), true); + EntityFactory::register(FireworksRocket::class, ["FireworksRocket"]); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 8946209..de9912a 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -6,14 +6,15 @@ use BlockHorizons\Fireworks\item\Fireworks; use pocketmine\entity\Entity; -use pocketmine\level\Level; use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\ActorEventPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; +use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; +use pocketmine\world\World; class FireworksRocket extends Entity { - public const NETWORK_ID = Entity::FIREWORKS_ROCKET; + public const NETWORK_ID = EntityLegacyIds::FIREWORKS_ROCKET; public const DATA_FIREWORK_ITEM = 16; //firework item @@ -23,15 +24,18 @@ class FireworksRocket extends Entity { /** @var int */ protected $lifeTime = 0; - public function __construct(Level $level, CompoundTag $nbt, ?Fireworks $fireworks = null){ - parent::__construct($level, $nbt); + public function __construct(World $world, CompoundTag $nbt, ?Fireworks $fireworks = null){ + parent::__construct($world, $nbt); - if($fireworks !== null && $fireworks->getNamedTagEntry("Fireworks") instanceof CompoundTag) { - $this->propertyManager->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); + if($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { + $this->networkProperties->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); $this->setLifeTime($fireworks->getRandomizedFlightDuration()); } - $level->broadcastLevelSoundEvent($this, LevelSoundEventPacket::SOUND_LAUNCH); + $packet = new LevelSoundEventPacket(); + $packet->sound = LevelSoundEventPacket::SOUND_LAUNCH; + $packet->position = $this->location->asVector3(); + $world->broadcastPacketToViewers($this->location, $packet); } protected function tryChangeMovement(): void { diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index 4047348..389500c 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -4,13 +4,17 @@ namespace BlockHorizons\Fireworks\item; +use BlockHorizons\Fireworks\entity\FireworksRocket; use pocketmine\block\Block; use pocketmine\entity\Entity; +use pocketmine\entity\EntityFactory; use pocketmine\item\Item; +use pocketmine\item\ItemIds; +use pocketmine\item\ItemUseResult; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; -use pocketmine\Player; +use pocketmine\player\Player; class Fireworks extends Item { @@ -39,7 +43,7 @@ class Fireworks extends Item { public const COLOR_WHITE = "\x0f"; public function __construct(int $meta = 0) { - parent::__construct(self::FIREWORKS, $meta, "Fireworks"); + parent::__construct(ItemIds::FIREWORKS, $meta, "Fireworks"); } public function getFlightDuration(): int { @@ -51,41 +55,44 @@ public function getRandomizedFlightDuration(): int { } public function setFlightDuration(int $duration): void { - $tag = $this->getExplosionsTag(); - $tag->setByte("Flight", $duration); - $this->setNamedTagEntry($tag); + $this->getExplosionsTag()->setByte("Flight", $duration); } public function addExplosion(int $type, string $color, string $fade = "", bool $flicker = false, bool $trail = false): void { - $explosion = new CompoundTag(); - $explosion->setByte("FireworkType", $type); - $explosion->setByteArray("FireworkColor", $color); - $explosion->setByteArray("FireworkFade", $fade); - $explosion->setByte("FireworkFlicker", $flicker ? 1 : 0); - $explosion->setByte("FireworkTrail", $trail ? 1 : 0); - $tag = $this->getExplosionsTag(); - $explosions = $tag->getListTag("Explosions") ?? new ListTag("Explosions"); - $explosions->push($explosion); - $tag->setTag($explosions); - $this->setNamedTagEntry($tag); + $explosions = $tag->getListTag("Explosions"); + if($explosions === null){ + $tag->setTag("Explosions", $explosions = new ListTag()); + } + + $explosions->push(CompoundTag::create() + ->setByte("FireworkType", $type) + ->setByteArray("FireworkColor", $color) + ->setByteArray("FireworkFade", $fade) + ->setByte("FireworkFlicker", $flicker ? 1 : 0) + ->setByte("FireworkTrail", $trail ? 1 : 0) + ); } protected function getExplosionsTag(): CompoundTag { - return $this->getNamedTag()->getCompoundTag("Fireworks") ?? new CompoundTag("Fireworks"); + $tag = $this->getNamedTag()->getCompoundTag("Fireworks"); + if($tag === null){ + $this->getNamedTag()->setTag("Fireworks", $tag = CompoundTag::create()); + } + return $tag; } - public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): bool { - $nbt = Entity::createBaseNBT($blockReplace->add(0.5, 0, 0.5), new Vector3(0.001, 0.05, 0.001), lcg_value() * 360, 90); + public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { + $nbt = EntityFactory::createBaseNBT($blockReplace->getPos()->add(0.5, 0, 0.5), new Vector3(0.001, 0.05, 0.001), lcg_value() * 360, 90); - $entity = Entity::createEntity("FireworksRocket", $player->getLevel(), $nbt, $this); + $entity = EntityFactory::create(FireworksRocket::class, $player->getWorld(), $nbt, $this); if($entity instanceof Entity) { --$this->count; $entity->spawnToAll(); - return true; + return ItemUseResult::SUCCESS(); } - return false; + return ItemUseResult::FAIL(); } } \ No newline at end of file From d0fb744ed97bb73cde541dca74e348669411c55f Mon Sep 17 00:00:00 2001 From: Muqsit Date: Fri, 24 Apr 2020 00:30:42 -0500 Subject: [PATCH 02/19] 4.0 changes --- src/BlockHorizons/Fireworks/Loader.php | 2 +- src/BlockHorizons/Fireworks/item/Fireworks.php | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 5db896a..9598416 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -14,7 +14,7 @@ class Loader extends PluginBase { protected function onEnable(): void { - ItemFactory::register(new Fireworks(), true); + ItemFactory::getInstance()->register(new Fireworks(), true); EntityFactory::register(FireworksRocket::class, ["FireworksRocket"]); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index 389500c..bad6d1e 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -6,7 +6,6 @@ use BlockHorizons\Fireworks\entity\FireworksRocket; use pocketmine\block\Block; -use pocketmine\entity\Entity; use pocketmine\entity\EntityFactory; use pocketmine\item\Item; use pocketmine\item\ItemIds; @@ -85,14 +84,8 @@ protected function getExplosionsTag(): CompoundTag { public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { $nbt = EntityFactory::createBaseNBT($blockReplace->getPos()->add(0.5, 0, 0.5), new Vector3(0.001, 0.05, 0.001), lcg_value() * 360, 90); - - $entity = EntityFactory::create(FireworksRocket::class, $player->getWorld(), $nbt, $this); - - if($entity instanceof Entity) { - --$this->count; - $entity->spawnToAll(); - return ItemUseResult::SUCCESS(); - } - return ItemUseResult::FAIL(); + EntityFactory::create(FireworksRocket::class, $player->getWorld(), $nbt, $this)->spawnToAll(); + $this->pop(); + return ItemUseResult::SUCCESS(); } } \ No newline at end of file From 1777ade8b9e2b6113a12ab639528da2cfda3d9c5 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Sat, 25 Apr 2020 09:44:46 -0500 Subject: [PATCH 03/19] 4.0 changes to EntityFactory --- src/BlockHorizons/Fireworks/Loader.php | 2 +- src/BlockHorizons/Fireworks/item/Fireworks.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 9598416..119b1e3 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -15,6 +15,6 @@ class Loader extends PluginBase { protected function onEnable(): void { ItemFactory::getInstance()->register(new Fireworks(), true); - EntityFactory::register(FireworksRocket::class, ["FireworksRocket"]); + EntityFactory::getInstance()->register(FireworksRocket::class, ["FireworksRocket"]); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index bad6d1e..5f8a84d 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -84,7 +84,7 @@ protected function getExplosionsTag(): CompoundTag { public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { $nbt = EntityFactory::createBaseNBT($blockReplace->getPos()->add(0.5, 0, 0.5), new Vector3(0.001, 0.05, 0.001), lcg_value() * 360, 90); - EntityFactory::create(FireworksRocket::class, $player->getWorld(), $nbt, $this)->spawnToAll(); + EntityFactory::getInstance()->create(FireworksRocket::class, $player->getWorld(), $nbt, $this)->spawnToAll(); $this->pop(); return ItemUseResult::SUCCESS(); } From a09c1860b2c7557dd292b8cc1ca3dd31f9ac51af Mon Sep 17 00:00:00 2001 From: Muqsit Date: Mon, 4 May 2020 05:52:04 -0500 Subject: [PATCH 04/19] 4.0 changes --- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index de9912a..d4b5d96 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -72,6 +72,9 @@ protected function doLifeTimeTick(): bool { } protected function doExplosionAnimation(): void { - $this->broadcastEntityEvent(ActorEventPacket::FIREWORK_PARTICLES); + $viewers = $this->getViewers(); + if(count($viewers) > 0){ + $this->server->broadcastPackets($viewers, [ActorEventPacket::create($this->id, ActorEventPacket::FIREWORK_PARTICLES, 0)]); + } } } \ No newline at end of file From 6c6bf3bd0e5281e7e98e5a3f683177ea23ec1ade Mon Sep 17 00:00:00 2001 From: Muqsit Date: Mon, 18 May 2020 05:38:58 -0500 Subject: [PATCH 05/19] 4.0: Entity::NETWORK_ID -> Entity::getNetworkTypeId() --- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index d4b5d96..0b165bd 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -14,10 +14,12 @@ class FireworksRocket extends Entity { - public const NETWORK_ID = EntityLegacyIds::FIREWORKS_ROCKET; - public const DATA_FIREWORK_ITEM = 16; //firework item + public static function getNetworkTypeId() : int{ + return EntityLegacyIds::FIREWORKS_ROCKET; + } + public $width = 0.25; public $height = 0.25; From 970137c846cf450f5e16869a7e1d4b60d172100b Mon Sep 17 00:00:00 2001 From: Muqsit Date: Sun, 24 May 2020 01:19:26 -0500 Subject: [PATCH 06/19] 4.0 changes --- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 0b165bd..119b3b4 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -30,7 +30,7 @@ public function __construct(World $world, CompoundTag $nbt, ?Fireworks $firework parent::__construct($world, $nbt); if($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { - $this->networkProperties->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); + $this->getNetworkProperties()->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); $this->setLifeTime($fireworks->getRandomizedFlightDuration()); } From 1e24e7ce781bf9f31c8fba6fecfda313aaaba071 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Sat, 20 Jun 2020 16:55:09 -0500 Subject: [PATCH 07/19] 4.0: Changes to entity save data --- src/BlockHorizons/Fireworks/Loader.php | 7 ++++++- .../Fireworks/entity/FireworksRocket.php | 20 +++++++++---------- .../Fireworks/item/Fireworks.php | 7 ++++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 119b1e3..4b467d4 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -7,14 +7,19 @@ use BlockHorizons\Fireworks\item\Fireworks; use BlockHorizons\Fireworks\entity\FireworksRocket; +use pocketmine\entity\EntityDataHelper; use pocketmine\entity\EntityFactory; use pocketmine\item\ItemFactory; +use pocketmine\nbt\tag\CompoundTag; use pocketmine\plugin\PluginBase; +use pocketmine\world\World; class Loader extends PluginBase { protected function onEnable(): void { ItemFactory::getInstance()->register(new Fireworks(), true); - EntityFactory::getInstance()->register(FireworksRocket::class, ["FireworksRocket"]); + EntityFactory::getInstance()->register(FireworksRocket::class, static function(World $world, CompoundTag $nbt) : FireworksRocket{ + return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world)); + }, ["FireworksRocket"]); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 119b3b4..4ec5afc 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -6,18 +6,17 @@ use BlockHorizons\Fireworks\item\Fireworks; use pocketmine\entity\Entity; -use pocketmine\nbt\tag\CompoundTag; +use pocketmine\entity\Location; use pocketmine\network\mcpe\protocol\ActorEventPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; -use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; -use pocketmine\world\World; +use pocketmine\network\mcpe\protocol\types\entity\EntityIds; class FireworksRocket extends Entity { public const DATA_FIREWORK_ITEM = 16; //firework item - public static function getNetworkTypeId() : int{ - return EntityLegacyIds::FIREWORKS_ROCKET; + public static function getNetworkTypeId() : string{ + return EntityIds::FIREWORKS_ROCKET; } public $width = 0.25; @@ -26,18 +25,17 @@ public static function getNetworkTypeId() : int{ /** @var int */ protected $lifeTime = 0; - public function __construct(World $world, CompoundTag $nbt, ?Fireworks $fireworks = null){ - parent::__construct($world, $nbt); - + public function __construct(Location $location, ?Fireworks $fireworks = null, ?int $lifeTime = null){ if($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { - $this->getNetworkProperties()->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); - $this->setLifeTime($fireworks->getRandomizedFlightDuration()); + $this->getNetworkProperties()->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); + $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } + parent::__construct($location); $packet = new LevelSoundEventPacket(); $packet->sound = LevelSoundEventPacket::SOUND_LAUNCH; $packet->position = $this->location->asVector3(); - $world->broadcastPacketToViewers($this->location, $packet); + $location->getWorldNonNull()->broadcastPacketToViewers($this->location, $packet); } protected function tryChangeMovement(): void { diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index 5f8a84d..ccd60d5 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -6,7 +6,7 @@ use BlockHorizons\Fireworks\entity\FireworksRocket; use pocketmine\block\Block; -use pocketmine\entity\EntityFactory; +use pocketmine\entity\Location; use pocketmine\item\Item; use pocketmine\item\ItemIds; use pocketmine\item\ItemUseResult; @@ -83,8 +83,9 @@ protected function getExplosionsTag(): CompoundTag { } public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { - $nbt = EntityFactory::createBaseNBT($blockReplace->getPos()->add(0.5, 0, 0.5), new Vector3(0.001, 0.05, 0.001), lcg_value() * 360, 90); - EntityFactory::getInstance()->create(FireworksRocket::class, $player->getWorld(), $nbt, $this)->spawnToAll(); + $rocket = new FireworksRocket(Location::fromObject($blockReplace->getPos()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); + $rocket->setMotion(new Vector3(0.001, 0.05, 0.001)); + $rocket->spawnToAll(); $this->pop(); return ItemUseResult::SUCCESS(); } From 3bf14a3619e8d143ca8fbdda17531daa2631d408 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Sat, 27 Jun 2020 18:16:48 -0500 Subject: [PATCH 08/19] Fix FireworksRocket crashing on spawn --- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 4ec5afc..fc2bf9c 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -26,12 +26,13 @@ public static function getNetworkTypeId() : string{ protected $lifeTime = 0; public function __construct(Location $location, ?Fireworks $fireworks = null, ?int $lifeTime = null){ + parent::__construct($location); + if($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { $this->getNetworkProperties()->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } - parent::__construct($location); $packet = new LevelSoundEventPacket(); $packet->sound = LevelSoundEventPacket::SOUND_LAUNCH; $packet->position = $this->location->asVector3(); From 91d162395d757c0ff3fd9b041955ad28e3ec0144 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Thu, 9 Jul 2020 23:47:17 -0500 Subject: [PATCH 09/19] 4.0: Position::getWorldNonNull() -> Position::getWorld(), Item::__construct(int, int, ...) -> Item::__construct(ItemIdentifier, ...) --- src/BlockHorizons/Fireworks/Loader.php | 4 +++- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 2 +- src/BlockHorizons/Fireworks/item/Fireworks.php | 5 ----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 4b467d4..fce2805 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -10,6 +10,8 @@ use pocketmine\entity\EntityDataHelper; use pocketmine\entity\EntityFactory; use pocketmine\item\ItemFactory; +use pocketmine\item\ItemIdentifier; +use pocketmine\item\ItemIds; use pocketmine\nbt\tag\CompoundTag; use pocketmine\plugin\PluginBase; use pocketmine\world\World; @@ -17,7 +19,7 @@ class Loader extends PluginBase { protected function onEnable(): void { - ItemFactory::getInstance()->register(new Fireworks(), true); + ItemFactory::getInstance()->register(new Fireworks(new ItemIdentifier(ItemIds::FIREWORKS, 0), "Fireworks"), true); EntityFactory::getInstance()->register(FireworksRocket::class, static function(World $world, CompoundTag $nbt) : FireworksRocket{ return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world)); }, ["FireworksRocket"]); diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index fc2bf9c..7c8aa5b 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -36,7 +36,7 @@ public function __construct(Location $location, ?Fireworks $fireworks = null, ?i $packet = new LevelSoundEventPacket(); $packet->sound = LevelSoundEventPacket::SOUND_LAUNCH; $packet->position = $this->location->asVector3(); - $location->getWorldNonNull()->broadcastPacketToViewers($this->location, $packet); + $location->getWorld()->broadcastPacketToViewers($this->location, $packet); } protected function tryChangeMovement(): void { diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index ccd60d5..1ce2ea1 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -8,7 +8,6 @@ use pocketmine\block\Block; use pocketmine\entity\Location; use pocketmine\item\Item; -use pocketmine\item\ItemIds; use pocketmine\item\ItemUseResult; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; @@ -41,10 +40,6 @@ class Fireworks extends Item { public const COLOR_GOLD = "\x0e"; public const COLOR_WHITE = "\x0f"; - public function __construct(int $meta = 0) { - parent::__construct(ItemIds::FIREWORKS, $meta, "Fireworks"); - } - public function getFlightDuration(): int { return $this->getExplosionsTag()->getByte("Flight", 1); } From 490b5f3b52b021a246482a46cd8ba822de82de2c Mon Sep 17 00:00:00 2001 From: XenialDan Date: Thu, 24 Dec 2020 15:45:11 +0100 Subject: [PATCH 10/19] Fix #12, Fix #13, Use broadcastAnimation && syncNetworkData --- README.md | 3 +- plugin.yml | 2 +- src/BlockHorizons/Fireworks/Loader.php | 9 ++--- .../Fireworks/entity/FireworksRocket.php | 35 ++++++++++++------- .../animation/FireworkParticleAnimation.php | 25 +++++++++++++ 5 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php diff --git a/README.md b/README.md index 9918215..93a0e3c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Adds fireworks to your PocketMine server
Download the compiled `.phar` format of Fireworks from the [Poggit CI](https://poggit.pmmp.io/ci/BlockHorizons/Fireworks). ## API ### Adding firework items to a player's inventory -Giving players fireworks is easy as pie. Here are some examples (where `$player` is a `\pocketmine\Player` object): +Giving players fireworks is easy as pie. Here are some examples (where `$player` is a `\pocketmine\player\Player` +object): - **Base firework** ```php /** @var Fireworks $fw */ diff --git a/plugin.yml b/plugin.yml index 30f71cd..83d49ad 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: Fireworks main: BlockHorizons\Fireworks\Loader api: 4.0.0 -version: 0.0.3 +version: 0.0.4 diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index fce2805..ff72b9f 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -4,15 +4,16 @@ namespace BlockHorizons\Fireworks; -use BlockHorizons\Fireworks\item\Fireworks; use BlockHorizons\Fireworks\entity\FireworksRocket; - +use BlockHorizons\Fireworks\item\Fireworks; +use pocketmine\data\bedrock\EntityLegacyIds; use pocketmine\entity\EntityDataHelper; use pocketmine\entity\EntityFactory; use pocketmine\item\ItemFactory; use pocketmine\item\ItemIdentifier; use pocketmine\item\ItemIds; use pocketmine\nbt\tag\CompoundTag; +use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\plugin\PluginBase; use pocketmine\world\World; @@ -20,8 +21,8 @@ class Loader extends PluginBase { protected function onEnable(): void { ItemFactory::getInstance()->register(new Fireworks(new ItemIdentifier(ItemIds::FIREWORKS, 0), "Fireworks"), true); - EntityFactory::getInstance()->register(FireworksRocket::class, static function(World $world, CompoundTag $nbt) : FireworksRocket{ + EntityFactory::getInstance()->register(FireworksRocket::class, static function (World $world, CompoundTag $nbt): FireworksRocket { return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world)); - }, ["FireworksRocket"]); + }, ["FireworksRocket", EntityIds::FIREWORKS_ROCKET], EntityLegacyIds::FIREWORKS_ROCKET); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 7c8aa5b..4f85763 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -5,17 +5,19 @@ namespace BlockHorizons\Fireworks\entity; use BlockHorizons\Fireworks\item\Fireworks; +use FireworkParticleAnimation; use pocketmine\entity\Entity; use pocketmine\entity\Location; -use pocketmine\network\mcpe\protocol\ActorEventPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\network\mcpe\protocol\types\entity\EntityIds; +use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; class FireworksRocket extends Entity { public const DATA_FIREWORK_ITEM = 16; //firework item - public static function getNetworkTypeId() : string{ + public static function getNetworkTypeId(): string + { return EntityIds::FIREWORKS_ROCKET; } @@ -24,12 +26,16 @@ public static function getNetworkTypeId() : string{ /** @var int */ protected $lifeTime = 0; + /** @var Fireworks|null */ + protected $fireworks; - public function __construct(Location $location, ?Fireworks $fireworks = null, ?int $lifeTime = null){ + public function __construct(Location $location, ?Fireworks $fireworks = null, ?int $lifeTime = null) + { parent::__construct($location); - if($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { - $this->getNetworkProperties()->setCompoundTag(self::DATA_FIREWORK_ITEM, $fireworks->getNamedTag()); + $this->fireworks = $fireworks; + + if ($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } @@ -62,8 +68,9 @@ public function setLifeTime(int $life): void { $this->lifeTime = $life; } - protected function doLifeTimeTick(): bool { - if(!$this->isFlaggedForDespawn() and --$this->lifeTime < 0) { + protected function doLifeTimeTick(): bool + { + if (--$this->lifeTime < 0 && !$this->isFlaggedForDespawn()) { $this->doExplosionAnimation(); $this->flagForDespawn(); return true; @@ -72,10 +79,14 @@ protected function doLifeTimeTick(): bool { return false; } - protected function doExplosionAnimation(): void { - $viewers = $this->getViewers(); - if(count($viewers) > 0){ - $this->server->broadcastPackets($viewers, [ActorEventPacket::create($this->id, ActorEventPacket::FIREWORK_PARTICLES, 0)]); - } + protected function doExplosionAnimation(): void + { + $this->broadcastAnimation(new FireworkParticleAnimation($this), $this->getViewers()); + } + + public function syncNetworkData(EntityMetadataCollection $properties): void + { + parent::syncNetworkData($properties); + $properties->setCompoundTag(self::DATA_FIREWORK_ITEM, $this->fireworks->getNamedTag()); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php new file mode 100644 index 0000000..38d87a1 --- /dev/null +++ b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php @@ -0,0 +1,25 @@ +firework = $firework; + } + + public function encode(): array + { + return [ + ActorEventPacket::create($this->firework->getId(), ActorEventPacket::FIREWORK_PARTICLES, 0) + ]; + } +} \ No newline at end of file From 1f428174e2f84e75bfaf6cf5fac101d31e534d5c Mon Sep 17 00:00:00 2001 From: XenialDan Date: Thu, 24 Dec 2020 15:51:01 +0100 Subject: [PATCH 11/19] Fix branch not building --- .poggit.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.poggit.yml b/.poggit.yml index 5dc4dc0..dc458a1 100644 --- a/.poggit.yml +++ b/.poggit.yml @@ -1,6 +1,7 @@ --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/BlockHorizons/Fireworks branches: - master +- "4.0" projects: Fireworks: path: "" From 719a510ca77e9cbee4d1196e0441e8e298c7cdd3 Mon Sep 17 00:00:00 2001 From: XenialDan Date: Thu, 24 Dec 2020 17:26:40 +0100 Subject: [PATCH 12/19] Fix fireworks not spawning --- src/BlockHorizons/Fireworks/Loader.php | 8 +++-- .../Fireworks/entity/FireworksRocket.php | 31 +++++++++++-------- .../animation/FireworkParticleAnimation.php | 2 ++ .../Fireworks/item/Fireworks.php | 15 +++++---- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index ff72b9f..6167880 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -17,12 +17,14 @@ use pocketmine\plugin\PluginBase; use pocketmine\world\World; -class Loader extends PluginBase { +class Loader extends PluginBase +{ - protected function onEnable(): void { + public function onEnable(): void + { ItemFactory::getInstance()->register(new Fireworks(new ItemIdentifier(ItemIds::FIREWORKS, 0), "Fireworks"), true); EntityFactory::getInstance()->register(FireworksRocket::class, static function (World $world, CompoundTag $nbt): FireworksRocket { - return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world)); + return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world), ItemFactory::getInstance()->get(ItemIds::FIREWORKS)); }, ["FireworksRocket", EntityIds::FIREWORKS_ROCKET], EntityLegacyIds::FIREWORKS_ROCKET); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 4f85763..3b859c7 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -1,18 +1,20 @@ fireworks = $fireworks; + parent::__construct($location, $fireworks->getNamedTag()); + $this->setMotion(new Vector3(0.001, 0.05, 0.001)); - if ($fireworks !== null && $fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { + if ($fireworks->getNamedTag()->getCompoundTag("Fireworks") !== null) { $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } @@ -45,26 +47,29 @@ public function __construct(Location $location, ?Fireworks $fireworks = null, ?i $location->getWorld()->broadcastPacketToViewers($this->location, $packet); } - protected function tryChangeMovement(): void { + protected function tryChangeMovement(): void + { $this->motion->x *= 1.15; $this->motion->y += 0.04; $this->motion->z *= 1.15; } - public function entityBaseTick(int $tickDiff = 1): bool { - if($this->closed) { + public function entityBaseTick(int $tickDiff = 1): bool + { + if ($this->closed) { return false; } $hasUpdate = parent::entityBaseTick($tickDiff); - if($this->doLifeTimeTick()) { + if ($this->doLifeTimeTick()) { $hasUpdate = true; } return $hasUpdate; } - public function setLifeTime(int $life): void { + public function setLifeTime(int $life): void + { $this->lifeTime = $life; } diff --git a/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php index 38d87a1..2c46658 100644 --- a/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php +++ b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace BlockHorizons\Fireworks\entity\animation; + use BlockHorizons\Fireworks\entity\FireworksRocket; use pocketmine\entity\animation\Animation; use pocketmine\network\mcpe\protocol\ActorEventPacket; diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index 1ce2ea1..d4d28a5 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -69,19 +69,22 @@ public function addExplosion(int $type, string $color, string $fade = "", bool $ ); } - protected function getExplosionsTag(): CompoundTag { + protected function getExplosionsTag(): CompoundTag + { $tag = $this->getNamedTag()->getCompoundTag("Fireworks"); - if($tag === null){ + if ($tag === null) { $this->getNamedTag()->setTag("Fireworks", $tag = CompoundTag::create()); } return $tag; } - public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { - $rocket = new FireworksRocket(Location::fromObject($blockReplace->getPos()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); - $rocket->setMotion(new Vector3(0.001, 0.05, 0.001)); - $rocket->spawnToAll(); + public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult + { + $entity = new FireworksRocket(Location::fromObject($blockReplace->getPos()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); + $this->pop(); + $entity->spawnToAll(); + //TODO: what if the entity was marked for deletion? return ItemUseResult::SUCCESS(); } } \ No newline at end of file From 96581aef754b89938b4d7a180a7243f1fa416d6f Mon Sep 17 00:00:00 2001 From: XenialDan Date: Thu, 24 Dec 2020 17:53:26 +0100 Subject: [PATCH 13/19] Add sounds --- .../Fireworks/entity/FireworksRocket.php | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 3b859c7..e3e8fef 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -9,6 +9,7 @@ use pocketmine\entity\Entity; use pocketmine\entity\Location; use pocketmine\math\Vector3; +use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; @@ -41,10 +42,7 @@ public function __construct(Location $location, Fireworks $fireworks, ?int $life $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } - $packet = new LevelSoundEventPacket(); - $packet->sound = LevelSoundEventPacket::SOUND_LAUNCH; - $packet->position = $this->location->asVector3(); - $location->getWorld()->broadcastPacketToViewers($this->location, $packet); + $location->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_LAUNCH, $this->location->asVector3())); } protected function tryChangeMovement(): void @@ -77,6 +75,7 @@ protected function doLifeTimeTick(): bool { if (--$this->lifeTime < 0 && !$this->isFlaggedForDespawn()) { $this->doExplosionAnimation(); + $this->playSounds(); $this->flagForDespawn(); return true; } @@ -89,6 +88,32 @@ protected function doExplosionAnimation(): void $this->broadcastAnimation(new FireworkParticleAnimation($this), $this->getViewers()); } + public function playSounds(): void + { + // This late in, there's 0 chance fireworks tag is null + $fireworksTag = $this->fireworks->getNamedTag()->getCompoundTag("Fireworks"); + $explosionsTag = $fireworksTag->getListTag("Explosions"); + if ($explosionsTag === null) { + // We don't throw an error here since there are fireworks that can die without noise or particles, + // which means they are lacking an explosion tag. + return; + } + + foreach ($explosionsTag->getValue() as $info) { + if ($info instanceof CompoundTag) { + if ($info->getByte("FireworkType", 0) === Fireworks::TYPE_HUGE_SPHERE) { + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_LARGE_BLAST, $this->location->asVector3())); + } else { + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_BLAST, $this->location->asVector3())); + } + + if ($info->getByte("FireworkFlicker", 0) === 1) { + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_TWINKLE, $this->location->asVector3())); + } + } + } + } + public function syncNetworkData(EntityMetadataCollection $properties): void { parent::syncNetworkData($properties); From 9f5dde866b46d0198ab2cf6d46e41362d1655475 Mon Sep 17 00:00:00 2001 From: Unickorn Date: Thu, 18 Mar 2021 08:24:41 +0300 Subject: [PATCH 14/19] 4.0: width/height -> getInitialSizeInfo() --- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index e3e8fef..fb1fdc2 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -7,6 +7,7 @@ use BlockHorizons\Fireworks\entity\animation\FireworkParticleAnimation; use BlockHorizons\Fireworks\item\Fireworks; use pocketmine\entity\Entity; +use pocketmine\entity\EntitySizeInfo; use pocketmine\entity\Location; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; @@ -24,9 +25,6 @@ public static function getNetworkTypeId(): string return EntityIds::FIREWORKS_ROCKET; } - public $width = 0.25; - public $height = 0.25; - /** @var int */ protected $lifeTime = 0; /** @var Fireworks */ @@ -119,4 +117,9 @@ public function syncNetworkData(EntityMetadataCollection $properties): void parent::syncNetworkData($properties); $properties->setCompoundTag(self::DATA_FIREWORK_ITEM, $this->fireworks->getNamedTag()); } + + protected function getInitialSizeInfo(): EntitySizeInfo + { + return new EntitySizeInfo(0.25, 0.25); + } } \ No newline at end of file From fdefc53c758eafef27fa68e0b7a640e2c3d5f92f Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 5 Sep 2021 01:43:35 +0300 Subject: [PATCH 15/19] PM4: getPos() -> getPosition() --- src/BlockHorizons/Fireworks/item/Fireworks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index d4d28a5..363ee0a 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -80,11 +80,11 @@ protected function getExplosionsTag(): CompoundTag public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult { - $entity = new FireworksRocket(Location::fromObject($blockReplace->getPos()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); + $entity = new FireworksRocket(Location::fromObject($blockReplace->getPosition()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); $this->pop(); $entity->spawnToAll(); //TODO: what if the entity was marked for deletion? return ItemUseResult::SUCCESS(); } -} \ No newline at end of file +} From ceba8352c0462cff0c28d728ac7b0d2550c589a6 Mon Sep 17 00:00:00 2001 From: Wertzui123 <46199283+Wertzui123@users.noreply.github.com> Date: Tue, 17 May 2022 16:20:48 +0200 Subject: [PATCH 16/19] Update to latest PocketMine-MP and fix fireworks not being nbt-deserialized correctly --- plugin.yml | 2 +- src/BlockHorizons/Fireworks/Loader.php | 3 ++- .../Fireworks/entity/FireworksRocket.php | 23 +++++++++++++------ .../animation/FireworkParticleAnimation.php | 3 ++- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/plugin.yml b/plugin.yml index 83d49ad..862a3c5 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: Fireworks main: BlockHorizons\Fireworks\Loader api: 4.0.0 -version: 0.0.4 +version: 0.0.4.1 diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 6167880..5a801f0 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -9,6 +9,7 @@ use pocketmine\data\bedrock\EntityLegacyIds; use pocketmine\entity\EntityDataHelper; use pocketmine\entity\EntityFactory; +use pocketmine\item\Item; use pocketmine\item\ItemFactory; use pocketmine\item\ItemIdentifier; use pocketmine\item\ItemIds; @@ -24,7 +25,7 @@ public function onEnable(): void { ItemFactory::getInstance()->register(new Fireworks(new ItemIdentifier(ItemIds::FIREWORKS, 0), "Fireworks"), true); EntityFactory::getInstance()->register(FireworksRocket::class, static function (World $world, CompoundTag $nbt): FireworksRocket { - return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world), ItemFactory::getInstance()->get(ItemIds::FIREWORKS)); + return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world), Item::nbtDeserialize($nbt->getCompoundTag("Item"))); }, ["FireworksRocket", EntityIds::FIREWORKS_ROCKET], EntityLegacyIds::FIREWORKS_ROCKET); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index fb1fdc2..37f57b2 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -12,8 +12,10 @@ use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; +use pocketmine\network\mcpe\protocol\types\CacheableNbt; use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; +use pocketmine\network\mcpe\protocol\types\LevelSoundEvent; class FireworksRocket extends Entity { @@ -40,7 +42,7 @@ public function __construct(Location $location, Fireworks $fireworks, ?int $life $this->setLifeTime($lifeTime ?? $fireworks->getRandomizedFlightDuration()); } - $location->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_LAUNCH, $this->location->asVector3())); + $location->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::nonActorSound(LevelSoundEvent::LAUNCH, $this->location->asVector3(), false)); } protected function tryChangeMovement(): void @@ -100,14 +102,14 @@ public function playSounds(): void foreach ($explosionsTag->getValue() as $info) { if ($info instanceof CompoundTag) { if ($info->getByte("FireworkType", 0) === Fireworks::TYPE_HUGE_SPHERE) { - $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_LARGE_BLAST, $this->location->asVector3())); + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::nonActorSound(LevelSoundEvent::LARGE_BLAST, $this->location->asVector3(), false)); } else { - $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_BLAST, $this->location->asVector3())); - } + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::nonActorSound(LevelSoundEvent::BLAST, $this->location->asVector3(), false)); + } if ($info->getByte("FireworkFlicker", 0) === 1) { - $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_TWINKLE, $this->location->asVector3())); - } + $this->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::nonActorSound(LevelSoundEvent::TWINKLE, $this->location->asVector3(), false)); + } } } } @@ -115,11 +117,18 @@ public function playSounds(): void public function syncNetworkData(EntityMetadataCollection $properties): void { parent::syncNetworkData($properties); - $properties->setCompoundTag(self::DATA_FIREWORK_ITEM, $this->fireworks->getNamedTag()); + $properties->setCompoundTag(self::DATA_FIREWORK_ITEM, new CacheableNbt($this->fireworks->getNamedTag())); } protected function getInitialSizeInfo(): EntitySizeInfo { return new EntitySizeInfo(0.25, 0.25); } + + public function saveNBT(): CompoundTag + { + $nbt = parent::saveNBT(); + $nbt->setTag("Item", $this->fireworks->nbtSerialize()); + return $nbt; + } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php index 2c46658..9f5f6b2 100644 --- a/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php +++ b/src/BlockHorizons/Fireworks/entity/animation/FireworkParticleAnimation.php @@ -7,6 +7,7 @@ use BlockHorizons\Fireworks\entity\FireworksRocket; use pocketmine\entity\animation\Animation; use pocketmine\network\mcpe\protocol\ActorEventPacket; +use pocketmine\network\mcpe\protocol\types\ActorEvent; class FireworkParticleAnimation implements Animation { @@ -21,7 +22,7 @@ public function __construct(FireworksRocket $firework) public function encode(): array { return [ - ActorEventPacket::create($this->firework->getId(), ActorEventPacket::FIREWORK_PARTICLES, 0) + ActorEventPacket::create($this->firework->getId(), ActorEvent::FIREWORK_PARTICLES, 0) ]; } } \ No newline at end of file From ab7e600a4a515d42d0b98919b622f38364042e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Le=C3=B3n?= <58715544+JavierLeon9966@users.noreply.github.com> Date: Thu, 1 Dec 2022 15:04:59 -0300 Subject: [PATCH 17/19] Updated code to 5.0.0-ALPHA5 --- src/BlockHorizons/Fireworks/Loader.php | 10 +++++++--- src/BlockHorizons/Fireworks/entity/FireworksRocket.php | 8 ++++++++ src/BlockHorizons/Fireworks/item/Fireworks.php | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 5a801f0..500af59 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -7,15 +7,17 @@ use BlockHorizons\Fireworks\entity\FireworksRocket; use BlockHorizons\Fireworks\item\Fireworks; use pocketmine\data\bedrock\EntityLegacyIds; +use pocketmine\data\bedrock\item\ItemTypeNames; +use pocketmine\data\bedrock\item\SavedItemData; use pocketmine\entity\EntityDataHelper; use pocketmine\entity\EntityFactory; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; use pocketmine\item\ItemIdentifier; -use pocketmine\item\ItemIds; +use pocketmine\item\ItemTypeIds; use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\plugin\PluginBase; +use pocketmine\world\format\io\GlobalItemDataHandlers; use pocketmine\world\World; class Loader extends PluginBase @@ -23,7 +25,9 @@ class Loader extends PluginBase public function onEnable(): void { - ItemFactory::getInstance()->register(new Fireworks(new ItemIdentifier(ItemIds::FIREWORKS, 0), "Fireworks"), true); + $fireworks = new Fireworks(new ItemIdentifier(ItemTypeIds::newId()), "Fireworks"); + GlobalItemDataHandlers::getSerializer()->map($fireworks, static fn() => new SavedItemData(ItemTypeNames::FIREWORK_ROCKET)); + GlobalItemDataHandlers::getDeserializer()->map(ItemTypeNames::FIREWORK_ROCKET, static fn() => clone $fireworks); EntityFactory::getInstance()->register(FireworksRocket::class, static function (World $world, CompoundTag $nbt): FireworksRocket { return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world), Item::nbtDeserialize($nbt->getCompoundTag("Item"))); }, ["FireworksRocket", EntityIds::FIREWORKS_ROCKET], EntityLegacyIds::FIREWORKS_ROCKET); diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index 37f57b2..e4a2217 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -45,6 +45,14 @@ public function __construct(Location $location, Fireworks $fireworks, ?int $life $location->getWorld()->broadcastPacketToViewers($this->location, LevelSoundEventPacket::nonActorSound(LevelSoundEvent::LAUNCH, $this->location->asVector3(), false)); } + protected function getInitialDragMultiplier(): float{ + return 0.99; + } + + protected function getInitialGravity(): float{ + return 0.05; + } + protected function tryChangeMovement(): void { $this->motion->x *= 1.15; diff --git a/src/BlockHorizons/Fireworks/item/Fireworks.php b/src/BlockHorizons/Fireworks/item/Fireworks.php index 363ee0a..daf5d7a 100644 --- a/src/BlockHorizons/Fireworks/item/Fireworks.php +++ b/src/BlockHorizons/Fireworks/item/Fireworks.php @@ -78,7 +78,7 @@ protected function getExplosionsTag(): CompoundTag return $tag; } - public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector): ItemUseResult + public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, array &$returnedItems): ItemUseResult { $entity = new FireworksRocket(Location::fromObject($blockReplace->getPosition()->add(0.5, 0, 0.5), $player->getWorld(), lcg_value() * 360, 90), $this); From fddb943ac1e8db28f6b4cb9b6b78505f5a3e5b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Le=C3=B3n?= <58715544+JavierLeon9966@users.noreply.github.com> Date: Thu, 1 Dec 2022 15:19:27 -0300 Subject: [PATCH 18/19] Bump api to 5.0.0 --- plugin.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.yml b/plugin.yml index 862a3c5..748fb57 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: Fireworks main: BlockHorizons\Fireworks\Loader -api: 4.0.0 +api: 5.0.0 version: 0.0.4.1 From 8d4da648f78291daa01eefd5964404f5ddc23707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Le=C3=B3n?= <58715544+JavierLeon9966@users.noreply.github.com> Date: Wed, 21 Jun 2023 18:29:51 -0300 Subject: [PATCH 19/19] Do not save fireworks to disk --- src/BlockHorizons/Fireworks/Loader.php | 11 ----------- .../Fireworks/entity/FireworksRocket.php | 8 ++++++++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/BlockHorizons/Fireworks/Loader.php b/src/BlockHorizons/Fireworks/Loader.php index 500af59..3c139d3 100644 --- a/src/BlockHorizons/Fireworks/Loader.php +++ b/src/BlockHorizons/Fireworks/Loader.php @@ -4,21 +4,13 @@ namespace BlockHorizons\Fireworks; -use BlockHorizons\Fireworks\entity\FireworksRocket; use BlockHorizons\Fireworks\item\Fireworks; -use pocketmine\data\bedrock\EntityLegacyIds; use pocketmine\data\bedrock\item\ItemTypeNames; use pocketmine\data\bedrock\item\SavedItemData; -use pocketmine\entity\EntityDataHelper; -use pocketmine\entity\EntityFactory; -use pocketmine\item\Item; use pocketmine\item\ItemIdentifier; use pocketmine\item\ItemTypeIds; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\network\mcpe\protocol\types\entity\EntityIds; use pocketmine\plugin\PluginBase; use pocketmine\world\format\io\GlobalItemDataHandlers; -use pocketmine\world\World; class Loader extends PluginBase { @@ -28,8 +20,5 @@ public function onEnable(): void $fireworks = new Fireworks(new ItemIdentifier(ItemTypeIds::newId()), "Fireworks"); GlobalItemDataHandlers::getSerializer()->map($fireworks, static fn() => new SavedItemData(ItemTypeNames::FIREWORK_ROCKET)); GlobalItemDataHandlers::getDeserializer()->map(ItemTypeNames::FIREWORK_ROCKET, static fn() => clone $fireworks); - EntityFactory::getInstance()->register(FireworksRocket::class, static function (World $world, CompoundTag $nbt): FireworksRocket { - return new FireworksRocket(EntityDataHelper::parseLocation($nbt, $world), Item::nbtDeserialize($nbt->getCompoundTag("Item"))); - }, ["FireworksRocket", EntityIds::FIREWORKS_ROCKET], EntityLegacyIds::FIREWORKS_ROCKET); } } \ No newline at end of file diff --git a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php index e4a2217..4e546e6 100644 --- a/src/BlockHorizons/Fireworks/entity/FireworksRocket.php +++ b/src/BlockHorizons/Fireworks/entity/FireworksRocket.php @@ -53,6 +53,14 @@ protected function getInitialGravity(): float{ return 0.05; } + /** + * TODO: The entity should be saved and loaded, but this is not possible. + * @see https://bugs.mojang.com/browse/MCPE-165230 + */ + public function canSaveWithChunk(): bool{ + return false; + } + protected function tryChangeMovement(): void { $this->motion->x *= 1.15;