Class Countdown<T, SELF extends Countdown<T,SELF>>

java.lang.Object
dev.bouncingelf10.timelesslib.api.countdown.Countdown<T,SELF>
Type Parameters:
T - Context type (e.g. the server or client instance) passed to handlers.
SELF - Concrete subtype, so builder-style methods return the right type. See ServerCountdown / ClientCountdown.
All Implemented Interfaces:
TaskHandle
Direct Known Subclasses:
ClientCountdown, ServerCountdown

public abstract class Countdown<T, SELF extends Countdown<T,SELF>> extends Object implements TaskHandle
A single countdown-to-zero task, produced by ServerCountdownManager.start(Duration) / ClientCountdownManager.start(Duration).
Implements the same TaskHandle lifecycle (cancel(), pause(), resume()) as a plain scheduled task, plus richer countdown-shaped notifications (tick, finish, threshold, custom-interval handlers).
  • Method Details

    • cancel

      public boolean cancel()
      Cancels the countdown.
      Specified by:
      cancel in interface TaskHandle
      Returns:
      true if the countdown was cancelled, false if it was already finished or canceled
    • pause

      public boolean pause()
      Pauses the countdown. Note: If the countdown is already paused, this method does nothing.
      Specified by:
      pause in interface TaskHandle
      Returns:
      true if the countdown was paused, false if it was already finished or canceled
    • resume

      public boolean resume()
      Resumes a paused countdown. Note: If the countdown is not paused, this method does nothing.
      Specified by:
      resume in interface TaskHandle
      Returns:
      true if the countdown was resumed, false if it was already finished or canceled
    • pauseOrUnpause

      public boolean pauseOrUnpause()
      Toggles to pause or unpause the countdown.
      Specified by:
      pauseOrUnpause in interface TaskHandle
      Returns:
      true if the countdown was changed state, false if it was already finished or canceled
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface TaskHandle
      Returns:
      true if the task has been canceled
    • isPaused

      public boolean isPaused()
      Specified by:
      isPaused in interface TaskHandle
    • isFinished

      public boolean isFinished()
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface TaskHandle
    • isScheduled

      public boolean isScheduled()
      Specified by:
      isScheduled in interface TaskHandle
      Returns:
      true if the task is scheduled for execution
    • getRemainingDelay

      public Optional<Duration> getRemainingDelay()
      Description copied from interface: TaskHandle
      Returns the remaining delay before the next run.
      Specified by:
      getRemainingDelay in interface TaskHandle
      Returns:
      remaining delay, or empty if canceled
    • getPeriod

      public Optional<Duration> getPeriod()
      Description copied from interface: TaskHandle
      Returns the period of a repeating task.
      Specified by:
      getPeriod in interface TaskHandle
      Returns:
      the period, or empty if not repeating
    • runNow

      public boolean runNow()
      Description copied from interface: TaskHandle
      Runs the task immediately if possible.
      Specified by:
      runNow in interface TaskHandle
      Returns:
      true if the task was executed
    • remaining

      public Duration remaining()
    • elapsed

      public Duration elapsed()
      Returns:
      time elapsed since the countdown started, clamped to the total duration.
    • progress

      public double progress()
      Returns:
      progress from 0.0 (just started) to 1.0 (finished).
    • id

      public net.minecraft.resources.Identifier id()
      Specified by:
      id in interface TaskHandle
      Returns:
      unique ID of this task
    • onTick

      public SELF onTick(BiConsumer<T,Duration> handler)
      Adds a task to execute every countdown tick specified by the tick interval.
      Parameters:
      handler - Handler to execute every countdown tick.
    • onTick

      public SELF onTick(Consumer<Duration> handler)
      Adds a task to execute every countdown tick specified by the tick interval, without needing the context.
      Parameters:
      handler - Handler to execute every countdown tick.
    • onFinish

      public SELF onFinish(Consumer<T> handler)
      Adds a task to execute when the countdown finishes.
      Parameters:
      handler - Handler to execute when the countdown finishes.
    • onFinish

      public SELF onFinish(Runnable handler)
      Adds a task to execute when the countdown finishes, without needing the context.
      Parameters:
      handler - Handler to execute when the countdown finishes.
    • onThreshold

      public SELF onThreshold(Duration threshold, Consumer<T> handler)
      Adds a task to execute when the countdown reaches the specified threshold.
      Parameters:
      threshold - Duration threshold to reach.
      handler - Handler to execute when the threshold is reached.
    • every

      public SELF every(Duration interval, Consumer<T> handler)
      Adds a task to execute every time the specified interval elapses. (Like onTick(BiConsumer), but with a specified interval instead of the countdown tick interval)
      Parameters:
      interval - Interval at which to execute the handler.
      handler - Handler to execute every time the interval elapses.