Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Best way to use Timers

Comments in 'Plugin Development' started by Taha_The_Hacker, Nov 10, 2015.

  1. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Hi,

    i know that there are Tasks to use that, but they lag..
    i want to run the code after x seconds 1 time
    i've used sleep(), but it'll stop the whole server from working,
    so anything like sleep() ? because the task Lags
    I checked 1vs1 plugin, but no.
    in php bin file that PM used, there isn't EvWatcher or EvTimer (BEST TIMERS)

    Thanks
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Never use sleep() in PM. Use a task. The best and only way.
  3. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    they "Lagged" me in a spleef plugin (or maybe another thing did)
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    ??? A task won't lag your server.
    Taha_The_Hacker likes this.
  5. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    omg then it's my wifi fault in localhost either..
    i'll try again
  6. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Correction: Never use sleep() for tasks. It delays execution of everything in the thread it is run on. Use it for other stuff. :p
    Legoboy0215 likes this.
  7. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Depends on what the task does.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You basically don't want to use sleep() on the main thread. Don't think sleep() in the way it "delays execution for a few seconds". Think it as that sleep() "lags your server for a few seconds". Now scared? :D
    A task doesn't lag your server. But if you schedule new tasks every time with a new handler, then it lags. Why not just use a repeating task?
    Yes I know you don't want it to be run indefinitely. Then simply make one task that checks all spleef matches on the server per tick, and tick the matches if needed, like this:
    https://github.com/LegionPE/Matches-Base/blob/master/src/legionpe/theta/match/match/MatchTicker.php
    Taha_The_Hacker and Gamecrafter like this.
  9. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    so...
    will this work?
    PHP:
    public function onRun($currentTick){
    if(
    $currentTick === 20){
    //Now??
    }
    }
  10. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
  11. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    already did.

    then i'll use
    PHP:
    //Main
    public $sec 0;
    //start the repeating task 1 sec

    //Task

    public function onRun($currentTick){
    $this->getOwner()->sec++;
    if(
    $this->getOwner()->sec === 10){
    echo 
    "Now?";
    }

    }
  12. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
    PHP:
    //Main
    public $sec 0;
    //start the repeating task 1 sec

    //Task

    public function onRun($currentTick){
    $this->getOwner()->sec++;
    if(
    $this->getOwner()->sec === 10){
    echo 
    "Now?";
    $this->getOwner()->sec 0//make $sec 0 again
    }

    }
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Reset the timer to 0 and execute the code you want to execute.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The point is, you should make one permanent repeating task that will run code for however many times when needed, do nothing when not needed.
  15. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I created several tasks. for($i=30;$i>=0;$i--){new delayed task (time);}
    Because i cant cancel a repeating task.. cancelTaskId did never cancel it. it just continued
  16. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    to cancel it, I think you should start it with
    PHP:
    $this->myAwesomeTask $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20)->getTaskId();
    and end it with
    PHP:
    $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getOwner()->myAwesomeTask);
    Last edited: Nov 13, 2015
  17. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    You need getTaskId when you init it? Oh, i thought when you cancel it only.
    I had this:
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask($this->tasks2[$entity->getName()] = new SendTip($this$this->tasks2), 1);
    Nice, i'll give it a try
  18. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    What do you mean?
  19. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    nothing just read the my post (post edited)
  20. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    :) i see no changes. But thanks anyways, now my tasks get cancelled :D
    Taha_The_Hacker likes this.

Share This Page

Advertisement