How to make a countdown clock?

Discussion in 'Plugin Development' started by VincentSilas, Feb 5, 2020.

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

    VincentSilas

    I've been designing a minigame map, and I need to make it so 40 minutes after I press a button which dictates the start of the minigame, a specific command runs. I also need the countdown from 40 minutes to 0 being visible. The seconds do not necessarily need to be visible, but it would also be great if they could.

    I don't know much about commands, but I've watched lots of tutorials on how to do this, so I have a little bit of an idea of how this all would work. https://9apps.ooo/

    However, those tutorials were for versions before 1.13, and it seems like commands have chanhged completely on 1.13 from previo https://solitaire.onl/us versions, and I couldn't get myself to just understand how some commands are "translated" from those previous versions to current 1.13 and 1.14

    Alternatively, It would also work for me to build a physical, actual countdown clock that has customizable time and an analog display. However, the only one that would work that I found does NOT have any type of download, and I just can't build a giant redstone machine myself (as I am not very good with that either). I would really appreciate if there was a world I could download containing an already working countdown clock for 1.14

    However, commands would still do the trick if that option isn't viable.
     
    Last edited: Feb 9, 2020
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    KarimAKL

    @VincentSilas You could make a BukkitRunnable that runs every second, then subtract the time of start by the amount of seconds that has passed, then check if it's 0, if that's the case then do whatever you want.

    To display the countdown, you could get the time of start, subtract it by the amount of seconds passed, and then convert it to the format you want.
     
  4. Offline

    bowlerguy66

    @VincentSilas Are you sure you're in the right forum? Your post doesn't have any reference to code or plugin development.
     
  5. Offline

    Casinator

    @VincentSilas
    Code:
    Bukkit.getScheduler().scheduleAsyncRepeatingTask(<yourplugin>, new Runnable(){
    
    @Override
    public void run(){
       //your repeatet task
    }, <your delay for start>, <your delay of repeating>
    if you want to cancel this scheduler at a specific moment you have to do this:
    Code:
    public class yourClass{
      private int TaskId;
     
      //your stuff...
      TaskId = Bukkit.getScheduler().rep....
      
       ...public void run(){
           //your stuff to repeat
           if(your cancel boolean){
               Bukkit.getScheduler().cancelTask(TaskId);
            }
           }
    
    
    
    }
    You save the ID of the scheduler in an int, you are only able to cancel the task, when you know the TaskId
    Hope i was able to help:)
     
  6. Offline

    Sw_aG

    Make an Integer for the clock if you don't have one, then make a bukkit runnable that will run every 20 ticks
    20 ticks = 1 second; (Make with that what you will)

    Example:
    PHP:
       new BukkitRunnable() {               
                           
       @
    Override
       
    public void run() {
           
    //Your code (this will run every second)
        
    }
    }.
    runTaskTimer(plugin2020);
    Lets say you have an integer, and you want to make a countdown.
    It would look like this:

    PHP:
     int count 10;  // How long the countdown will last in seconds
       
    new BukkitRunnable() {               
                          
       @
    Override
       
    public void run() {

         if(
    count 0){
         
    p.sendMessage("Timer: " count);
         
    count--; // Subtracting the integer by 1
        
    } else {
        
    this.cancel;
        }
        }
    }.
    runTaskTimer(plugin2020);
    Notice this line:
    PHP:
    }.runTaskTimer(plugin2020);
    There's also a runTaskLater which will run the code within after the amount of ticks in the brackets
    Just multiply the amount of seconds you want by 20 "*".


    And for the command to happen, use getServer().dispatchCommand
    You can use the console command sender if you'd like
     
Thread Status:
Not open for further replies.

Share This Page