Solved Countdown working

Discussion in 'Spigot Plugin Development' started by Th3Wh1teG0d, Jan 26, 2020.

Thread Status:
Not open for further replies.
  1. Hello, im making a Countdown Command but when i send the Command twice it send every second the message that i send and it wont stop

    Code:

    Code (Java):

    public class CountDownCommand implements CommandExecutor {
       
        private int taskID;
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {
                taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getMain(), new Runnable() {
                    int countdown = Integer.valueOf(args[1]);
                   
                    @Override
                    public void run() {
                        if(countdown <= 0) {
                            String message = args[0];
                            Bukkit.getScheduler().cancelTask(taskID);
                            Bukkit.broadcastMessage(Data.pr + message);
                            return;
                        }
                        Bukkit.broadcastMessage(Data.pr + countdown);
                        countdown--;
                    }
                }, 0, 20);
            }
            return false;
        }

    }

     
     
  2. First check if a countdown is already running using by evaluating
    Code (Text):
    Bukkit.getScheduler().isCurrentlyRunning(taskID)
    and if so, either don't start a new countdown or stop the currently running countdown first:
    Code (Text):
    Bukkit.getScheduler().cancelTask(taskID)
     
  3. It dont change

    Code (Java):

    public class CountDownCommand implements CommandExecutor {
       
        private int taskID;
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {
                if(Bukkit.getScheduler().isCurrentlyRunning(taskID)) {
                    Bukkit.getScheduler().cancelTask(taskID);
                } else {
                    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getMain(), new Runnable() {
                        int countdown = Integer.valueOf(args[1]);
                   
                        @Override
                        public void run() {
                            if(countdown <= 0) {
                                String message = args[0];
                                Bukkit.getScheduler().cancelTask(taskID);
                                Bukkit.broadcastMessage(Data.pr + message);
                                return;
                            }
                            Bukkit.broadcastMessage(Data.pr + countdown);
                            countdown--;
                        }
                    }, 0, 20);
                }
            }
            return false;
        }
    }

     
     
  4. I got it... Just cancel the taskID every time the Player executes the command and them make another Countdown...
     
Thread Status:
Not open for further replies.