Countdown Timer

Discussion in 'Plugin Development' started by Deleted user, Jul 11, 2012.

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

    Deleted user

    So I have a countdown timer and it's fully functional it goes down from minutes and then once it's at 1 minute it goes to 50 seconds, 40 seconds, and so on to 10 seconds and then 0. However it doesn't countdown 9, 8 , 7, 6 , etc.. I'm pretty awful with 'timers' anyone willing to help me out? This should be all the code you need to help me, I'll like whomever helps. Thanks in advance!

    Code:java
    1.  
    2.  
    3. public void startCountdown() {
    4. countdown_id = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    5. int time = COUNTDOWN_TIME * 60;
    6. boolean timer_started = false;
    7.  
    8. public void run() {
    9. if (!timer_started) {
    10. setCountdownTime(time / 60, false);
    11. timer_started = true;
    12. } else {
    13. time -= 10;
    14.  
    15. if (time < 0)
    16. Bukkit.getScheduler().cancelTask(countdown_id);
    17. else {
    18. if (time % 60 == 0 && time / 60 >= 1)
    19. setCountdownTime(time / 60, false);
    20. else if (time < 60)
    21. setCountdownTime(time, true);
    22. }
    23. }
    24. }
    25.  
    26. }, 10, 200);
    27. }
    28.  
    29. public void setCountdownTime(int time_left, boolean seconds) {
    30. if (time_left != 0 && !seconds)
    31. HcChat.broadcastMessage(time_left + " minute" + (time_left != 1 ? "s" : "") + " until game starts!");
    32. else if (time_left > 0 && seconds) {
    33. HcChat.broadcastMessage(time_left + " seconds until game starts!");
    34. } else {
    35. startGame();
    36. }
    37.  
    38. TIME_LEFT = time_left;
    39. TIME_SECONDS = seconds;
    40. }
    41.  


    Bump?

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

    ZeusAllMighty11

    Don't bump your own threads more than once per hour. We can all see them, and some of the devs go out of their way as to go to the 2nd page, 3rd page, etc. Until they find an unsolved thread.

    Doesn't even look like you tried to declare what happens when the time is less than or equal to 10 seconds left in the timer.
     
  3. Offline

    Deleted user

    Sorry Zeus, and your right I haven't because I don't know how.

    Bump.

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

    sayaad

    Code:java
    1. int CountDownValue = 61;
    2. public static boolean startCountDown = false;
    3. final Timer timer1 = new Timer();
    4. TimerTask task = new TimerTask() {
    5. public void run() {
    6. if(startCountDown == true){
    7.  
    8. CountDownValue--;
    9.  
    10. if (CountDownValue == 60 || CountDownValue == 50 || CountDownValue == 40 || CountDownValue == 30 || CountDownValue == 20 || CountDownValue < 11) {
    11. Bukkit.getServer().broadcastMessage(CountDownValue + " seconds remaining");
    12. }
    13. else if (CountDownValue == 0) {
    14. this.startCountDown = false;
    15. CountDownValue = 61;
    16. //insert your code here
    17. }
    18. }
    19. }
    20. };


    onEnable, insert :

    Code:java
    1. timer1.schedule(task, 0, 1000);


    to start the countdown, simply set the value of startCountDown to true.
     
  5. Offline

    Deleted user

    I'd really prefer to use schedulers, to be more efficient.

    Bump.

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

    pzxc

    You're doing time -= 10, subtracting 10 each time. So it will never go 9, 8, 7, 6.... because you're doing 10 at a time.
    You need to do one at a time (time -= 1 or better yet time--)
    And the ONLY display it if the time is divisible by 10 *or* less than 10
    if (time % 10 == 0 || time <= 10) { ... }
     
  7. Offline

    Deleted user

    Okay let me try this..

    Can you give me some example code please?

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

    mcgamer99

    Try this:
    You must only change YourMainPluginName to the name of the main of your plugin.

    Code:
        public int timer = 2400;
       
        YourMainPluginName plugin;
     
        public void Timer() {
           
            Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
               
                public void run() {
                   
                    if(timer != 2400) {   
                        Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "Last 20 minutes!");
                        timer--;
                    }
                   
                    if(timer != 1200) {
                        Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "Last 10 minutes!");
                        timer--;
                    }
                   
                    if(timer != 600) {
                        Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "Last 5 minutes!");
                        timer--;
                    }
                   
                    if(timer != -1) {
                        if(timer != 0) {
                            Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The timer is finished, do something!");
                            timer--;
                        }
                    }
                   
                }         
            }, 0L, 2400L);
        }
     
  9. Offline

    Deleted user

    This is actually almost exactly what I have but mine is more efficient
    and configurable your's is crude and hard coded,
     
  10. Offline

    sayaad

    He was just giving an example :3
    You can customize it however you want
     
  11. Offline

    Deleted user

    The thing is mine works perfectly but Idk how to make it so that once it hits 10 seconds it changes -= 10 to -=1
     
  12. Offline

    sayaad

    Code:java
    1. time -= 10;

    to
    Code:java
    1. time--;

    or
    Code:java
    1. time = (time - 1);


    and change the timer to 1 second
     
  13. Offline

    Deleted user

    20 ticks in 1 second right?
     
  14. Offline

    sayaad

    yup
     
  15. Offline

    Deleted user

    Just tried this code and if I set it to 1200 which should be 1 minute it counts down super fast and yeah... its like the countdown is going down like not 1 second by 1 second buy like 60 seconds at a time..


    Code:java
    1.  
    2. public void startCountdown() {
    3.  
    4. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    5.  
    6. public void run() {
    7. if (COUNTDOWN_TIME != 1200) {
    8. HcChat.broadcastMessage("1 minute until game starts!");
    9. COUNTDOWN_TIME--;
    10. }
    11. if(COUNTDOWN_TIME != 600) {
    12. HcChat.broadcastMessage("30 seconds until game starts!");
    13. COUNTDOWN_TIME--;
    14. }
    15. if(COUNTDOWN_TIME != 200) {
    16. HcChat.broadcastMessage("10 seconds until game starts!");
    17. COUNTDOWN_TIME--;
    18. }
    19. if(COUNTDOWN_TIME != 180) {
    20. HcChat.broadcastMessage("9 seconds until game starts!");
    21. COUNTDOWN_TIME--;
    22. }
    23. if(COUNTDOWN_TIME != 160) {
    24. HcChat.broadcastMessage("8 seconds until game starts!");
    25. COUNTDOWN_TIME--;
    26. }
    27. if(COUNTDOWN_TIME != 140) {
    28. HcChat.broadcastMessage("7 seconds until game starts!");
    29. COUNTDOWN_TIME--;
    30. }
    31. if(COUNTDOWN_TIME != 120) {
    32. HcChat.broadcastMessage("6 seconds until game starts!");
    33. COUNTDOWN_TIME--;
    34. }
    35. if(COUNTDOWN_TIME != 100) {
    36. HcChat.broadcastMessage("5 seconds until game starts!");
    37. COUNTDOWN_TIME--;
    38. }
    39. if(COUNTDOWN_TIME != 80) {
    40. HcChat.broadcastMessage("4 seconds until game starts!");
    41. COUNTDOWN_TIME--;
    42. }
    43. if(COUNTDOWN_TIME != 60) {
    44. HcChat.broadcastMessage("3 seconds until game starts!");
    45. COUNTDOWN_TIME--;
    46. }
    47. if(COUNTDOWN_TIME != 40) {
    48. HcChat.broadcastMessage("2 seconds until game starts!");
    49. COUNTDOWN_TIME--;
    50. }
    51. if(COUNTDOWN_TIME != 20) {
    52. HcChat.broadcastMessage("1 second until game starts!");
    53. COUNTDOWN_TIME--;
    54. }
    55. if (COUNTDOWN_TIME != -1) {
    56. if (COUNTDOWN_TIME != 0) {
    57. startGame();
    58. COUNTDOWN_TIME--;
    59. }
    60. }
    61.  
    62. }
    63. }, 0L, 200L);
    64. }
    65.  
     
  16. Offline

    sayaad

    try using my method of counting then...

    NOTE : Change the value of COUNTDOWN_TIME from ticks to seconds and add one second to it

    Code:java
    1. public void startCountdown() {
    2.  
    3. int count = COUNTDOWN_TIME;
    4.  
    5. countdown_id = Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()) {
    6.  
    7. public void run() {
    8.  
    9. //note : I changed the timer to 20L so the value of count is in seconds
    10.  
    11. count--;
    12.  
    13. if(count == 60){
    14. HcChat.broadcastMessage("1 minute until game starts!");
    15. }
    16.  
    17. if(count == 30 || count == 20 || count < 11){
    18. HcChat.broadcastMessage(count + " seconds until game starts!");
    19. }
    20.  
    21. if(count == 0){
    22. startGame();
    23. Bukkit.getScheduler().cancelTask(countdown_id);
    24. }
    25.  
    26. }
    27. }, 0L, 20L);
    28. }
     
  17. Offline

    Deleted user

    What exactly is 20L or what does it mean?
     
  18. Offline

    sayaad

    It means the value "20" in a long.
    For example, you can say :

    Long long = 10;

    It will be the same as

    Long long = 10L;
    '
    Before in early java, you was not able to assign an Integer value to a long so they added the "L" to specify the difference.
     
  19. Offline

    Deleted user

    Idk why for the life of my it will not let me edit the number like if I do int count = COUNTDOWN_TIME * 60; so that it's in minutes like 1 = 60 which is one minute. It won't let me it just nulls the fuck out and basically ignores it.
     
  20. Offline

    sayaad

    Da faq?

    Edit : Ik this is the same thing but it will give you an idea whether the value COUNTDOWN_TIME is null

    Code:java
    1. int count = COUNTDOWN_TIME;
    2. count = (count * 60);
     
  21. Offline

    Deleted user

    Hey I got another question, when a player dies they are kicked right. How can I make it so it doesn't show "name has left the game" when kicking a player?
     
  22. Offline

    sayaad

    Code:java
    1. @EventHandler
    2. public void onKick(PlayerKickEvent evnet){
    3. e.setLeaveMessage(null);
    4. }
     
  23. Offline

    Kazzababe

    Code:java
    1. public void countDown() {
    2. Countdown = this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. if( (Time % 10) == 0 && Time != 0) {
    5. Bukkit.broadcastMessage(ChatColor.GRAY + "The game will begin in " + Time + " seconds!");
    6. } else if( Time == 0 ) {
    7. inProgress = true;
    8. Bukkit.broadcastMessage(ChatColor.RED + "The games have begun!");
    9. Bukkit.getScheduler().cancelTask(Countdown);
    10. }
    11. Time--;
    12. }
    13. }, 0L, 20L);
    14. }
     
  24. Offline

    webicex

    Kazzababe

    Hey Kazzababe, you have not stated the variable declarations in your code? I do not understand what each variable equals. hehe :p
     
  25. Offline

    joko301

    Eballer48

    I know this post is long gone, but you would have to do a cancel(); method, then restate your method as: countdown =- 1;
     
  26. Offline

    Voltex

    Seriously dude...
     
Thread Status:
Not open for further replies.

Share This Page