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 I'm sure there are other ways to do it, but this works
Thanks! I did this: Code:java public static int MinutesToCountDown = 10; public static int SecondsToCountDown = 5; public static Plugin plugin; int taskID1; int taskID2; public void start() { taskID1 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(SimplyPaintball.getInstance(), new Runnable() { public void run() { MinutesToCountDown--; if (MinutesToCountDown!=0) { Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!"); } if (MinutesToCountDown==0) { plugin.getServer().getScheduler().cancelTask(taskID1); MinutesToCountDown=5; startSecondsCountdown(); } } }, 20*60L, 20*60L); } public Runnable startSecondsCountdown() { taskID2 = SimplyPaintball.getInstance().getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { public void run() { SecondsToCountDown--; if (SecondsToCountDown!=0) { Bukkit.broadcastMessage(SecondsToCountDown + " second(s) left until game ends!"); } if (SecondsToCountDown==0) { plugin.getServer().getScheduler().cancelTask(taskID2); SecondsToCountDown=5; Bukkit.broadcastMessage("GAME STARTED!"); } } }, 20L, 20L); return null; }} And used: Code:java GameCounter gc = new GameCounter(); 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?
Yeah Sorry. I updated it since. Start with x+1 minutes. You could probably replace: Code:java if (MinutesToCountDown!=0) { Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!"); } with: Code:java switch (MinutesToCountDown) {case 10:case 5:case 4:case 3:case 2:case 1:Bukkit.broadcastMessage(MinutesToCountDown + " minute(s) left until game ends!");break;default:break;}
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
WauloK Why would you have 2 seperate runnables? Heres what I would do: Code:java private static int left = 60*5; public void startTimer() { Bukkit.broadcastMessage("Starting the Countdown for 5 minutes!"); new Runnable() { public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), this, 0L, 20L); @Override public void run() { if (left > 0) { left--; switch (left) { case 60*4: Bukkit.broadcastMessage("There are 4 minutes remaining!"); break; case 60*3: Bukkit.broadcastMessage("There are 3 minutes remaining!"); break; case 60*2: Bukkit.broadcastMessage("There are 2 minutes remaining!"); break; case 60: Bukkit.broadcastMessage("There is 1 minute remaining!"); break; case 30: Bukkit.broadcastMessage("There are 30 seconds remaining!"); break; case 10: Bukkit.broadcastMessage("There are 10 seconds remaining!"); break; case 3: Bukkit.broadcastMessage("There are 3 seconds remaining!"); break; case 2: Bukkit.broadcastMessage("There are 2 seconds remaining!"); break; case 1: Bukkit.broadcastMessage("There are 1 seconds remaning!"); break; } } else { Bukkit.getScheduler().cancelTask(taskID); Bukkit.broadcastMessage("GAME STARTING!"); } } }; }
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
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 ?