Package de.marcely.bedwars.tools
Class ExecutionChain
java.lang.Object
de.marcely.bedwars.tools.ExecutionChain
Simplifies execution of multiple async or time-dependant tasks.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionasync()
Runs the upcoming chain in Bukkit's async thread-pool.asyncLater
(long ticks) Runs the upcoming chain in Bukkit's async thread-pool after a certain amount of ticks.void
cancel()
Tries to cancel the chain of tasks.void
Tries to cancel the chain of tasks.protected ExecutionChain
chain
(ThrowingConsumer<Runnable> runn) Set the error handler for all the tasks in the chain.protected void
static ExecutionChain
Initializes the chain.boolean
Checks if the chain is running right now.run()
Runs the chain of tasks.sync()
Synchronizes the upcoming chain with the main thread.syncLater
(long ticks) Synchronizes the upcoming chain with the main thread after a certain amount of ticks.Adds a task to the chain that will be executed after the one before.<T> ExecutionChain
thenConsumer
(ThrowingConsumer<Consumer<T>> consumer, ThrowingConsumer<T> handler) Adds a consumer-task that is dependant on a future arrival of an object.thenLoose
(ThrowingBiConsumer<Runnable, Consumer<Exception>> looseRunn) Execute a task whose finish might not be instant.thenThrowing
(ThrowingRunnable runn) Adds a task to the chain that will be executed after the one before.
-
Constructor Details
-
ExecutionChain
public ExecutionChain()
-
-
Method Details
-
then
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
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 supplierhandler
- 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
Synchronizes the upcoming chain with the main thread.- Returns:
- The next chain object you work with
-
syncLater
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
Runs the upcoming chain in Bukkit's async thread-pool.- Returns:
- The next chain object you work with
-
asyncLater
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
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
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 chainIllegalStateException
- 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
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
-
handle
-
init
Initializes the chain.- Parameters:
plugin
- The plugin that is running the chain- Returns:
- The object to which you add the first task
-