Solved Bukkit Runnable Problem

Discussion in 'Plugin Development' started by Reptar_, Apr 24, 2014.

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

    Reptar_

    So I'm making a gamemode which has a countdown, but I'm not sure how to reset the countdown after each time it finishes. So if I do /start, the countdown begins. When I do /start when that countdown ends, the countdown goes down twice as fast. How do I get it to restart at the same speed instead of it doubling up each time I do /start?

    For example:

    1st run through takes 1 minute.
    2nd run through takes 30 seconds.

    Here's my code:
    http://pastebin.com/rjeRUwXM
     
  2. Offline

    DoctorDark

    Reptar_

    Make a false running boolean, when you use the command /start, set the boolean to true.

    You then check if the boolean is running before allowing you to run the command. If it is, cancel the scheduler and start the task again, if it isn't start the task normally.
     
  3. Offline

    Reptar_

    How do you cancel it again?
     
  4. Offline

    Wizehh

    Inside the runnable, use this.cancel(); (or just cancel() ).
     
  5. Offline

    Reptar_

    I tried that but it keeps throwing errors. Maybe I'm putting it in the wrong place in the runnable?
     
  6. Offline

    Wizehh

    Can you post the code in question please?
     
  7. Offline

    Reptar_

    Says "The method cancel() is undefined for the type Runnable(){"

    Code:java
    1.  
    2. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    3. public void run(){
    4. if (countdown != 0){
    5. countdown--;
    6. if(countdown <= 5 && countdown !=0){
    7. Bukkit.broadcastMessage(ChatColor.GRAY + "[" +
    8. ChatColor.RED + "Blood" +
    9. ChatColor.DARK_RED + "Thirster" +
    10. ChatColor.GRAY + "]" +
    11. ChatColor.GOLD + " The game is starting in " + countdown + " seconds!");
    12. this.cancel();
    13. }
    14. }
    15. }
    16. }, 0L, 20L);
    17. }else{
    18. p.sendMessage(ChatColor.GRAY + "[" +
    19. ChatColor.RED + "Blood" +
    20. ChatColor.DARK_RED + "Thirster" +
    21. ChatColor.GRAY + "]" +
    22. ChatColor.GOLD + " You don't have permission to do that!");
    23. }
    24. }
    25. returnfalse;
    26. }
    27.  
    28. }
    29.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    Wizehh

    Hmm. Okay, use this instead:
    PHP:
    int taskId getServer().scheduleSync...
     
        
    //inside run()
        
    Bukkit.getScheduler().cancelTask(taskId);
     
  9. Offline

    Reptar_

    No errors so far. About to test and I'll let you know how it goes.

    Ok, I made some errors while fixing it up, which I fixed. But everything works great now. Thanks! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page