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($task, 56); 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.
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..
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 $p, Player $player){ $player->setImmobile(true); parent::... } public function onCancel() : void{ $this->player->setImmobile(false); }}
How can i block the player to chat and running commands, when the task is running (__construct)? Like you do with setImmobile..?
Save the player by name or UUID. Check if player is saved. Cancel PlayerCommandPreprocessEvent if so. Unset the player in the delayed task.
Compare the player's name to the one you saved in your array. Spoiler: example PHP: public function(MyFavouriteEvent $event) { if ($event->getPlayer()->getName() === "turtles" ) { // do stuff }}
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?
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.
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($task, string $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); }}