Package net.risingworld.api
Class Timer
java.lang.Object
net.risingworld.api.Timer
A timer which executes at specified intervals.
Example: Create and start a timer which executes every minute and broadcasts a message
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Timer
findTimer
(int id) Looks if there is an active timer with the provided id.int
getID()
Gets the ID of this timer.float
Gets the remaining initial delay (seconds) of this timer.float
Gets the interval (seconds) of this timer.float
Gets the passed seconds (i.e how long this timer is already active).int
Gets how often the timer is supposed to trigger / repeat.getTask()
Gets the task which will be executed once the timer triggers, or null if no task was set for this timer.float
getTick()
Used internally! Returns the current "tick" of this timer.boolean
isActive()
Gets whether or not this timer is currently active.boolean
isKilled()
boolean
isPaused()
Gets whether or not this timer is currently paused.void
kill()
Kills the timer and releases all resources which belong to this timer.static boolean
killTimer
(int id) Kills the timer which has the provided id.void
pause()
Pauses this timer.void
reinit
(float interval, float delay, int repetitions) void
setInitialDelay
(float delay) Sets the initial delay for this timer.void
setInterval
(float interval) Sets the interval for this timer.void
setRepetitions
(int repetitions) Sets the amount of repetitions, i.e how often the timer is supposed to trigger / repeat.void
Sets a new task which will be executed once the timer triggers.void
setTick
(float tick) Used internally! Do not call this method manually.
Increments the internal "tick" of this timer.void
start()
Starts the timer.static boolean
timerExists
(int id) Determines if an active timer with the provided id exists.
-
Constructor Details
-
Timer
Creates a new timer.- Parameters:
interval
- the interval in seconds. Only applies to subsequent repetitions (i.e does not apply to first execution, in this case, set the initial delay instead).delay
- the initial delay in seconds.repetitions
- determines how often the timer should repeat. Use 0 if the timer should only trigger once, or -1 if the timer should repeat infinitely.task
- a runnable which is executed when the timer triggers. It's allowed to providenull
and set the task at a later stage (seesetTask(java.lang.Runnable)
).
-
-
Method Details
-
start
public void start()Starts the timer. Has no effect if the timer is already active. -
pause
public void pause() -
reinit
public void reinit(float interval, float delay, int repetitions) -
kill
public void kill()Kills the timer and releases all resources which belong to this timer.
It is not possible to resume or restart the timer, so only call this method if you want to discard the timer. -
isActive
public boolean isActive()Gets whether or not this timer is currently active. Note that the timer is still considered as "active" if it's paused (to check if it's paused, seeisPaused()
).- Returns:
- true if the timer is active, false if not.
-
isPaused
public boolean isPaused()Gets whether or not this timer is currently paused. Note that this does not takeisActive()
into account.- Returns:
- true if the timer is paused, false if not.
- See Also:
-
isKilled
public boolean isKilled() -
setInterval
public void setInterval(float interval) Sets the interval for this timer.- Parameters:
interval
- the new interval in seconds.
-
getInterval
public float getInterval()Gets the interval (seconds) of this timer.- Returns:
- the interval in seconds.
-
setInitialDelay
public void setInitialDelay(float delay) Sets the initial delay for this timer. Has no effect if the timer has already started.- Parameters:
delay
- the initial delay in seconds.
-
getInitialDelay
public float getInitialDelay()Gets the remaining initial delay (seconds) of this timer. Returns 0 if the timer has already started.- Returns:
- the remaining initial delay in seconds.
-
setRepetitions
public void setRepetitions(int repetitions) Sets the amount of repetitions, i.e how often the timer is supposed to trigger / repeat. Set to 0 if the timer should only trigger once.- Parameters:
repetitions
- the amount of repetitions.
-
getRepetitions
public int getRepetitions()Gets how often the timer is supposed to trigger / repeat. This value decrements every time the timer triggers (unless it is set to -1).- Returns:
- the remaining amount of repetitions.
-
getTask
Gets the task which will be executed once the timer triggers, or null if no task was set for this timer.- Returns:
- the Runnable task.
-
setTask
Sets a new task which will be executed once the timer triggers. If you set null, no task will be executed when the timer triggers. If another task it already set, it will be overridden.- Parameters:
task
- the new Runnable task.- Example: Create timer and kill it when a certain condition is true
-
getID
public int getID()Gets the ID of this timer.- Returns:
- the timer ID
-
setTick
public void setTick(float tick) Used internally! Do not call this method manually.
Increments the internal "tick" of this timer.- Parameters:
tick
- time per frame.
-
getTick
public float getTick()Used internally! Returns the current "tick" of this timer.- Returns:
- the "tick", i.e the internal timer counter.
-
getPassedSeconds
public float getPassedSeconds()Gets the passed seconds (i.e how long this timer is already active).- Returns:
- the amount of passed second since start of the timer.
-
findTimer
Looks if there is an active timer with the provided id.- Parameters:
id
- the id of the timer.- Returns:
- the timer with the provided id, or null if no timer with this id was found.
-
killTimer
public static boolean killTimer(int id) Kills the timer which has the provided id. If no timer was found, nothing happens.- Parameters:
id
- the timer id.- Returns:
- true if the timer with the provided id existed and if it wasn't killed yet, false if there was either no such timer or if the timer was already killed.
-
timerExists
public static boolean timerExists(int id) Determines if an active timer with the provided id exists.- Parameters:
id
- the id of the timer.- Returns:
- true if a timer with the provided id exists, false if not.
-