Wait 60 Minutes for a task to be done.

Discussion in 'Plugin Development' started by nicholasntp, Mar 30, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    nicholasntp

    I know how to sort of use the scheduler, but im not totally sure what I should do when I want a task to start in 60 minutes after I specify the timer to start. Then after the task is run, do NOT run the timer again.
     
  2. Offline

    Taco

    Look into using a syncDelayedTask. The delay for it is in server ticks, and there are 20 ticks in a second. So you'd want to use 72000 as your delay for a 60 minute delay.
     
  3. Offline

    nicholasntp

    Ok great! Thanks for the help! Now how would I check the time remaining? I think i might know how, but how SHOULD I do it? Because I usually do WAY more than I need to.

    Probably make another task that runs every 20 or so ticks, right?

    Any suggestions?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  4. Offline

    Technius

    Code:java
    1. getServer().getScheduler().scheduleSyncDelayedTask(this, runnable, 20*60*60);
     
  5. Offline

    nicholasntp

    Already solved that, but I had another question. Thanks though.
    -------------------------------------\/-----------------------------------------
     
  6. Offline

    CorrieKay

    you need to check the time left over?

    Instead of checking how much time is left over, you should set a task to run every 20 ticks (second) that counts down seconds in its object, which should be stored in an int, or whichever of your choice. you can then read from there.
     
  7. Offline

    nicholasntp

    Example?
    But then I need to check when it gets to 0. Another task?
     
  8. Offline

    dillyg10

    Lemme give u an example of what CorrieKay is saying...
    Code:java
    1.  
    2. public Map<Player, Integer> timeRemaining = new HashMap<Player, Integer>();
    3. public Map<Player, Boolean> inDelay = new HashMap<Player, Boolean>();
    4. // set up the 2 hashmaps somewhere make SURE they are not null.
    5. public void RepeatingRun(final Player p){
    6. myPlugin.getServer().getScheduler().scheduleAsyncRepeatingTask(myPlugin, new Runnable() {
    7.  
    8. public void run() {
    9. for (Player p : getServer().getOnlinePlayers()) {
    10. if (!indelay.get(p)) continue;
    11. int time = inDelay.get(p);
    12. time -= 1;
    13. inDelay.put(p,time);
    14. System.out.println(""+time);
    15. }
    16. }
    17. }, 20L, 200L);
    18. }
     
  9. Offline

    nicholasntp

    Eww Eww Formatting. Anyway to fix that? Hit edit, Highlight all, then theres like a white eraser thingy
     
  10. Offline

    CorrieKay

    Okay, im gonna assume that you want to create a countdown timer of an hour.

    you'd do this

    Code:
    public class CountdownTimer extends TimerTask(){
    int secondsPassed =0;
    public void run(){
    secondsPassed++;
    if(secondsPassed>=3600){
    //code to execute if an hour has passed. Possibly to tell another function to remove the task from the scheduler?
    }
    }
    public int secondsPassed(){
    return secondsPassed;//lets you see the amount of time that has passed.
    }
    }
    In the main class, or whatever class will handle the task, you'd do this.

    Code:
    int hourCountdownTask;
    hourCountdownTask = scheduler.scheduleAsyncRepeatingTask(plugin, task, 0[aka start executing now], 20 [aka execute once per second]);
     
    public void cancelCountdownTask(){
    scheduler.cancelTask(hourCountdownTask);
    }
     
  11. Offline

    dillyg10

    Fixed format.. soz about that XD.
     
  12. Offline

    nicholasntp

    Error here...

    Code:java
    1.  
    2. hourCountdownTask = scheduler.scheduleAsyncRepeatingTask(plugin, new CountdownTimer(), 0, 20);
    3.  

    Code:
    The method scheduleAsyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (-Plugin Name Here-, CountdownTimer, int, int)
    
    Oh, Its fine! :p but why hashmaps with a player??? There isnt a player... lol.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  13. Offline

    CorrieKay

    just in case you wanted to create a countdown for individual players, probably

    Lemme see the CountdownTimer class you made

    also, most of what i wrote up there is psuedo code, and is only designed to show you the core functionalities. I'd recomend making your own

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  14. Offline

    nicholasntp

    Ahh I see.

    Ok, here you go.
    CountdownTimer Class (open)
    Code:java
    1.  
    2. import java.util.TimerTask;
    3.  
    4. public class CountdownTimer extends TimerTask {
    5. intsecondsPassed =0;
    6.  
    7. public int secondsPassed(){
    8. returnsecondsPassed;
    9. }
    10. public void run(){
    11. secondsPassed++;
    12. if(secondsPassed>=3600){
    13. //code to execute if an hour has passed. Possibly to tell another function to remove the task from the scheduler?
    14. }
    15. }
    16.  
    17.  
    18.  
    19. }
    20.  
     
  15. Offline

    dillyg10

    oh lol :D. Then just use an int, I assumed u were having a player.
     
  16. Offline

    nicholasntp

    Ahh got it.
     
  17. Offline

    CorrieKay

    lolWAT. longs??

    i just saw that, hold on... that makes NO SENSE D:

    Lemme see the class file that the scheduler is in.
     
  18. Offline

    nicholasntp

    kk

    Part1:
    Code:java
    1.  
    2. public BlaBlaBla plugin;
    3. int hourCountdownTask;
    4. BukkitScheduler scheduler = this.getServer().getScheduler();
    5.  


    Part2
    Code:java
    1.  
    2. public void onEnable() {
    3. logger.info("BlaBla v1.0 Enabling");
    4. SetupConfigFiles();
    5. hourCountdownTask = scheduler.scheduleAsyncRepeatingTask(plugin, new CountdownTimer(), 0, 20);
    6.  
    7. }


    Wait! Now its working! What the?!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  19. Offline

    CorrieKay

    Few issues, line 3 doesnt have a space between int and the variable name, and "plugin" isnt a declared variable. replace that with "this"

    other than that... Uh, well, theres nothing wrong with it. I just wrote it exactly like you did, and im not getting any errors.

    I have no idea why its saying those numbers are ints, when they're longs...

    Edit: YAY! :D
     
  20. Offline

    nicholasntp

    So now after this delay, I need another shorter delay to start. Same setup? Haha
     
  21. Offline

    CorrieKay

    dont forget to stop the task. remember that int we saved? hourCountDownTask?

    scheduler.cancelTask(hourCountdownTask) when you hit one hour. Then, in the same function, you can create another. However, i suggest creating a constructor within the countdown timer class that holds an amount of time that you want to make it cancel.

    so the task scheduling would look like this:
    Code:
    scheduler.scheduleAsyncepeatingTask(this, new CountdownTimer(int goes here), 0, 20);
     
  22. Offline

    nicholasntp

    Got it. Thank you so much.
     
    CorrieKay likes this.
  23. Offline

    CorrieKay

    no problem! glad to help out :D
     
Thread Status:
Not open for further replies.

Share This Page