Class Timer

java.lang.Object
net.risingworld.api.Timer

public class Timer extends Object
A timer which executes at specified intervals.
Example: Create and start a timer which executes every minute and broadcasts a message
1//Create the timer object, this does not start the timer automatically.
2//Interval = 60 seconds, delay = 0 (so it starts immediately),
3//repetitions set to -1 (so it will be repeated infinitely).
4Timer testTimer = new Timer(60f, 0f, -1, () -> { //lambda expression
5 //This will be executed when the timer triggers
6 Server server = getServer();
7 server.broadcastTextMessage("Remember to visit our homepage at <color=yellow>www.rising-world.net"</color>);
8});
9
10//Now start the timer, only when calling this method, the timer will be executed
11testTimer.start();
  • Constructor Summary

    Constructors
    Constructor
    Description
    Timer(float interval, float delay, int repetitions, Runnable task)
    Creates a new timer.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Timer
    findTimer(int id)
    Looks if there is an active timer with the provided id.
    int
    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.
    Gets the task which will be executed once the timer triggers, or null if no task was set for this timer.
    float
    Used internally! Returns the current "tick" of this timer.
    boolean
    Gets whether or not this timer is currently active.
    boolean
     
    boolean
    Gets whether or not this timer is currently paused.
    void
    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
    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
    Starts the timer.
    static boolean
    timerExists(int id)
    Determines if an active timer with the provided id exists.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Timer

      public Timer(float interval, float delay, int repetitions, Runnable task)
      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 provide null and set the task at a later stage (see setTask(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()
      Pauses this timer. This has no effect if the timer is currently not active. You can always resume by calling start()

      Please note: If you want to kill the timer and release all its resources, you have to call kill() instead.
    • 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, see isPaused()).
      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 take isActive() 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

      public Runnable 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

      public void setTask(Runnable task)
      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
      1//Create new timer with "null" task, set it to final (since we want to reference it from within the runnable)
      2final Timer timer = new Timer(0.5f, 0f, -1, null);
      3
      4//Now create the task/runnable, it's safe to reference the timer from the runnable
      5Runnable timerTask = () -> {
      6 //check condition and if it's true, kill the timer
      7 if(...){
      8 timer.kill();
      9 }
      10};
      11
      12//Remember to assign the runnable to the timer
      13timer.setTask(timerTask);
      14
      15//Start the timer
      16timer.start();
    • 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

      public static Timer findTimer(int id)
      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.