Class ExecutionChain

java.lang.Object
de.marcely.bedwars.tools.ExecutionChain

public class ExecutionChain extends Object
Simplifies execution of multiple async or time-dependant tasks.
  • Constructor Details

    • ExecutionChain

      public ExecutionChain()
  • Method Details

    • then

      public ExecutionChain then(Runnable runn)
      Adds a task to the chain that will be executed after the one before.
      Parameters:
      runn - The task to be executed
      Returns:
      The next chain object you work with
    • thenThrowing

      public ExecutionChain thenThrowing(ThrowingRunnable runn)
      Adds a task to the chain that will be executed after the one before.
      Parameters:
      runn - The task to be executed
      Returns:
      The next chain object you work with
    • thenConsumer

      public <T> ExecutionChain thenConsumer(ThrowingConsumer<Consumer<T>> consumer, ThrowingConsumer<T> handler)
      Adds a consumer-task that is dependant on a future arrival of an object.
      Parameters:
      consumer - Returns the consumer that you pass to the supplier
      handler - Your task that you want to execute with the received object
      Returns:
      The next chain object you work with
    • thenLoose

      Execute a task whose finish might not be instant.
      Parameters:
      looseRunn - The task to be executed. Returns a runnable that you must run once you are done
      Returns:
      The next chain object you work with
    • sync

      public ExecutionChain sync()
      Synchronizes the upcoming chain with the main thread.
      Returns:
      The next chain object you work with
    • syncLater

      public ExecutionChain syncLater(long ticks)
      Synchronizes the upcoming chain with the main thread after a certain amount of ticks.
      Parameters:
      ticks - The amount of ticks to wait
      Returns:
      The next chain object you work with
    • async

      public ExecutionChain async()
      Runs the upcoming chain in Bukkit's async thread-pool.
      Returns:
      The next chain object you work with
    • asyncLater

      public ExecutionChain asyncLater(long ticks)
      Runs the upcoming chain in Bukkit's async thread-pool after a certain amount of ticks.
      Parameters:
      ticks - The amount of ticks to wait
      Returns:
      The next chain object you work with
    • error

      public ExecutionChain error(Consumer<Exception> errorHandler)
      Set the error handler for all the tasks in the chain.
      Parameters:
      errorHandler - The consumer that gets called in case of an exception in any task
      Returns:
      The next chain object you work with
    • run

      public ExecutionChain run()
      Runs the chain of tasks.
      Returns:
      The next chain object you work with. Only use is to cancel()
      Throws:
      IllegalStateException - If no tasks were added to the chain
      IllegalStateException - If the chain is already running
    • cancel

      public void cancel()
      Tries to cancel the chain of tasks.

      This will only cancel the execution of further tasks in the chain, and not the one that is already running.

    • cancel

      public void cancel(Runnable callback)
      Tries to cancel the chain of tasks.

      This will only cancel the execution of further tasks in the chain, and not the one that is already running.

      Parameters:
      callback - Gets called after the last task has been processed
    • isRunning

      public boolean isRunning()
      Checks if the chain is running right now.

      It isn't running if it either has been cancelled, has finished or never hasn't been run so far.

      Returns:
      true if the chain is running
    • chain

      protected ExecutionChain chain(ThrowingConsumer<Runnable> runn)
    • handle

      protected void handle(Exception ex)
    • init

      public static ExecutionChain init(Plugin plugin)
      Initializes the chain.
      Parameters:
      plugin - The plugin that is running the chain
      Returns:
      The object to which you add the first task