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

Cooldown on event?

Discussion in 'Development' started by Zuruki, Apr 10, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    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?
     
    Last edited: Apr 11, 2017
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  3. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    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?
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    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.
     
    SOFe and Zuruki like this.
  5. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    So, is this possible? Is it possible to trigger tasks on event?
     
  6. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Yes you can.
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
    HimbeersaftLP and Zuruki like this.
  8. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    How exactly do I define cooldownarray?
     
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
    Zuruki likes this.
  10. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Yes, I'm still currently learning OOP.
    Thankyou so much for your help, I am learning even more due to everyone on PMMP forums ;)
     
    corytortoise likes this.
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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
     
  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.