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

How can i know if a scheduleDelayedTask is finish and running?

Discussion in 'Development' started by LewBr, Jan 31, 2018.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Hello there, i know need to explain more..

    For example i have a delayed task when player joins the server:

    PHP:
        $task = new BlaBla($this->plugin$event->getPlayer());
    $this->getServer()->getScheduler()->scheduleDelayedTask($task56);
    and i want know and stop the player to do something when the delayedtask is running and when they finish too, for example:
    PHP:
    if(running($task)){
    $player->sendMessage("You can't do anything when the task is running, please wait before they finish.");
    }
    //or something like that when finish:
    if(finished($task)){
    $player->sendMessage("The delayed task has been finished :).");
    }
    How can i do like that? should i do something on the function onRun on the Task file?
    Please, i know that "finished" and "running" don't do anything, i am just trying to explain.
     
    Last edited: Jan 31, 2018
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Player::setImmobile() and gamemode adventure?
     
  3. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    What you mean by that? I just want to send something to a player when the delayed task has been finished and when the task is running and while that i want to block the player to do something, like walking, chatting etc..
     
    Last edited: Feb 1, 2018
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    setImmobile() will make sure the player doesn't move and setting their gamemode to adventure mode will make sure they cannot place, break and fire many of the events. Of course your can customize it to eliminate the drawbacks of builtin solutions. You can call those functions in your task's constructor as you're executing the task right as you create an instance of it. Alternatively, have an array property in your main class and inject values in it from the task's constructor and handle the array accordingly.

    PHP:
    class BlaBla extends PluginTask{

        public function 
    __construct(Plugin $pPlayer $player){
            
    $player->setImmobile(true);
            
    parent::...
        }

        public function 
    onCancel() : void{
            
    $this->player->setImmobile(false);
        }
    }
     
    Last edited: Feb 1, 2018
    LewBr likes this.
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    How can i block the player to chat and running commands, when the task is running (__construct)? Like you do with setImmobile..?
     
    Last edited: Feb 1, 2018
  6. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Save the player by name or UUID. Check if player is saved. Cancel PlayerCommandPreprocessEvent if so. Unset the player in the delayed task.
     
    LewBr likes this.
  7. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    How do i register an event to an specific saved player name?
     
  8. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Compare the player's name to the one you saved in your array.
    PHP:
    public function(MyFavouriteEvent $event) {
      if (
    $event->getPlayer()->getName() === "turtles" ) {
        
    // do stuff
      
    }
    }
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I don't think you even need a Task for whatever you're trying to do. Is cancelling events all that you're doing? Does the your task's onRun() have an empty body?
     
  10. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    onRun has too much of things, i am making a check verifying account, that's because i want to block a player to do some things when the taks is running.
     
  11. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Try this
    PHP:
    public $tasks = [];

    public function 
    isTaskRunning($name){
       if(!isset(
    $this->tasks[strtolower($name)])){
          return 
    false;
         }
         return 
    true;
    }

    public function 
    onJoin(PlayerJoinEvent $e){
       
    $this->startTask(new YourTask($e->getPlayer(), $this"test"), $e->getPlayer()->getName());
    }

    public function 
    removeTask($name){
         
    $this->tasks[strtolower($name)] = null;
    }

    public function 
    startTask($taskstring $name){
    $this->tasks[strtolower($name)] = new $task;
    }
    And in your task
    PHP:
    class YourTask extends Task{

        public 
    $plugin;

        public 
    $taskName;

        public function 
    __construct($player$main$taskName){
           
    $this->plugin $plugin;
          
    $this->taskName $taskName;
        }

        public function 
    onClose(){
          
    $this->plugin->removeTask($this->taskName);
        }

    }
     
    LewBr likes 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.