runTaskTimer scheduling questions

Discussion in 'Plugin Development' started by Bobcat00, Apr 10, 2014.

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


    I have a periodic task which is scheduled within a listener when an event occurs. The task executes every second until it determines that it's done.

    Here's what I have so far. The listener:
    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerJoin(ArenaPlayerJoinEvent event)
        // Stuff goes here
        new ReadyTask(plugin, arena).runTaskTimer(plugin,20,20);
    The task class:
    public class ReadyTask extends BukkitRunnable
        private final JavaPlugin plugin;
        Arena arena;
        public ReadyTask(JavaPlugin plugin, Arena arena)
            this.plugin = plugin;
            this.arena  = arena;
        public void run()
            // Lots of stuff in here
            if (someCondition == 0)
    There are some things I need some help with:

    1. I need to avoid scheduling the task if it's already running and hasn't cancelled itself yet.

    2. I do need to schedule the task if it has cancelled itself and the listener event occurs again.

    3. And the really tricky part - I can have multiple instances of the task, one for each 'arena', and these tasks can run concurrently. So I guess that means I want to make a 'new' task for each arena.

    So I think I need to make a hashmap, but I'm not sure what to put in it. The key can be the 'arena'. But what would I store as the value to allow me to keep the tasks separate and to determine if an arena's task is already running?

    I tried storing new ReadyTask objects, but even after the task was cancelled, Bukkit wouldn't let me reschedule it. (I was hoping that I could just blindly schedule the task, and if it wasn't running it would be scheduled; if it was running the call to schedule it would be ignored.)

    Suggestions would be appreciated.
  2. Offline



    Simply use the bukkit scheduler:
    2. int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    4. @Override
    5. public void run() {
    6. // Do stuff here
    7. }
    8. }, 20, 20);
    10. Bukkit.getScheduler().isCurrentlyRunning(task); // Check if running
    11. Bukkit.getScheduler().cancelTask(task); // Cancel running scheduler

    MfG Xenira
  3. Offline


    I'm not sure if I understand, but this is what I'm getting:
    -You only want one task per arena.
    -If the task is already running, you don't want to reschedule it.

    You could simply store the tasks inside the arena, could you not? Then use the scheduler.isCurrentlyRunning method on the task.getTaskId. You could even create a method in your task class to do that if you wanted, just for cleanliness.
  4. Offline


    Thanks! This works much better. I don't know why the Bukkit Scheduler Programming Tutorial says to use BukkitRunnable instead of BukkitScheduler.
  5. Offline


    Scheduler tut is for bukkit implementation of scheduler not the base scheduler used for typical task design
Thread Status:
Not open for further replies.

Share This Page