Interface TaskContext

All Superinterfaces:
Executor
All Known Implementing Classes:
Context

@NonExtendable public interface TaskContext extends Executor
Submits tasks with one thread affinity. Also an 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 Type
    Method
    Description
    void
    execute(Runnable command)
    Runs the command through run(Runnable), discarding its handle.
    boolean
    Whether the calling thread owns this context right now, whether or not it is running inside a task submitted through this API.
    run(Runnable task)
    Runs once, never inline, even on the context's own thread; on tick contexts, on the next tick.
    runLater(Duration delay, Runnable task)
    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.
    supply(Supplier<? extends T> task)
    Runs task once (as run(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

      TaskHandle run(Runnable task)
      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 ends TaskState.FAILED and its completion fails with that throwable.
      Throws:
      RejectedExecutionException - if the owner is unloaded
    • runLater

      TaskHandle runLater(Duration delay, Runnable task)
      Runs once after a delay. Zero behaves as run(Runnable). Failure logging and exceptional completion are the same as for run.
      Throws:
      IllegalArgumentException - if the delay is negative
      RejectedExecutionException - if the owner is unloaded
    • runRepeating

      TaskHandle 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. 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 async
      RejectedExecutionException - if the owner is unloaded
    • supply

      <T> CompletableFuture<T> supply(Supplier<? extends T> task)
      Runs task once (as run(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 follows CompletableFuture rules: 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 with TaskRetiredException.
      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 true and every other thread gets false. Under Folia the answer follows region ownership. The async context is owned by no thread and always answers false; 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

      void execute(Runnable command)
      Runs the command through run(Runnable), discarding its handle. A thrown failure is logged exactly once at WARNING with the owner's name and original throwable.
      Specified by:
      execute in interface Executor
      Throws:
      RejectedExecutionException - if the owner is unloaded or the entity is already removed