countdown before game ?

Discussion in 'Plugin Development' started by oxilo, Jun 18, 2021.

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

    oxilo

    Hello i'm new at creating a plugin but i want to do a game with a countdown before launch it and when the countdown is finish that start a game is that possible ?
    Like this :

    //code
    start countdown
    while(countdown.isFinish==flase){
    continue countdown
    }
    delete countdown
    start the game
    //code
     
  2. Offline

    Newdel

    Definitly not like this. The server will probably crash when the countdown is too long because it blocks the main thread
    Use a delayed task instead like this
    Set countdown
    new BukkitRunnable () -> { (not valid code)
    start game
    }.runTaskLater(countdownTime);

    More information and better examples:
    https://bukkit.fandom.com/wiki/Scheduler_Programming
     
  3. Offline

    c7dev

    It would be much easier to make a synchronous task timer, which runs the code every second as it decreases the countdown variable. There are more things you could add, such as utilizing "this.cancel();" to stop the timer if a player leaves, for example.
    Code:
    new BukkitRunnable() {
                int i = 10;
                public void run() {
                    if (i > 0) Bukkit.broadcastMessage("§bGame starts in " + i + " seconds!");
                    else startGame() //your method to start the game here
                    i = i - 1;
                }
            }.runTaskTimer(plugin, 0, 20l)
    
     
Thread Status:
Not open for further replies.

Share This Page