Check if task is running

Discussion in 'Plugin Development' started by ThrustLP, Apr 2, 2016.

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

    ThrustLP

    Hello guys! I have a fairly simple part of code here with a command and an scheduler. So whenever I type the command the task counts down from X to 0 and then does something. My problem is that if I type the command twice there are two tasks running. So I want to send the player a message and cancel starting a new task when there is already one running. Here is my code:

    Code:
    
        int cd; //Is defined where it belongs to 
    
    


    Code:
    if(args[0].equalsIgnoreCase("start")){
                 
                 
                 
                 
                 
                    p.sendMessage("Started");
                 
                 
                 
                    cd = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                     
                     
                     
                     
                        Integer amount = Integer.valueOf(15);
    
                        @Override
                        public void run() {
    
                            if(amount != 0){
                                Bukkit.broadcastMessage(amount + " seconds left!");
                                amount --;
                             
                            }else if(amount == 0){
                     
                               p.sendMessage("Finished");
                              Bukkit.getScheduler().cancelTask(cd);
                            }
                         
                         
                        }
                    }, 20, 20);
    
                }
    So everything works fine, the only thing that I want to add is a check if the task is already running.

    How can I do that?

    Thank you so much and have a nice day!

    Tobi
     
  2. Offline

    mine-care

    Is it just me, or this is decompiled code?
    I don't see any reason why you use the object Integer rather than the primitive int.
     
Thread Status:
Not open for further replies.

Share This Page