Interface TaskContext
- All Superinterfaces:
Executor
- All Known Implementing Classes:
Context
Executor for future continuations,
such as future.thenApplyAsync(fn, scheduler.global()).
Delays and periods are Durations. Tick contexts (global, region, entity) round them up
to whole ticks with Ticks.ceil(Duration) (50 ms nominal per tick, so they stretch when the
server lags); a zero delay means the next tick, never the current one. The async context uses
wall-clock time: a task starts no earlier than its delay after submission, on a thread that owns
no tick context, but no upper bound is promised beyond "eventually" (it depends on the server's
async pool). An async task submitted with run(Runnable) may start on another thread before
run returns; that is still not inline. Whether two executions of one repeating async task
can overlap is not specified in 7.0.
On an entity that is already removed, run, runLater and runRepeating return a handle that is already RETIRED (its completion fails with TaskRetiredException), supply returns a future already failed with TaskRetiredException, and execute throws RejectedExecutionException.
-
Method Summary
Modifier and TypeMethodDescriptionvoidRuns the command throughrun(Runnable), discarding its handle.booleanWhether the calling thread owns this context right now, whether or not it is running inside a task submitted through this API.Runs once, never inline, even on the context's own thread; on tick contexts, on the next tick.Runs once after a delay.runRepeating(Duration initialDelay, Duration period, Consumer<? super TaskHandle> task) Runs after the initial delay and then at the given period until cancelled, retired or unloaded.<T> CompletableFuture<T> Runstaskonce (asrun(Runnable)) and completes the returned future with its result, or exceptionally with what it threw, without logging the failure while the future can still observe it.
-
Method Details
-
run
Runs once, never inline, even on the context's own thread; on tick contexts, on the next tick. A thrown failure is logged exactly once at WARNING with the owner's name and original throwable; the handle endsTaskState.FAILEDand its completion fails with that throwable.- Throws:
RejectedExecutionException- if the owner is unloaded
-
runLater
Runs once after a delay. Zero behaves asrun(Runnable). Failure logging and exceptional completion are the same as forrun.- Throws:
IllegalArgumentException- if the delay is negativeRejectedExecutionException- if the owner is unloaded
-
runRepeating
Runs after the initial delay and then at the given period until cancelled, retired or unloaded. The consumer receives its own handle and may cancel itself. An exception from one execution is logged with the owner's name and does not stop repetition.- Throws:
IllegalArgumentException- if the initial delay is negative, or the period is less than one tick for a tick context or not positive for asyncRejectedExecutionException- if the owner is unloaded
-
supply
Runstaskonce (asrun(Runnable)) and completes the returned future with its result, or exceptionally with what it threw, without logging the failure while the future can still observe it. The future followsCompletableFuturerules: cancelling it before it completes always succeeds. If the task has not started it then never runs; if it has started it finishes, its result is discarded, and an exception it throws is logged once as a WARNING with the owner's name, because no caller can observe it any more. An entity removed before the start completes the future withTaskRetiredException.- Throws:
RejectedExecutionException- if the owner is unloaded
-
isCurrentThread
boolean isCurrentThread()Whether the calling thread owns this context right now, whether or not it is running inside a task submitted through this API.On Paper every tick context (global, every region, every entity) is owned by the server main thread, so code on the main thread (an event listener, a command, a task) gets
trueand every other thread getsfalse. Under Folia the answer follows region ownership. The async context is owned by no thread and always answersfalse; to check that the caller is off the tick threads use!scheduler.global().isCurrentThread(). For an entity that is already removed the answer is unspecified. -
execute
Runs the command throughrun(Runnable), discarding its handle. A thrown failure is logged exactly once at WARNING with the owner's name and original throwable.- Specified by:
executein interfaceExecutor- Throws:
RejectedExecutionException- if the owner is unloaded or the entity is already removed
-