Tutorial Create a simple countdown

Discussion in 'Resources' started by poepdrolify, Jul 7, 2015.


Was this tutorial helpful?

  1. Yes

    3 vote(s)
  2. No

    1 vote(s)
  3. A bit

    0 vote(s)
Thread Status:
Not open for further replies.
  1. Offline


    Because many people ask for help on countdowns in minigame plugins, I made a simple countdown tutorial, even for new developers. The only thing you need is basic Java and Bukkit coding experience.

    • Creating your Main class
    • Creating a setTimer() method
    • Creating a startTimer() method
    • Creating a stopTimer() method
    • Starting the timer at specific events
    • Adding some more useful stuff

    Creating your Main class
    Creating your Main class (open)
    Start by create your Main plugin class (I assume you already now how to create a class). If you done that, add these two lines to the class:
    int time;
    int taskID;
    What this does, it creates a new integer called time, and we can use it later in our methods. The integer taskID is for the ID of our scheduler. Now, let's continue with the setTimer() method.

    Creating a setTimer() method
    Creating a setTimer() method (open)
    In this method, we will create a simple method that sets the time to an x amount. Create a new method like this:
       public void setTimer(int amount) {
            time = amount;
    Done, that wasn't so difficult, right?

    Creating a startTimer() method
    Creating a startTimer() method (open)
    In this method, we will create a scheduler, that actually is our timer, and make it run. start by creating a method called startTimer():
    public void startTimer() {
    In this method we want to create a scheduler, that runs every seconds. Add taskID = ... because we need to have the ID of the task. Since Minecraft uses ticks, we write down 20 ticks:
       public void startTimer() {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            taskID = scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
            }, 0L, 20L);
    After this we want to check if time equals 0. If it equals 0, it means that the time is up, so we want to broadcast that the time is up:
                   if(time == 0) {
                        Bukkit.broadcastMessage(ChatColor.RED + "Time is up!");
    Don't forget to add the stopTimer(), but it gives an error since we didn't created it yet. But for later it is easier. If you want to broadcast a message every ... seconds, you could check if time equals x, but in our case, we want to check if the time is divisible by 5, if so, broadcast a message:
                   if(time % 5 == 0) {
                        Bukkit.broadcastMessage(ChatColor.RED + "Timer remaining " + time + " seconds");
    And at the end of the scheduler, add this:
    time = time - 1;
    With this done, we can move to the next part.

    Creating a stopTimer() method

    Creating a stopTimer() method (open)
    In this method, we want to make it so that it stops our timer, or stops our scheduler. Because we have the taskID, it is easier to stop the scheduler:
        public void stopTimer() {
    This cancels the task, so the timer is cancelled as well.

    Starting the timer at specific events
    Starting the timer at specific events (open)
    If you want to start your timer only if there are enough players in the server, you first need to start the timer in your onEnable(), so that it always runs. In your timer scheduler, check if the amount of players in the server for example is not more than 2:
    if (!(Bukkit.getOnlinePlayers().size() > 2)) {
    Make sure to add a return statement, otherwise the code continues. You can also start a timer when a player interacts, for example. In an event, just add setTimer(int x), and than startTimer().

    Adding some more useful stuff
    Adding some more useful stuff (open)

    On many minigame servers, the xp bar level is equal to the timer. To do this, add this in you timer scheduler:
    for (Player p : Bukkit.getOnlinePlayers()) {
    You can also make it so that the amount of xp is equal to the timer:
    for (Player p : Bukkit.getOnlinePlayers()) {
        p.setExp(time / (float) 30);
    This requires an already set time, in my case 30.

    If this tutorial worked for you, please leave a comment down below. If you found any mistakes, please leave them in the comments as well.
  2. Really good and helpful tutorial! :D
  3. Offline


  4. Offline


    Hasn't this thread been made many times?
    LCastr0 likes this.
  5. @poepdrolify Nice... Thanks for subscribing I guess! I will upload more videos asap:)
  6. Offline


    I don't know @ChipDev
  7. Offline


    You start two timers and break the stop method...
    Last edited by a moderator: Jul 9, 2015
  8. Offline


    @xTrollxDudex I don't understand your comment. What's wrong with it? And there aren't two timers. Btw, this is a countdown not cooldown
    Last edited: Jul 9, 2015
  9. Offline


    There need not be, as you can call startTimer() twice. Broken.

    Apologies, my eyes aren't functioning very well in the morning.
  10. @poepdrolify The problem that @xTrollxDudex is pointing out is that this tutorial isn't very OOP at all. Your tutorial can only ever have one timer, and if someone tries to set two timers, the first one will never end, it will simply continue until your plugin shuts down, wasting resources for no benefit. The second timer will cancel itself okay (as long as you don't schedule another one before it's finished), except that it will probably be in half the time you were expecting - remember that first timer? Yeah, it's still happily chugging along, taking away one from the timer every second, messing with all future timers.

    So in order to improve this resource to a usable condition, you'll need to figure a solution to that either by preventing more than one timer at once (bad) or rewriting it to support multiple timers and just generally be more OO (acceptable). I'd also suggest you stop using the old scheduler methods and move to BukkitRunnables, they much better and come with a convenient cancel() method.
  11. Offline


    @AdamQpzm This is a basic tutorial, if someone wants more timer he/she should try that him/herself
  12. Offline


    Even for a basic tutorial you should not teach how to write badly designed code.
  13. @poepdrolify I'm afraid @RainoBoy97 is right here - in its current state, this is bad code. And it being for basic is no excuse for using bad code. Heck, if anything, it being a basic tutorial means you should be more careful with your code - basic tutorials are more likely to be followed by beginners, and it's more important not to give beginners bad code than it is people who have more experience, as they're more likely to be able to fix whatever it is you're doing wrong.
Thread Status:
Not open for further replies.

Share This Page