book-blankScheduling and Async

The task API allows JavaScript scripts to schedule and manage tasks within your plugin environment. It's a bridge to Minecraft's asynchronous and scheduled task systems, designed with Folia compatibility in mind.


Overview

You can access the task object directly in your script. It offers utilities to:

  • Pause execution (wait)

  • Wait for other scripts or plugins to load

  • Run functions asynchronously or on the main thread

  • Delay or repeat function execution

  • Schedule tasks per entity

  • Cancel scheduled tasks


Usage Example

task.spawn(() => {
  task.wait(1.5); // Wait 1.5 seconds
  log.info("Spawned task finished waiting!");
});

task.repeat(2, 5, () => {
  log.info("Repeating every 5 seconds (after 2s delay)");
});

API Reference

task.wait(seconds: number): void

Pauses the script execution for the specified number of seconds (can be fractional).

circle-exclamation

Example:


task.waitForScript(scriptName: string): void

Waits until the specified script is fully loaded and running.

Example:


task.waitForPlugin(pluginName: string): void

Waits until a specific plugin is loaded and enabled.

Example:


task.spawn(func: () => void): number

Runs a function asynchronously. Returns the taskId so it can be cancelled later.

Example:

circle-info

On Folia:

  • task.spawn() and related functions will create real threads and submit them to the thread pool. This means you’re not limited by the Cpu thread count. You can run as many concurrent tasks as needed!

On Non-Folia Servers (Bukkit, Spigot, Paper):

  • These methods will fall back to Bukkit’s async scheduler, meaning they still run off the main thread, but within Bukkit’s thread limits. You can still spawn many async tasks, making this API a safe and compatible choice across all server types.


task.main(func: () => void): number

Runs the function on the main server thread. Use for tasks that must access Bukkit APIs that aren’t thread-safe.

Example:

triangle-exclamation

task.entitySchedule(entity: Entity, func: () => void): number

Runs the function in context of a specific entity (Folia only). This is ideal for AI or entity-specific logic.

Example:

circle-check

Compatibility Note


task.delay(delay: number, func: () => void): number

Runs a function once after the specified delay (in seconds). Returns a taskId.

Example:


task.repeat(delay: number, period: number, func: () => void): number

Repeats a function periodically, starting after a delay. Time is in seconds. Returns a taskId.

Example:


task.cancel(taskId: number): void

Cancels a scheduled task using its taskId.

Example:


circle-info

Notes

  • Thread Safety: Use task.main() or task.entitySchedule() if accessing Bukkit or Minecraft internals.

  • Exception Handling: Errors in scripts are caught and logged; they won't crash the server.

  • Integration: All tasks are registered per-script and tracked for clean cancellation on script reload.


Last updated

Was this helpful?