Skip to main content

Core Concepts

Types of Schedulers:

  1. syncScheduler
    • Executes tasks on the main thread, ideal for interacting with the main game/server logic.
  2. asyncScheduler
    • Executes tasks on a separate thread, used for tasks that are resource-intensive or should not block the main thread.
  3. scheduler
    • A generic interface for managing both sync and async tasks based on user preferences or system needs.

Common Methods

  1. after(delay, timeUnit)
    • Schedules a task to execute after a delay.
    • Arguments:
      • delay: The amount of time to wait.
      • timeUnit: Unit of time (e.g., TimeUnit.SECONDS).
  2. every(interval, timeUnit)
    • Schedules a repeating task.
    • Arguments:
      • interval: Time between consecutive executions.
      • timeUnit: Unit of time.
  3. run(action)
    • Executes the defined action.
    • Arguments:
      • action: A Runnable or method reference (e.g., this::taskMethod).

Examples

Synchronous Scheduler (syncScheduler)

Example 1: Run a Task After Delay

BasePlugin.syncScheduler()
.after(5, TimeUnit.SECONDS)
.run(() -> System.out.println("Task executed on the main thread after 5 seconds."));

Example 2: Repeating Task

BasePlugin.syncScheduler()
.every(10, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running on the main thread every 10 seconds."));

Use Case:

  • Modify the game state or server settings safely on the main thread.

Asynchronous Scheduler (asyncScheduler)

Example 1: Background Task Execution

BasePlugin.asyncScheduler()
.after(3, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Heavy task started asynchronously.");
// Simulate heavy computation
});

Example 2: Repeated Background Task

BasePlugin.asyncScheduler()
.every(5, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running asynchronously every 5 seconds."));

Use Case:

  • Handle I/O operations or computational tasks without blocking the main thread.

Generic Scheduler (scheduler)

Example 1: Conditional Task Type

boolean useAsync = true;

BasePlugin.scheduler(useAsync)
.after(2, TimeUnit.SECONDS)
.run(() -> System.out.println("This task runs on " + (useAsync ? "an async thread." : "the main thread.")));

Example 2: Dynamic Task Type for Repeating Jobs

boolean isAsync = false;

BasePlugin.scheduler(isAsync)
.every(3, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task " + (isAsync ? "asynchronously" : "synchronously")));

Advanced Use Cases

  1. Chaining Multiple Tasks
BasePlugin.syncScheduler()
.after(5, TimeUnit.SECONDS)
.run(() -> System.out.println("First task executed."))
.after(7, TimeUnit.SECONDS)
.run(() -> System.out.println("Second task executed 2 seconds later."));
  1. Handling Task Cancellation
ScheduledTask task = BasePlugin.asyncScheduler()
.every(1, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running."));

BasePlugin.syncScheduler()
.after(10, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Cancelling the repeating task after 10 seconds.");
task.cancel();
});
  1. Combining Sync and Async Tasks
BasePlugin.asyncScheduler()
.after(2, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Async task running. Switching to sync task...");
BasePlugin.syncScheduler()
.run(() -> System.out.println("Sync task running after async task!"));
});
  1. Delayed Execution with Conditions
BasePlugin.syncScheduler()
.after(3, TimeUnit.SECONDS)
.run(() -> {
if (Math.random() > 0.5) {
System.out.println("Condition met. Task executed.");
} else {
System.out.println("Condition not met. Task skipped.");
}
});
  1. Long-Running Tasks (Split into Smaller Steps)
BasePlugin.asyncScheduler()
.every(1, TimeUnit.SECONDS)
.run(new Runnable() {
private int step = 0;

@Override
public void run() {
if (step >= 5) {
System.out.println("Task complete.");
return; // Task ends after 5 iterations
}
System.out.println("Executing step " + (++step));
}
});

Comparison of Schedulers

FeaturesyncSchedulerasyncSchedulerscheduler
ThreadMain threadWorker threadConfigurable (sync/async)
Use CaseGame/server logicBackground tasksDepends on user preference
PerformanceLimited by main threadNon-blocking, fasterFlexible
Task RepetitionSupportedSupportedSupported
ChainingSupportedSupportedSupported

That's all