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

Solved Run sound for once and stop!

Discussion in 'Development' started by KHAV, Aug 15, 2017.

  1. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    Hello PMMP Forum

    i used in my plugin PlayerMoveEvent and i added AnvilBreakSound when you move but AnvilBreakSound repeats every second i want it to run for once and stop or run for once every like 5 minutes.

    How i can do it?

    This is the code if you want:
    PHP:
    public function onMove(PlayerMoveEvent $event)
    {
             
    $player $event->getPlayer();
         
    $x1 $this->getConfig()->get("x1");
         
    $y1 $this->getConfig()->get("y1");
         
    $z1 $this->getConfig()->get("z1");
         
    $x2 $this->getConfig()->get("x2");
         
    $y2 $this->getConfig()->get("y2");
         
    $z2 $this->getConfig()->get("z2");
         
    $level $this->getConfig()->get("level");
         if(
    $this->isInside($player,new Vector3($x1,$y1,$z1,$level),new Vector3($x2,$y2,$z2,$level))){
                     
    $item Item::get(30201);
                     
    $item->setCustomName("§9Space §fHelmet");
                     
    $item->addEnchantment(Enchantment::getEnchantment(0)->setLevel(1));
                     
    $hed $player->getInventory()->getHelmet();
                     if (
    $hed->getCustomName() == $item->getCustomName()) {
                         if (
    $hed->getEnchantment(0)) {
                             if (
    $hed->getId() == 302) {
                                 
    $player->removeAllEffects();
                                 
    $player->addEffect(Effect::getEffect(10)->setDuration(5)->setAmplifier(0)->setVisible(true));
                                 
    $player->addEffect(Effect::getEffect(2)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
                                 
    $player->addEffect(Effect::getEffect(8)->setDuration(999999999)->setAmplifier(1)->setVisible(true));

                             }
                         }
                     } else {
                          
    // Here is the sound
                         
    $player->addSound(new AnvilBreakSound($player));
                         
    $player->sendMessage(TextFormat::RED "Warning!! You are suffocating, Please wear the §9Space §fHelmet §4to breathe");
                         
    $player->removeAllEffects();
                         
    $player->addEffect(Effect::getEffect(19)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
                         
    $player->addEffect(Effect::getEffect(9)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
                     }
                 } else {
                     
    $player->removeAllEffects();
             
    $item Item::get(30201);
             
    $item->setCustomName("§9Space §fHelmet");
             
    $item->addEnchantment(Enchantment::getEnchantment(0)->setLevel(1));
             
    $player->getInventory()->remove($item);
                 }
             }
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    at first: Please use Effect constants https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/entity/Effect.php#L36-L59. Thanks!
    for your problem, i think you basically want to play the sound (and maybe the warning message) on enter. Actually, you may even want to run the whole code only once on enter.
    You may consider to use a task in general, because onMove is called quite often and depending on how your isInside implementation is it might be time expensive. But that's not important for your question.
    Finally back to your question,just have an array with the eids of the players that are already in the area. With that you can determine if he is new there and you haven't played the sound for him yet or not.
    PHP:
    private $insidePlayers;

    public function 
    onMove(PlayerMoveEvent $event){
        
    $player $event->getPlayer();
        
    $x1 $this->getConfig()->get("x1");
        
    $y1 $this->getConfig()->get("y1");
        
    $z1 $this->getConfig()->get("z1");
        
    $x2 $this->getConfig()->get("x2");
        
    $y2 $this->getConfig()->get("y2");
        
    $z2 $this->getConfig()->get("z2");
        
    $level $this->getConfig()->get("level");
        if(
    $this->isInside($player, new Vector3($x1,$y1,$z1,$level), new Vector3($x2,$y2,$z2,$level))){
            
    $item Item::get(30201);
            
    $item->setCustomName("§9Space §fHelmet");
            
    $item->addEnchantment(Enchantment::getEnchantment(0)->setLevel(1));
            
    $hed $player->getInventory()->getHelmet();
            if(
    $hed->getCustomName() == $item->getCustomName()){
                if(
    $hed->getEnchantment(0)){
                    if(
    $hed->getId() == 302){
                        
    $player->removeAllEffects();
                        
    $player->addEffect(Effect::getEffect(10)->setDuration(5)->setAmplifier(0)->setVisible(true));
                        
    $player->addEffect(Effect::getEffect(2)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
                        
    $player->addEffect(Effect::getEffect(8)->setDuration(999999999)->setAmplifier(1)->setVisible(true));

                    }
                }
            }else{
                if(!
    in_array(($playerId $player->getId()), $this->insidePlayers)){
                    
    $this->insidePlayers[] = $playerId;
                    
    //player just entered the area
                    
    $player->addSound(new AnvilBreakSound($player));
                    
    $player->sendMessage(TextFormat::RED "Warning!! You are suffocating, Please wear the §9Space §fHelmet §4to breathe");
                }
                
    $player->removeAllEffects();
                
    $player->addEffect(Effect::getEffect(19)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
                
    $player->addEffect(Effect::getEffect(9)->setDuration(999999999)->setAmplifier(0)->setVisible(true));
            }
        }else{
            
    $player->removeAllEffects();
            
    $item Item::get(30201);
            
    $item->setCustomName("§9Space §fHelmet");
            
    $item->addEnchantment(Enchantment::getEnchantment(0)->setLevel(1));
            
    $player->getInventory()->remove($item);
        }
    }
     
  3. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Save the player id as a key and use `isset` for faster performance.
     
    jasonwynn10 likes this.
  4. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    Thanks Solved!
     
  5. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    yup, that would be faster
     
  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.