Hello every1. i want to learn about creating task or timer with PocketMine API. so i hope someone (you) who understand with it, please explain on comment session. for example i want prevent player to move before 10 second (20*10 = 200) join an Arena. so i canceled the current event. and then, after 5 minutes all players will teleport to spawn from Arena, to end the game. i still dont get it to implement Timer with Listener or other function. Thank you!
Tasks are pretty straightforward. You need to create a class: PHP: <?phpnamespace username\PluginName;use pocketmine\scheduler\PluginTask;class ExampleTask extends PluginTask{ private $plugin; public function __construct($plugin){ parent::__construct($plugin); $this->plugin = $this->getOwner(); } public function onRun($currentTick){ //repeating code (You can get your server object through $this->plugin->getServer();) }} and then just use the server object to schedule it: (Assuming you're in a class extending PluginBase PHP: $this->getServer()->getScheduler()->scheduleRepeatingTask(new ExampleTask($this), 20); //20==every secound
Timer Scheduler & Tasks are for doing an action after a period of time, not having a state within a period of time. (The player will be unfrozen 10 seconds later vs The player cannot move for 10 seconds) What is the difference? For the former, "be unfrozen" is the action, while for the latter, "be immobile" is the state. How is a state different from an action? For an action, you do something actively. For example, "kill the player" is an action, but "the player is dead" is a state, a description, a condition. In programming, the things you put in a function are always actions. You change a value, you call a function, you stop the code from executing. How does a condition affect execution? A condition is queried upon when something is executing, and it does not actively executes. Your condition is "cannot move". What executions will query this condition? Of course, you only need to know whether the player can move when the player tries to move. This is, as you said, cancel an event. You handle a PlayerMoveEvent and cancel it if the condition applies. To be most convenient, you can change a value to make a player frozen/unfrozen, but this does not exist in PocketMine (there is Player->blocked, but it has side effects and it isn't very reliable). Therefore, you have to make PocketMine ask you whether the player can move every time the player tries to move. How do you yourself know whether the player can move? You ask yourself whether it is within the 10 seconds you asked for. But the time that code executes changes - how do you know if it is that relative 10 seconds? Therefore, we make the relative time absolute. For example, if I tell you that I typed this sentence in the past minute, you don't know when I typed this sentence. So how do you find it out? You look at the time this post is posted, and minus one miniute, to know what I meant by "in the past minute". Without the post timestamp from the forums? I have to tell you the exact (absolute) date and time, that is, 2016-11-16 17:38:05 UTC+8. Same goes to your code. You write down that "It is now 2016-11-16 17:38:05 UTC+8, and for the next 10 seconds the player can't move". Since time only goes forwards, I can make it even more straightforward: "The player can't move before 2016-11-16 17:38:15 UTC+8". An absolute time can be retrieved using the time() or microtime() function. So, you only store the timestamp for the "unfreeze" time, then in the PlayerMoveEvent handler, you check if the current time is less than the "unfreeze" time. You don't need to check if it is less than the "freeze" time, because time only goes forwards. You don't need to use scheduler tasks at all.
Thanks SOF3 and Tim, yea i need someone to explain it and someone to make an example for it.. so i can assume it to my code perfectly and i have a new knowledge about that.