Class TaskQueue

java.lang.Object
io.github.bakedlibs.dough.scheduling.TaskQueue

public class TaskQueue extends Object
This class provides useful utilities to schedule Tasks (sync and async). Tasks are added into a Queue and then run sequentially via execute(Plugin) You can provide a delay between the individual tasks via the ticks argument in thenRun(int, Runnable) If you need to access the index of your current task (whether it is the first, last or xth task) you can use the methods with IntConsumer as an argument, otherwise just use the ones with Runnable
Author:
TheBusyBiscuit
  • Constructor Details

    • TaskQueue

      public TaskQueue()
  • Method Details

    • execute

      public void execute(@Nonnull Plugin plugin)
      Use this method to execute the final Task Queue. You should add the tasks before-hand. An IllegalStateException will be thrown if the queue is empty.
      Parameters:
      plugin - The plugin that is performing this execution
    • thenRun

      @Nonnull public TaskQueue thenRun(@Nonnull IntConsumer consumer)
      This method will schedule the given Task with no delay and synchronously. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRun

      @Nonnull public TaskQueue thenRun(@Nonnull Runnable runnable)
      This method will schedule the given Task with no delay and synchronously.
      Parameters:
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRunAsynchronously

      @Nonnull public TaskQueue thenRunAsynchronously(@Nonnull IntConsumer consumer)
      This method will schedule the given Task with no delay and asynchronously. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRunAsynchronously

      @Nonnull public TaskQueue thenRunAsynchronously(@Nonnull Runnable runnable)
      This method will schedule the given Task with no delay and asynchronously.
      Parameters:
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRun

      @Nonnull public TaskQueue thenRun(int ticks, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with the given delay and synchronously. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      ticks - The time to wait before running this task after the previous one.
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRun

      @Nonnull public TaskQueue thenRun(int ticks, @Nonnull Runnable runnable)
      This method will schedule the given Task with the given delay and synchronously.
      Parameters:
      ticks - The time to wait before running this task after the previous one.
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRunAsynchronously

      @Nonnull public TaskQueue thenRunAsynchronously(int ticks, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with the given delay and asynchronously. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      ticks - The time to wait before running this task after the previous one.
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRunAsynchronously

      @Nonnull public TaskQueue thenRunAsynchronously(int ticks, @Nonnull Runnable runnable)
      This method will schedule the given Task with the given delay and synchronously.
      Parameters:
      ticks - The time to wait before running this task after the previous one.
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeat

      @Nonnull public TaskQueue thenRepeat(int iterations, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with no delay and synchronously. The task will be repeated for the given amount of iterations. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      iterations - The amount of times to repeat this task
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeat

      @Nonnull public TaskQueue thenRepeat(int iterations, @Nonnull Runnable runnable)
      This method will schedule the given Task with no delay and synchronously. The task will be repeated for the given amount of iterations.
      Parameters:
      iterations - The amount of times to repeat this task
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatAsynchronously

      @Nonnull public TaskQueue thenRepeatAsynchronously(int iterations, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with no delay and asynchronously. The task will be repeated for the given amount of iterations. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      iterations - The amount of times to repeat this task
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatAsynchronously

      @Nonnull public TaskQueue thenRepeatAsynchronously(int iterations, @Nonnull Runnable runnable)
      This method will schedule the given Task with no delay and asynchronously. The task will be repeated for the given amount of iterations.
      Parameters:
      iterations - The amount of times to repeat this task
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatEvery

      @Nonnull public TaskQueue thenRepeatEvery(int ticks, int iterations, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with the given delay and synchronously. The task will be repeated for the given amount of iterations. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      ticks - The delay between executions (including the start delay)
      iterations - The amount of times to repeat this task
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatEvery

      @Nonnull public TaskQueue thenRepeatEvery(int ticks, int iterations, @Nonnull Runnable runnable)
      This method will schedule the given Task with the given delay and synchronously. The task will be repeated for the given amount of iterations.
      Parameters:
      ticks - The delay between executions (including the start delay)
      iterations - The amount of times to repeat this task
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatEveryAsynchronously

      @Nonnull public TaskQueue thenRepeatEveryAsynchronously(int ticks, int iterations, @Nonnull IntConsumer consumer)
      This method will schedule the given Task with the given delay and asynchronously. The task will be repeated for the given amount of iterations. Use the Integer parameter in your IntConsumer to determine the task's index.
      Parameters:
      ticks - The delay between executions (including the start delay)
      iterations - The amount of times to repeat this task
      consumer - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenRepeatEveryAsynchronously

      @Nonnull public TaskQueue thenRepeatEveryAsynchronously(int ticks, int iterations, @Nonnull Runnable runnable)
      This method will schedule the given Task with the given delay and asynchronously. The task will be repeated for the given amount of iterations.
      Parameters:
      ticks - The delay between executions (including the start delay)
      iterations - The amount of times to repeat this task
      runnable - The callback to run
      Returns:
      The current instance of TaskQueue
    • thenLoop

      public void thenLoop(@Nonnull IntConsumer consumer)
      This method will make the task run the given callback until eternity. The task will be run with no delay and synchronously. Do not add other tasks after calling this method.
      Parameters:
      consumer - The callback to run
    • thenLoop

      public void thenLoop(@Nonnull Runnable runnable)
      This method will make the task run the given callback until eternity. The task will be run with no delay and synchronously. Do not add other tasks after calling this method.
      Parameters:
      runnable - The callback to run
    • thenLoopAsynchronously

      public void thenLoopAsynchronously(@Nonnull IntConsumer consumer)
      This method will make the task run the given callback until eternity. The task will be run with no delay and asynchronously. Do not add other tasks after calling this method.
      Parameters:
      consumer - The callback to run
    • thenLoopAsynchronously

      public void thenLoopAsynchronously(@Nonnull Runnable runnable)
      This method will make the task run the given callback until eternity. The task will be run with no delay and asynchronously. Do not add other tasks after calling this method.
      Parameters:
      runnable - The callback to run
    • thenLoopEvery

      public void thenLoopEvery(int ticks, @Nonnull IntConsumer consumer)
      This method will make the task run the given callback until eternity. The task will be run with the given delay and synchronously. Do not add other tasks after calling this method.
      Parameters:
      ticks - The delay between executions (including the start delay)
      consumer - The callback to run
    • thenLoopEvery

      public void thenLoopEvery(int ticks, @Nonnull Runnable runnable)
      This method will make the task run the given callback until eternity. The task will be run with the given delay and synchronously. Do not add other tasks after calling this method.
      Parameters:
      ticks - The delay between executions (including the start delay)
      runnable - The callback to run
    • thenLoopEveryAsynchronously

      public void thenLoopEveryAsynchronously(int ticks, @Nonnull IntConsumer consumer)
      This method will make the task run the given callback until eternity. The task will be run with the given delay and asynchronously. Do not add other tasks after calling this method.
      Parameters:
      ticks - The delay between executions (including the start delay)
      consumer - The callback to run
    • thenLoopEveryAsynchronously

      public void thenLoopEveryAsynchronously(int ticks, @Nonnull Runnable runnable)
      This method will make the task run the given callback until eternity. The task will be run with the given delay and asynchronously. Do not add other tasks after calling this method.
      Parameters:
      ticks - The delay between executions (including the start delay)
      runnable - The callback to run
    • thenWait

      @Nonnull public TaskQueue thenWait(int ticks)
      This method will make the Queue just do nothing for the given amount of ticks. You should not really be using this method but it exists.
      Parameters:
      ticks - The amount of ticks to wait for
      Returns:
      The current instance of TaskQueue