1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Stop repeating task when arrow hits player or block

Discussion in 'Development' started by ethaniccc, Mar 26, 2020.

  1. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    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);
            }
        }
     
  2. tungstenvm

    tungstenvm Witch

    Messages:
    54
    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
     
  3. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    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.
     

    Attached Files:

  4. Heisenburger69

    Heisenburger69 Silverfish

    Messages:
    16
    GitHub:
    heisenburger69
    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
     
    RicardoMilos384 and ethaniccc like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.