Countdown!

Discussion in 'Plugin Development' started by MacBananaz, Apr 3, 2020.

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

    MacBananaz

    Hello, fellow coders.

    I'm very new to this whole Minecraft plugin dev, even to java itself, but I hope I can get some help.

    So here's what I'm trying to do in the onCommand() method in my Main class:

    if (label.equalsIgnoreCase("start")) {
    // INSERT TIMER HERE
    myMethod();
    }

    My plan was: A player types "/start" and the plugin waits 5 seconds before executing method "myMethod()".

    The problem is: I want it to send a message in the chat for every second passed.
    So the output would be something like:

    Starting in 5...
    Starting in 4...
    Starting in 3...
    Starting in 2...
    Starting in 1...
    *execute myMethod here*

    Thanks in advance!
     
  2. Offline

    bowlerguy66

    @MacBananaz Use a for loop with a delayed task. You can set up a delayed task like this:
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            //code here
                        }
                    }, <time in seconds> * 20L);
    Then, with the for loop, you can "count down" like this:
    Code:
                for(int i = 5; i >= 0; i--) { // 5 is the amount of time in seconds
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            if(i == 0) { // When the time in seconds is 0 you want your stuff to happen
                                // Your specialized code here
                                return;
                            }
                            Bukkit.broadcastMessage("Starting in " + i);
                        }
                    }, i * 20L); // Notice the time in seconds is replaced by i to add the countdown effect
                }
    
    Good luck!
     
    MacBananaz likes this.
Thread Status:
Not open for further replies.

Share This Page