Class Scheduler
java.lang.Object
net.vitacraft.serverlibraries.api.scheduling.Scheduler
- All Implemented Interfaces:
ServerScheduler
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> @NotNull Future<T> callSyncMethod(@NotNull Callable<T> task) Calls a method on the main thread and returns a Future object.voidcancelTask(int taskId) Removes task from scheduler.voidRemoves all tasks associated with a particular plugin from the scheduler.@NotNull List<ServerWorker> Returns a list of all active workers.@NotNull List<ServerSchedulerTask> Returns a list of all pending tasks.booleanisCurrentlyRunning(int taskId) Check if the task currently running.booleanisQueued(int taskId) Check if the task queued to be run later.voidrunTask(@NotNull Consumer<? super ServerSchedulerTask> task) Returns a task that will run on the next server tick.@NotNull ServerSchedulerTaskrunTask(@NotNull ServerRunnable task) voidrunTaskAsynchronously(@NotNull Consumer<? super ServerSchedulerTask> task) Returns a task that will run asynchronously.@NotNull ServerSchedulerTaskrunTaskAsynchronously(@NotNull ServerRunnable task) voidrunTaskLater(@NotNull Consumer<? super ServerSchedulerTask> task, long delay) Returns a task that will run after the specified number of server ticks.@NotNull ServerSchedulerTaskrunTaskLater(@NotNull ServerRunnable task, long delay) voidrunTaskLaterAsynchronously(@NotNull Consumer<? super ServerSchedulerTask> task, long delay) Returns a task that will run asynchronously after the specified number of server ticks.@NotNull ServerSchedulerTaskrunTaskLaterAsynchronously(@NotNull ServerRunnable task, long delay) voidrunTaskTimer(@NotNull Consumer<? super ServerSchedulerTask> task, long delay, long period) Returns a task that will repeatedly run until cancelled, starting after the specified number of server ticks.@NotNull ServerSchedulerTaskrunTaskTimer(@NotNull ServerRunnable task, long delay, long period) voidrunTaskTimerAsynchronously(@NotNull Consumer<? super ServerSchedulerTask> task, long delay, long period) Returns a task that will repeatedly run asynchronously until cancelled, starting after the specified number of server ticks.@NotNull ServerSchedulerTaskrunTaskTimerAsynchronously(@NotNull ServerRunnable task, long delay, long period) intscheduleAsyncDelayedTask(@NotNull ServerRunnable task) Schedules a once off task to occur as soon as possible.intscheduleAsyncDelayedTask(@NotNull ServerRunnable task, long delay) Schedules a once off task to occur after a delay.intscheduleAsyncRepeatingTask(@NotNull ServerRunnable task, long delay, long period) Schedules a repeating task.intscheduleSyncDelayedTask(@NotNull ServerRunnable task) intscheduleSyncDelayedTask(@NotNull ServerRunnable task, long delay) intscheduleSyncRepeatingTask(@NotNull ServerRunnable task, long delay, long period)
-
Constructor Details
-
Scheduler
public Scheduler()
-
-
Method Details
-
scheduleSyncDelayedTask
- Specified by:
scheduleSyncDelayedTaskin interfaceServerScheduler- Parameters:
task- Task to be executeddelay- Delay in server ticks before executing task- Returns:
- Task id number (-1 if scheduling failed)
-
scheduleSyncDelayedTask
- Specified by:
scheduleSyncDelayedTaskin interfaceServerScheduler- Parameters:
task- Task to be executed- Returns:
- Task id number (-1 if scheduling failed)
-
scheduleSyncRepeatingTask
public int scheduleSyncRepeatingTask(@NotNull @NotNull ServerRunnable task, long delay, long period) - Specified by:
scheduleSyncRepeatingTaskin interfaceServerScheduler- Parameters:
task- Task to be executeddelay- Delay in server ticks before executing first repeatperiod- Period in server ticks of the task- Returns:
- Task id number (-1 if scheduling failed)
-
scheduleAsyncDelayedTask
Description copied from interface:ServerSchedulerSchedules a once off task to occur after a delay. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncDelayedTaskin interfaceServerScheduler- Parameters:
task- Task to be executeddelay- Delay in server ticks before executing task- Returns:
- Task id number (-1 if scheduling failed)
-
scheduleAsyncDelayedTask
Description copied from interface:ServerSchedulerSchedules a once off task to occur as soon as possible. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncDelayedTaskin interfaceServerScheduler- Parameters:
task- Task to be executed- Returns:
- Task id number (-1 if scheduling failed)
-
scheduleAsyncRepeatingTask
public int scheduleAsyncRepeatingTask(@NotNull @NotNull ServerRunnable task, long delay, long period) Description copied from interface:ServerSchedulerSchedules a repeating task. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncRepeatingTaskin interfaceServerScheduler- Parameters:
task- Task to be executeddelay- Delay in server ticks before executing first repeatperiod- Period in server ticks of the task- Returns:
- Task id number (-1 if scheduling failed)
-
callSyncMethod
Description copied from interface:ServerSchedulerCalls a method on the main thread and returns a Future object. This task will be executed by the main server thread.- Note: The Future.get() methods must NOT be called from the main thread.
- Note2: There is at least an average of 10ms latency until the isDone() method returns true.
- Specified by:
callSyncMethodin interfaceServerScheduler- Type Parameters:
T- The callable's return type- Parameters:
task- Task to be executed- Returns:
- Future Future object related to the task
-
cancelTask
public void cancelTask(int taskId) Description copied from interface:ServerSchedulerRemoves task from scheduler.- Specified by:
cancelTaskin interfaceServerScheduler- Parameters:
taskId- Id number of task to be removed
-
cancelTasks
public void cancelTasks()Description copied from interface:ServerSchedulerRemoves all tasks associated with a particular plugin from the scheduler.- Specified by:
cancelTasksin interfaceServerScheduler
-
isCurrentlyRunning
public boolean isCurrentlyRunning(int taskId) Description copied from interface:ServerSchedulerCheck if the task currently running.A repeating task might not be running currently, but will be running in the future. A task that has finished, and does not repeat, will not be running ever again.
Explicitly, a task is running if there exists a thread for it, and that thread is alive.
- Specified by:
isCurrentlyRunningin interfaceServerScheduler- Parameters:
taskId- The task to check.- Returns:
- If the task is currently running.
-
isQueued
public boolean isQueued(int taskId) Description copied from interface:ServerSchedulerCheck if the task queued to be run later.If a repeating task is currently running, it might not be queued now but could be in the future. A task that is not queued, and not running, will not be queued again.
- Specified by:
isQueuedin interfaceServerScheduler- Parameters:
taskId- The task to check.- Returns:
- If the task is queued to be run.
-
getActiveWorkers
Description copied from interface:ServerSchedulerReturns a list of all active workers.This list contains asynch tasks that are being executed by separate threads.
- Specified by:
getActiveWorkersin interfaceServerScheduler- Returns:
- Active workers
-
getPendingTasks
Description copied from interface:ServerSchedulerReturns a list of all pending tasks. The ordering of the tasks is not related to their order of execution.- Specified by:
getPendingTasksin interfaceServerScheduler- Returns:
- Active workers
-
runTask
public void runTask(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will run on the next server tick.- Specified by:
runTaskin interfaceServerScheduler- Parameters:
task- the task to be run- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTask
@NotNull public @NotNull ServerSchedulerTask runTask(@NotNull @NotNull ServerRunnable task) throws IllegalArgumentException - Specified by:
runTaskin interfaceServerScheduler- Parameters:
task- the task to be run- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskAsynchronously
public void runTaskAsynchronously(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will run asynchronously.- Specified by:
runTaskAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be run- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskAsynchronously
@NotNull public @NotNull ServerSchedulerTask runTaskAsynchronously(@NotNull @NotNull ServerRunnable task) throws IllegalArgumentException - Specified by:
runTaskAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be run- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskLater
public void runTaskLater(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task, long delay) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will run after the specified number of server ticks.- Specified by:
runTaskLaterin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskLater
@NotNull public @NotNull ServerSchedulerTask runTaskLater(@NotNull @NotNull ServerRunnable task, long delay) throws IllegalArgumentException - Specified by:
runTaskLaterin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskLaterAsynchronously
public void runTaskLaterAsynchronously(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task, long delay) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will run asynchronously after the specified number of server ticks.- Specified by:
runTaskLaterAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskLaterAsynchronously
@NotNull public @NotNull ServerSchedulerTask runTaskLaterAsynchronously(@NotNull @NotNull ServerRunnable task, long delay) throws IllegalArgumentException - Specified by:
runTaskLaterAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskTimer
public void runTaskTimer(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task, long delay, long period) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will repeatedly run until cancelled, starting after the specified number of server ticks.- Specified by:
runTaskTimerin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the taskperiod- the ticks to wait between runs- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskTimer
@NotNull public @NotNull ServerSchedulerTask runTaskTimer(@NotNull @NotNull ServerRunnable task, long delay, long period) throws IllegalArgumentException - Specified by:
runTaskTimerin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the taskperiod- the ticks to wait between runs- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskTimerAsynchronously
public void runTaskTimerAsynchronously(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task, long delay, long period) throws IllegalArgumentException Description copied from interface:ServerSchedulerReturns a task that will repeatedly run asynchronously until cancelled, starting after the specified number of server ticks.- Specified by:
runTaskTimerAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task for the first timeperiod- the ticks to wait between runs- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-
runTaskTimerAsynchronously
@NotNull public @NotNull ServerSchedulerTask runTaskTimerAsynchronously(@NotNull @NotNull ServerRunnable task, long delay, long period) throws IllegalArgumentException - Specified by:
runTaskTimerAsynchronouslyin interfaceServerScheduler- Parameters:
task- the task to be rundelay- the ticks to wait before running the task for the first timeperiod- the ticks to wait between runs- Returns:
- a ServerSchedulerTask that contains the id number
- Throws:
IllegalArgumentException- if plugin is nullIllegalArgumentException- if task is null
-