How to do a Countdown for your game

Discussion in 'Resources' started by WauloK, Sep 13, 2013.

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

    WauloK

    I have seen many people asking for help on code for counting down 5 minutes before a game starts, then the last 5 seconds.

    There's been some wacky code demo'd for sure, but hopefully this will help you all out.
    You will want to tidy up the text being broadcast:

    4 minute(s) until the game starts.


    But I'll leave that as a task for the learner.
    Add on your pretty chat colours, etc and have fun.

    Let me know if you have any problems, but this should help solve some issues.
    I've uploaded it to my BitBucket repository so you can copy all you want from there and do with it as you wish:

    Countdowntest BitBucket repository

    [​IMG]

    I'm sure there are other ways to do it, but this works :)
     
  2. Offline

    Lactem

    This belongs in the resources section. Nice demo for the newer coders.
     
  3. Offline

    Jade

    Moved to correct section.
     
  4. Offline

    sgavster

    Thanks!
    I did this:
    Code:java
    1. public static int MinutesToCountDown = 10;
    2. public static int SecondsToCountDown = 5;
    3. public static Plugin plugin;
    4. int taskID1;
    5. int taskID2;
    6.  
    7.  
    8. public void start()
    9. {
    10. taskID1 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(SimplyPaintball.getInstance(), new Runnable()
    11. {
    12. public void run() {
    13. MinutesToCountDown--;
    14. if (MinutesToCountDown!=0) {
    15. Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!");
    16. }
    17. if (MinutesToCountDown==0) {
    18. plugin.getServer().getScheduler().cancelTask(taskID1);
    19. MinutesToCountDown=5;
    20. startSecondsCountdown();
    21. }
    22.  
    23. }
    24. }, 20*60L, 20*60L);
    25. }
    26.  
    27. public Runnable startSecondsCountdown() {
    28. taskID2 = SimplyPaintball.getInstance().getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    29. public void run() {
    30. SecondsToCountDown--;
    31. if (SecondsToCountDown!=0) {
    32. Bukkit.broadcastMessage(SecondsToCountDown + " second(s) left until game ends!");
    33. }
    34. if (SecondsToCountDown==0) {
    35. plugin.getServer().getScheduler().cancelTask(taskID2);
    36. SecondsToCountDown=5;
    37. Bukkit.broadcastMessage("GAME STARTED!");
    38. }
    39.  
    40. }
    41. }, 20L, 20L);
    42. return null;
    43. }
    44. }

    And used:
    Code:java
    1. GameCounter gc = new GameCounter();
    2. gc.start();

    To start it, it didn't say "10 minute(s) let until the game ends!" though. It said 9, 8 ect..
    Also is there a way to make it so it says it at certain times? Like 10 minutes, 5 minutes, 3, 2, 1, ect? :D
     
  5. Offline

    WauloK

    Yeah Sorry. I updated it since. Start with x+1 minutes.
    You could probably replace:
    Code:java
    1. if (MinutesToCountDown!=0) {
    2. Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!");
    3. }

    with:
    Code:java
    1. switch (MinutesToCountDown) {
    2. case 10:
    3. case 5:
    4. case 4:
    5. case 3:
    6. case 2:
    7. case 1:
    8. Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!");
    9. break;
    10. default:
    11. break;
    12. }
     
  6. Offline

    Chinwe

    Instead of minute(s) and second(s), you should check if the number of minutes/seconds != 1 before adding an 's' to the end, to get 3 seconds, 2 seconds, 1 second ;)
     
  7. Offline

    WauloK

  8. Offline

    JPG2000

    WauloK Why would you have 2 seperate runnables? Heres what I would do:
    Code:java
    1. private static int left = 60*5;
    2.  
    3. public void startTimer() {
    4. Bukkit.broadcastMessage("Starting the Countdown for 5 minutes!");
    5. new Runnable() {
    6. public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), this, 0L, 20L);
    7. @Override
    8. public void run() {
    9. if (left > 0) {
    10. left--;
    11. switch (left) {
    12. case 60*4:
    13. Bukkit.broadcastMessage("There are 4 minutes remaining!");
    14. break;
    15. case 60*3:
    16. Bukkit.broadcastMessage("There are 3 minutes remaining!");
    17. break;
    18. case 60*2:
    19. Bukkit.broadcastMessage("There are 2 minutes remaining!");
    20. break;
    21. case 60:
    22. Bukkit.broadcastMessage("There is 1 minute remaining!");
    23. break;
    24. case 30:
    25. Bukkit.broadcastMessage("There are 30 seconds remaining!");
    26. break;
    27. case 10:
    28. Bukkit.broadcastMessage("There are 10 seconds remaining!");
    29. break;
    30. case 3:
    31. Bukkit.broadcastMessage("There are 3 seconds remaining!");
    32. break;
    33. case 2:
    34. Bukkit.broadcastMessage("There are 2 seconds remaining!");
    35. break;
    36. case 1:
    37. Bukkit.broadcastMessage("There are 1 seconds remaning!");
    38. break;
    39. }
    40.  
    41. } else {
    42. Bukkit.getScheduler().cancelTask(taskID);
    43. Bukkit.broadcastMessage("GAME STARTING!");
    44. }
    45. }
    46. };
    47. }
     
    WauloK likes this.
  9. Offline

    WauloK

  10. Offline

    JPG2000

    WauloK Lol. But why would you do more code to do the same thing? #OutCoded
     
    Ultimate_n00b likes this.
  11. Offline

    WauloK

    I have no idea if there's much impact but rather than having a thread going every 1 second for 5 minutes, mine goes once every minute for 5 minutes then 5 times after that for 1 second. Probably not much impact tbh :)
     
  12. Offline

    CraftBang



    Code:
     
    public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), this, 0L, 20L);
    
    I get an error on Main , I tried to put in there my main class (MCInfinite) but it still gave an error.
    Can you help please ?
     
  13. Offline

    WauloK

    If the class is called MCInfinite, try changing Main.getPlugin() to MCInfinite.getPlugin() ?
     
    JPG2000 likes this.
Thread Status:
Not open for further replies.

Share This Page