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

Cancel Task for a player

Discussion in 'Development' started by VentroxStudio, Jan 7, 2017.

  1. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    Hey.
    Is it possible to cancel a task for only one player?

    With this code I cancel it normally.
    PHP:
    $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId());
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    FYI you can only cancel a task for a player IF you start the tasks separately for each players
     
    applqpak likes this.
  3. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    I started it using this:
    PHP:
    $task = new ChestTask($this$player);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    you can have the task check the plugin for players in an array and only run using an if statement in the task
     
    applqpak likes this.
  5. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    I have a countdown. If the player did something he starts the task. But if another player starts the a countdown and the first player stopped his countdown, the second countdown also stops
     
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This should work for onRun()
    PHP:
    foreach($this->plugin->counting as $player) {
        if(
    $player === $this->player) {
            return;
        }
        
    #run the task
    }
     
    applqpak likes this.
  7. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    What is counting?
     
  8. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    I'm searching for something like this
    PHP:
    $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId(), $player);
    I already have a own countdown for each player
     
    Last edited: Jan 7, 2017
  9. Aviv

    Aviv Baby Zombie

    Messages:
    156
    this looks like you add seperate tasks for each player that activated it
    then just this
    PHP:
    $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId());
    should work fine
     
  10. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    But I use foreach and getOnlinePlayers to define $player. Do I need to get the player from where I start the task?
     
  11. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    Ist there a way to do it like this?
     
  12. Primus

    Primus Zombie Pigman

    Messages:
    749
    My way of doing this, if it's a delayed task - pass a player name to Task, don't pass Player object. Now on Task::onRun() get the Player object by calling Server::getPlayer() run through null check and execute a code. If you do not understand what I just said, give up.
    How do you know which player to pick? (username, eID, UUID, ClientID etc.)
     
  13. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    I use this code:

    PHP:
    class ChestCloseTask extends PluginTask {

    public 
    $sec = array();
        public function 
    __construct(Plugin $owner){
            
    parent::__construct($owner);
        }
        public function 
    onRun($currentTick)
        {
        
    $players $this->getOwner()->getServer()->getDefaultLevel()->getPlayers();
       
    foreach (
    $players as $player) {

    $name $player->getName();
            
            
            
           
            if(
    $this->getOwner()->sec[$name] >= 1) {
            
    $this->getOwner()->sec[$name] = --$this->getOwner()->sec[$name];


    But how can I stop it for each player?
     
  14. Primus

    Primus Zombie Pigman

    Messages:
    749
    Code:
    foreach (players as player) {
       if ( player record exists ) continue;
       countdown(player);
    }
    
     
  15. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    And if the timer is at 0 set it to -1?? Or what ist Player record?

    PHP:
    if($player->countdown != -1){}
     
  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.