Package civitas.celestis.task
Class DelayedTask
java.lang.Object
civitas.celestis.task.DelayedTask
- All Implemented Interfaces:
Task
A delayed task which is only meant to be executed once. No matter
how many schedulers this task is registered to, it is guaranteed that
it will only be executed once throughout its lifetime.
Delayed tasks can be extended, but is not recommended. The intended
way of using a delayed task is by calling one of its constructors
and providing either a Runnable or a Consumer.
The latter will be provided with the task's delta. (the actual time
it took between registration and execution in milliseconds)
- See Also:
-
Field Summary
Fields inherited from interface civitas.celestis.task.Task
DEFAULT_INTERVAL -
Constructor Summary
ConstructorsConstructorDescriptionDelayedTask(Runnable action, long delay) Creates a new delayed task.DelayedTask(Consumer<Long> action, long delay) Creates a new delayed task. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidexecute(long delta) Handles the execution of this task.final longinterval()The interval will serve as the delay of this task.final voidonRegistered(Scheduler scheduler) Adds a reference to the scheduler to the internal set of schedulers.final voidonUnregistered(Scheduler scheduler) Removes a reference to the scheduler from the internal set of schedulers.
-
Constructor Details
-
DelayedTask
Creates a new delayed task.- Parameters:
action- The action to execute when the delay has passeddelay- The delay of this task in milliseconds
-
DelayedTask
Creates a new delayed task.- Parameters:
action- The action to execute when the delay has passeddelay- The delay of this task in milliseconds
-
-
Method Details
-
execute
public final void execute(long delta) Handles the execution of this task. It first checks if this task has already been executed. If it has not been executed, it executed the stored action, then sets the executed flag totrue. If not, it simply continues. It then proceeds to unregister itself from all registered schedulers, as it no longer requires to be registered to any scheduler. (it will do nothing) -
interval
public final long interval()The interval will serve as the delay of this task. -
onRegistered
Adds a reference to the scheduler to the internal set of schedulers.- Specified by:
onRegisteredin interfaceTask- Parameters:
scheduler- The scheduler this task was registered to
-
onUnregistered
Removes a reference to the scheduler from the internal set of schedulers.- Specified by:
onUnregisteredin interfaceTask- Parameters:
scheduler- The scheduler this task was unregistered from
-