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.void
cancelTask
(int taskId) Removes task from scheduler.void
Removes 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.boolean
isCurrentlyRunning
(int taskId) Check if the task currently running.boolean
isQueued
(int taskId) Check if the task queued to be run later.void
runTask
(@NotNull Consumer<? super ServerSchedulerTask> task) Returns a task that will run on the next server tick.@NotNull ServerSchedulerTask
runTask
(@NotNull ServerRunnable task) void
runTaskAsynchronously
(@NotNull Consumer<? super ServerSchedulerTask> task) Returns a task that will run asynchronously.@NotNull ServerSchedulerTask
runTaskAsynchronously
(@NotNull ServerRunnable task) void
runTaskLater
(@NotNull Consumer<? super ServerSchedulerTask> task, long delay) Returns a task that will run after the specified number of server ticks.@NotNull ServerSchedulerTask
runTaskLater
(@NotNull ServerRunnable task, long delay) void
runTaskLaterAsynchronously
(@NotNull Consumer<? super ServerSchedulerTask> task, long delay) Returns a task that will run asynchronously after the specified number of server ticks.@NotNull ServerSchedulerTask
runTaskLaterAsynchronously
(@NotNull ServerRunnable task, long delay) void
runTaskTimer
(@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 ServerSchedulerTask
runTaskTimer
(@NotNull ServerRunnable task, long delay, long period) void
runTaskTimerAsynchronously
(@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 ServerSchedulerTask
runTaskTimerAsynchronously
(@NotNull ServerRunnable task, long delay, long period) int
scheduleAsyncDelayedTask
(@NotNull ServerRunnable task) Schedules a once off task to occur as soon as possible.int
scheduleAsyncDelayedTask
(@NotNull ServerRunnable task, long delay) Schedules a once off task to occur after a delay.int
scheduleAsyncRepeatingTask
(@NotNull ServerRunnable task, long delay, long period) Schedules a repeating task.int
scheduleSyncDelayedTask
(@NotNull ServerRunnable task) int
scheduleSyncDelayedTask
(@NotNull ServerRunnable task, long delay) int
scheduleSyncRepeatingTask
(@NotNull ServerRunnable task, long delay, long period)
-
Constructor Details
-
Scheduler
public Scheduler()
-
-
Method Details
-
scheduleSyncDelayedTask
- Specified by:
scheduleSyncDelayedTask
in 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:
scheduleSyncDelayedTask
in 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:
scheduleSyncRepeatingTask
in 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:ServerScheduler
Schedules a once off task to occur after a delay. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncDelayedTask
in 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:ServerScheduler
Schedules a once off task to occur as soon as possible. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncDelayedTask
in 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:ServerScheduler
Schedules a repeating task. This task will be executed by a thread managed by the scheduler.- Specified by:
scheduleAsyncRepeatingTask
in 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:ServerScheduler
Calls 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:
callSyncMethod
in 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:ServerScheduler
Removes task from scheduler.- Specified by:
cancelTask
in interfaceServerScheduler
- Parameters:
taskId
- Id number of task to be removed
-
cancelTasks
public void cancelTasks()Description copied from interface:ServerScheduler
Removes all tasks associated with a particular plugin from the scheduler.- Specified by:
cancelTasks
in interfaceServerScheduler
-
isCurrentlyRunning
public boolean isCurrentlyRunning(int taskId) Description copied from interface:ServerScheduler
Check 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:
isCurrentlyRunning
in interfaceServerScheduler
- Parameters:
taskId
- The task to check.- Returns:
- If the task is currently running.
-
isQueued
public boolean isQueued(int taskId) Description copied from interface:ServerScheduler
Check 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:
isQueued
in interfaceServerScheduler
- Parameters:
taskId
- The task to check.- Returns:
- If the task is queued to be run.
-
getActiveWorkers
Description copied from interface:ServerScheduler
Returns a list of all active workers.This list contains asynch tasks that are being executed by separate threads.
- Specified by:
getActiveWorkers
in interfaceServerScheduler
- Returns:
- Active workers
-
getPendingTasks
Description copied from interface:ServerScheduler
Returns a list of all pending tasks. The ordering of the tasks is not related to their order of execution.- Specified by:
getPendingTasks
in interfaceServerScheduler
- Returns:
- Active workers
-
runTask
public void runTask(@NotNull @NotNull Consumer<? super ServerSchedulerTask> task) throws IllegalArgumentException Description copied from interface:ServerScheduler
Returns a task that will run on the next server tick.- Specified by:
runTask
in 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:
runTask
in 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:ServerScheduler
Returns a task that will run asynchronously.- Specified by:
runTaskAsynchronously
in 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:
runTaskAsynchronously
in 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:ServerScheduler
Returns a task that will run after the specified number of server ticks.- Specified by:
runTaskLater
in 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:
runTaskLater
in 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:ServerScheduler
Returns a task that will run asynchronously after the specified number of server ticks.- Specified by:
runTaskLaterAsynchronously
in 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:
runTaskLaterAsynchronously
in 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:ServerScheduler
Returns a task that will repeatedly run until cancelled, starting after the specified number of server ticks.- Specified by:
runTaskTimer
in 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:
runTaskTimer
in 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:ServerScheduler
Returns a task that will repeatedly run asynchronously until cancelled, starting after the specified number of server ticks.- Specified by:
runTaskTimerAsynchronously
in 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:
runTaskTimerAsynchronously
in 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
-