How do I create a cooldown on an event? For example a person can only eat a golden apple every 30 seconds. Using plugin task? If so, where can I learn about tasks?
This is a question that has been asked many times before. I recommend searching for the answer before creating a new thread https://forums.pmmp.io/search/2076028/?q=task&o=date&c[title_only]=1
Okay, but how can I cancel an event on a task? Like, a person eats something //task is triggered. During the time that the task is running, the event will be cancelled. When the task is over, a player can eat again. Basically a cooldown, right? Then when the player eats the same thing happens again. How can I do this?
You use a task to make something happen after a certain amount of time, or every * time. You can store time() in a variable or an array and check if time() - $timeVariable > 30, then execute code.
Yes, you can start a task when an event is called. It just isn't entirely necessary to use a task here. I suggest doing what Sandertv suggested: When a Player performs a certain action(in this case, eats a Golden Apple), you would check to see if the player is in a cooldown. Here is my concept: PHP: public function onConsume(PlayerItemConsumeEvent $event) { if($event->getItem()->getId() === Item::GOLDEN_APPLE) { //Using isset() may be unnecessary. if(isset($this->cooldownArray[$event->getPlayer()->getName()]) && $this->cooldownArray[$event->getPlayer()->getName()] - time() < 30) { //Player ate a Golden Apple within the last 30 seconds $event->setCancelled(); } else { //There is no cooldown for the player. $this->cooldownArray[$event->getPlayer()->getName()] = time(); } } } Where $this->cooldownArray is a global array that you define towards the top of the file, time() gets the timestamp from the machine the server is running on, and we access the time for each player by using their username as the key for the array.
You can do it right under the class declaration. PHP: namespace MyPlugin\MyPlugin; class Main extends PluginBase implements Listener { /* This can be public if you plan to access it from other classes, however it shouldn't be necessary here, so I made it private. */ private $cooldownArray = array(); //The rest of your plugin below. } If you don't know about them, you should check out global variables in PHP, they can be very useful.
Yes, I'm still currently learning OOP. Thankyou so much for your help, I am learning even more due to everyone on PMMP forums
That means you should not use a task if you want to cancel events. That means you should use time() to detect time difference rather than use a task to execute something. Googling "site:forums.pmmp.io timer cancel event" gives this post I posted months ago, which solves probably at least 10 other threads on this forum: https://forums.pmmp.io/threads/learning-timer-or-task.57/#post-826