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

Cooldown?

Discussion in 'Development' started by Junkdude, Dec 3, 2016.

Tags:
  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Is there a way to set a cooldown with a plugin task?
     
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    It depends on what you wanna use it for?
     
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Cooldown on blockbreakevent
     
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Define 'cooldown'. It can mean many things depending on context and what you imagine it to mean.
     
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    you can trigger an event after an amount of seconds
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Then schedule a task with a delay.
     
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Dont know how to
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    No, use time() function in PHP.
    Then learn the API, I'm not trying to be mean but this is for help with plugin bugs or issues and question about the API, not help with learning the API.
     
  9. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You can see the basics of creating a task here. You obviously have some knowledge of programming or you would be in the plugin request section so have a go at making that example work for a delayed task.
    PHP:
    pocketmine\Server::getScheduler()->scheduleDelayedTask(20 /* time in ticks to be delayed */PluginTask /* Your pocketmine\scheduler\PluginTask instance */);
     
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I have code but I get an error,
    PHP:
    <?php

    namespace Core;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Cancellable;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\utils\Config;

    class 
    Mining implements Listener {

        public function 
    __construct(Loader $plugin){
              
    $this->plugin $plugin;
        }
     

        public function 
    OnBreak(BlockBreakEvent $event){
            
    ###VARIABLES###
            
    $name $event->getPlayer();
            
    $this->array = array();
            
    $this->array[$name] = time();
            
    ##$this->miningskills = new Config($this->getDataFolder() . "/miningskills.yml", Config::YAML);
            ##$inventory = $event->getPlayer()->getInventory(); FOR FUTURE USE!
            ##$breaks = $this->miningskills->get($name);
            ##$this->miningskills->set($name,$breaks+1);
            ##$this->miningskills->save();
            
    $last_usage_time $this->array[$name];
            
    ###/VARIABLES###
            
    if(isset($this->array[$name])){
                if((
    time() - $last_usage_time) <= 5){
            if(
    $event->getBlock()->GetId() == or or 3){
            
    $event->getPlayer()->sendMessage("TEST");
        }
    }else if((
    time() - $last_usage_time) >= 5){
        
    $name->sendMessage("THIS SKILL IS CURRENTLY IN COOLDOWN!");
        }
    }
    }
    }
    ERROR: illegal offset type in isset or empty
     
  11. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    time() does not automatically call a function or execute code once a certain amount of time has passed, it simply returns a timestamp.
     
  12. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    It's
    PHP:
    $name $event->getPlayer()->getName();
    The getPlayer() method returns a player object, not a string.
     
  13. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    facepalm
     
  14. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    time() is a function that can be used for cooldown.
     
  15. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    It works and sends the first message now except, if its lower then 5 seconds it doesnt trigger the message
     
  16. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Time is a function for retrieving a timestamp based on the machines Timezone configuration. If you want to store and compare a time value then you're better off using microtime() unless of course you plan on using the value to compare more than just hours, minutes and seconds.
     
  17. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    yeah im doing it like every 180 seconds, so how would i use microtime()?
     
  18. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    PHP:
    // store the micro seconds timestamp in a variable or property
    $storedTime microtime(true);
    // difference in seconds between the stored time and current time
    $differance floor(microtime(true) - $storedTime);
    // compare the time difference to however many seconds you want the cooldown to last
    if(!$differance >= 5) {
        echo 
    "The cool down is still in progress";
    }
     
  19. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Microseconds is basically seconds*1 million.
    PHP:
    if((microtime(false) - $last_time_used) >= 1.8e+8){
    //your code here
    }
     
  20. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

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