Cancel BukkitRunnable

Discussion in 'Spigot Plugin Development' started by Ardno, Dec 2, 2019 at 6:13 PM.

  1. Hello,

    I'm making a minigame with timer. If in world "world" are 5+ players, counter has started, but if there are 4- players, I want to stop timer. I'm using new BukkitRunnable().
    How i do it?

    Here is my code:
    Code (Java):
    private final Main plugin;
        private int timer;
       
        private enum TimerState{ACTIVE, NONACTIVE};
        private TimerState timerState;
       
        public Timer() {
            this.plugin = (Main) JavaPlugin.getPlugin(Main.class);
        }
       
        public void setTimer(int timer) {
            this.timer = timer;
        }
       
        public void startTimer() {
            if(timerState == TimerState.NONACTIVE) {
                timerState = TimerState.ACTIVE;
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        if(plugin.getServer().getWorld("test").getPlayers().size() < 5) {
                            stopTimer(); //IDK how to complete this
                        }
                        timer--;
                    }
                }.runTaskTimer(plugin, 0L, 20L);
            }
        }
       
        public void stopTimer() {
            if(timerState == TimerState.ACTIVE) {
                timer = 60;
            }
        }
       
        public void getTimer() {
           
        }
       
        public boolean isTimerActive() {
           
           
            return false;
        }
     
  2. If my memory is good, either return or break the run void will cancel it :)
     
  3. You can call cancel() to cancel the BukkitRunnable
     
    • Winner Winner x 1
  4. Yeah :D
    thx
     
    • Friendly Friendly x 1
  5. This will not cancel a BukkitTask. This will only exit out of a code block.
     
    • Agree Agree x 2