From 4357c110c8056a45c4df403334afd4691542b3e6 Mon Sep 17 00:00:00 2001 From: Armen Deroian Date: Tue, 6 Dec 2022 09:19:14 -0500 Subject: [PATCH] Add the event: WorldParticleEvent (#5428) --- src/event/world/WorldParticleEvent.php | 73 ++++++++++++++++++++++++++ src/world/World.php | 17 ++++-- 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/event/world/WorldParticleEvent.php diff --git a/src/event/world/WorldParticleEvent.php b/src/event/world/WorldParticleEvent.php new file mode 100644 index 000000000..5fe5ca76a --- /dev/null +++ b/src/event/world/WorldParticleEvent.php @@ -0,0 +1,73 @@ +particle; + } + + public function setParticle(Particle $particle) : void{ + $this->particle = $particle; + } + + public function getPosition() : Vector3{ + return $this->position; + } + + /** + * @return Player[] + */ + public function getRecipients() : array{ + return $this->recipients; + } + + /** + * @param Player[] $recipients + */ + public function setRecipients(array $recipients) : void{ + $this->recipients = $recipients; + } +} diff --git a/src/world/World.php b/src/world/World.php index 42c2a16fa..3fa5c8187 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -50,6 +50,7 @@ use pocketmine\event\world\ChunkLoadEvent; use pocketmine\event\world\ChunkPopulateEvent; use pocketmine\event\world\ChunkUnloadEvent; use pocketmine\event\world\SpawnChangeEvent; +use pocketmine\event\world\WorldParticleEvent; use pocketmine\event\world\WorldSaveEvent; use pocketmine\event\world\WorldSoundEvent; use pocketmine\item\Item; @@ -692,14 +693,24 @@ class World implements ChunkManager{ * @param Player[]|null $players */ public function addParticle(Vector3 $pos, Particle $particle, ?array $players = null) : void{ - $pk = $particle->encode($pos); + $players ??= $this->getViewersForPosition($pos); + $ev = new WorldParticleEvent($this, $particle, $pos, $players); + + $ev->call(); + + if($ev->isCancelled()){ + return; + } + + $pk = $ev->getParticle()->encode($pos); + $players = $ev->getRecipients(); if(count($pk) > 0){ - if($players === null){ + if($players === $this->getViewersForPosition($pos)){ foreach($pk as $e){ $this->broadcastPacketToViewers($pos, $e); } }else{ - $this->server->broadcastPackets($this->filterViewersForPosition($pos, $players), $pk); + $this->server->broadcastPackets($this->filterViewersForPosition($pos, $ev->getRecipients()), $pk); } } }