Search code examples
javarunnablebukkit

Bukkit runnable tips


Hello any ideas how to make scheduleSyncDelayedTask so it doesnt cancel previous task, which should be running and canceled a bit later?

for(int x = 0; x < 8; x++){
    int taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {        
        @Override
        public void run() {                 
            CreateItems.createItemsOnStand2(player, bedna, listitems);      
        }
    }, 30*x , 2+x);

    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
        public void run() {
            Bukkit.getScheduler().cancelTask(taskID);
        }
    }, (x==0) ? 30 : 30*x);     
}

Solution

  • You can use BukkitRunnables for task scheduling

    new BukkitRunnable() {
        @Override
        public void run() {
            //Code you need running
            this.cancel();    //Cancelling
        }
    }.runTaskTimer(pluginInstance, delayTime, repeatingTime);
    
    
    
     new BukkitRunnable() {
        @Override
        public void run() {
            //Code you need running
            this.cancel();    //Cancelling
        }
    }.runTaskLater(pluginInstance, delayTime);
    

    It makes it so the task can be easily created and self-cancellable