Cancel a BukkitRunable

Discussion in 'Plugin Development' started by bosszepro, Jul 14, 2022.

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

    bosszepro

    Hii,
    I got an issue on my plugin. I don't know how to stop an event when a player leave an arraylist.
    Can anyone help me?
    Code:
    public class Start extends BukkitRunnable implements Listener{
      
        private int timerstart = 10;
        private Main main;
      
        public Start(Main main) {
            this.main = main;
        }
      
        @Override
        public void run() {
          
            for(Player pls : main.getPlayers()) {
                pls.setLevel(timerstart);
            }
            if(main.getPlayers().size() <= 1) {
            Bukkit.getScheduler().cancelTasks(main);
            }
          
            if(timerstart == 10 || timerstart == 5 || timerstart == 4 || timerstart == 3 || timerstart == 2 ) {
                Bukkit.broadcastMessage("Le jeu démarre dans " + timerstart + " secondes");
                Bukkit.getOnlinePlayers().forEach(t -> t.sendTitle("Le jeu commence dans", timerstart + " secondes"));
              
            }
            if(timerstart == 1) {
                Bukkit.broadcastMessage("Le jeu démarre dans " + timerstart + "seconde");
                Bukkit.getOnlinePlayers().forEach(t -> t.sendTitle("Le jeu commence dans", timerstart + " seconde"));
            }
          
          
            if(timerstart == 0) {
                if(main.getPlayers().size() == 2) {
                Bukkit.broadcastMessage("Lancement du jeu");
                main.setState(Status.SEARCHING);
                cancel();
                  
                }
    
            }
          
            timerstart --;
        }
      
    
    }
    
    
    Can anyone help me ?
     
    Last edited by a moderator: Jul 15, 2022
  2. Offline

    Tim_M

    Try checking at intervals if the player is still there, if not cancel the bukkitrunnable. To cancel it:
    you save the return value of runTaskTimer (which is a BukkitTask) in a variable and call the cancel() method.

    Also looking at your code, shouldn't you jsut have a PlayerQuitEvent somewhere that will automatically remove the player from arraylist?

    Or am I not understading your problem? Please elaborate.
     
  3. Offline

    BlueBottle9

    Maybe you can use "your-arraylist.contains(player)" to judge whether a player is still in the list. Then use"this.cancel()"
    to cancel the task which the code in. Or use task.cancel() to cancel other bukkit tasks. (task = other bukkit task /runable)
     
Thread Status:
Not open for further replies.

Share This Page