I have this function that makes it so when a player shoots an arrow particles follow the arrow. However, when the arrow hits a block or a player, it still continues the repeating task. Can someone help me, please? My function: PHP: public function shootBow(EntityShootBowEvent $event){ $arrow = $event->getProjectile(); $player = $event->getEntity(); if($player instanceof Player){ $this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick) use ($player, $arrow) : void{ if($arrow !== null){ $player->getLevel()->addParticle(new LavaParticle(new Vector3($arrow->getX(), $arrow->getY(), $arrow->getZ()))); } }), 1); } }
i dont think this idea is that simple you may need to use both 2 events ProjectileHitBlockEvent and ProjectileHitEntityEvent and the event in your code (or ProjectileLaunchEvent) PHP: public $saveId = [];public function shootBow(EntityShootBowEvent $event){ $arrow = $event->getProjectile(); $player = $event->getEntity(); if($player instanceof Player){ $this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick) use ($player, $arrow,$saveId) : void{ if($arrow !== null){ $player->getLevel()->addParticle(new LavaParticle(new Vector3($arrow->getX(), $arrow->getY(), $arrow->getZ()))); $saveId[$arrow->getSaveId()] = [$this->getTaskId()]; } }), 1); }}public function onHitBlock(ProjectileHitBlockEvent $event){ $entity = $event->getEntity()->getSaveId(); if(array_key_exists($entity,$this-->saveId()){ $this->getServer()->getScheduler()->cancelTask($this-->saveId()[$entity]); }} im not sure if that idea will gonna work but that what i think
I figured it out by using isFlaggedForDespawn My current code: PHP: public function shootBow(EntityShootBowEvent $event){ $arrow = $event->getProjectile(); $player = $event->getEntity(); $saveId = $this->saveId; if($player instanceof Player){ $this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick) use ($player, $arrow, $saveId, $event) : void{ if(!$arrow->isFlaggedForDespawn()){ $player->getLevel()->addParticle(new LavaParticle(new Vector3($arrow->getX(), $arrow->getY(), $arrow->getZ()))); } }), 1); } } public function onHitBlock(ProjectileHitBlockEvent $event){ $entity = $event->getEntity()->getSaveId(); $event->getEntity()->flagForDespawn(); } It also works when the arrow hits a player, since it's flagged for despawn when it hits a player. Edit: I don't know why an image is there, just ignore it.
A simpler way would be to create an entity extending arrow, set it as the projectile in EntityBowShootEvent, then have it spawn the particle while it's not flagged for despawn