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

Stop player moving on task

Discussion in 'Development' started by Nora1903, Jul 15, 2018.

Thread Status:
Not open for further replies.
  1. Nora1903

    Nora1903 Slime

    Messages:
    82
    GitHub:
    cuongvnz
    Like the title, how can i cancel PlayerMoveEvent on classTask?
    PHP:
    class MPCast extends Task{
        
        public 
    $main;
        public 
    $player;
        public 
    $second 5;
        
        public function 
    __construct(Loader $main,Player $player){
            
    $this->main $main;
            
    $this->player $player;
        }
        
        public function 
    onRun(int $tick){
            if(
    $this->second 0){
                
    //code here
                
    $ev->setCancelled(true);
            }
            if(
    $this->second == 0){
                
    $food $this->player->getFood()+5;
                if(
    $food >= 20){$food 20;}
                
    $this->player->setFood($food);
                
    $this->player->addTitle('+5 MP');
                
    $this->main->getScheduler()->cancelTask($this->getTaskId(),$this->player);
            }
            
    $this->player->sendPopup($this->second);
            
    $this->second--;
        }
    }
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    In your PlayerMoveEvent handler, you could check if the task is running. If it is, cancel the event. Or, you could just use Player->setImmobile(), since that will prevent any movement at all until you set them mobile again. However, you can't do it like you intended, because you can't just conjure up an event to cancel when the task is running. You can, however, do like I mentioned.
     
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    Off topic, but great one-liner
    PHP:
    $this->player->setFood(min(20$this->player->getFood() + 5));
    // Hardcoded values FTW?
    You can use something like this
    PHP:
    # __construct
    MainPlugin::freeze(Player)

    # MainPlugin::freeze
    $this->frozen[Player] = true;

    # PlayerMoveEvent
    if($this->frozen[Player]) $event->setCancelled();
    Edit: I know that I've mixed up static and non-static properties, but it's only proof of concept
     
    Last edited: Jul 16, 2018
  4. Flavius12

    Flavius12 Spider Jockey

    Messages:
    32
    GitHub:
    flavius12
    Why are you using a Task when you can handle PlayerMoveEvent directly?
     
  5. Nora1903

    Nora1903 Slime

    Messages:
    82
    GitHub:
    cuongvnz
    i want to stop play moving for few seconds
     
  6. Flavius12

    Flavius12 Spider Jockey

    Messages:
    32
    GitHub:
    flavius12
    You can't do in a task. You can try with
    PHP:
    $player->setImmobile()
    as @corytortoise suggests or you can do a small trick using PHP time() function in a way like this:
    PHP:
    class YourEventListener implements Listener {
        private 
    $freezed = array();
        public function 
    onPlayerMove(PlayerMoveEvent $ev){
            
    $p $ev->getPlayer();
            if(isset(
    $this->freezed[$p->getName()]){
                if(
    time() - $this->freezed[$p->getName()] < 10){ //The player will be freezed for 10 seconds
                    
    $ev->setCancelled();
                }else{
                    unset(
    $this->freezed[$p->getName()]);
                }
            }
        }
    }
    Then to freeze a player:
    PHP:
    $this->freezed["player_name"] = time();
    This is only an example code. Change the code according to your needs.
     
    xXNiceAssassinlo YT likes this.
  7. Nora1903

    Nora1903 Slime

    Messages:
    82
    GitHub:
    cuongvnz
    Is it able to setImmobile on Monster and animal ?
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Last edited: Jul 16, 2018
    Marabou and corytortoise like this.
Thread Status:
Not open for further replies.
  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.