24package com.dreamfirestudios.dreamcore.DreamStopwatch;
26import org.bukkit.Bukkit;
27import org.bukkit.plugin.Plugin;
29import java.util.Objects;
30import java.util.function.BiConsumer;
31import java.util.function.Consumer;
54 private final Plugin plugin;
55 private final long periodTicks;
56 private final Consumer<Integer> onTick;
57 private final BiConsumer<Integer, Boolean> onPause;
58 private final Consumer<Integer> onStop;
60 private int taskId = -1;
61 private int elapsedSeconds = 0;
62 private boolean paused =
false;
63 private boolean stopped =
false;
76 Consumer<Integer> onTick,
77 BiConsumer<Integer, Boolean> onPause,
78 Consumer<Integer> onStop
80 this.plugin = Objects.requireNonNull(plugin,
"plugin");
81 this.periodTicks = Math.max(1L, periodTicks);
82 this.onTick = onTick ==
null ? s -> {} : onTick;
83 this.onPause = onPause ==
null ? (s, p) -> {} : onPause;
84 this.onStop = onStop ==
null ? s -> {} : onStop;
89 if (taskId != -1)
return;
92 taskId = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
93 if (stopped || paused)
return;
96 onTick.accept(elapsedSeconds);
99 }, 0L, periodTicks).getTaskId();
105 if (taskId == -1 || paused || stopped)
return;
107 onPause.accept(elapsedSeconds,
true);
113 if (taskId == -1 || !paused || stopped)
return;
115 onPause.accept(elapsedSeconds,
false);
121 if (taskId == -1 || stopped)
return;
124 onStop.accept(elapsedSeconds);
135 private void cancelTask() {
137 Bukkit.getScheduler().cancelTask(taskId);
147 public boolean isRunning() {
return taskId != -1 && !paused && !stopped; }
159 int m = seconds / 60;
160 int s = seconds % 60;
161 return String.format(
"%02d:%02d", m, s);
A simple tick-driven stopwatch (counts up) with pause/resume and events.
DreamStopwatch(Plugin plugin, long periodTicks, Consumer< Integer > onTick, BiConsumer< Integer, Boolean > onPause, Consumer< Integer > onStop)
Creates a new stopwatch.
void pause()
Pauses the stopwatch (no tick increments while paused).
void reset()
Resets elapsed time to zero. Does not start the task.
int getElapsedSeconds()
Gets elapsed whole seconds.
boolean isPaused()
True if paused.
boolean isRunning()
True if running (started, not paused, not stopped).
void stop()
Stops the stopwatch and cancels the scheduled task.
void resume()
Resumes a paused stopwatch.
static String formatHMS(int seconds)
Utility formatter: mm:ss (e.g., 03:07).
void start()
Starts the stopwatch. No-op if already started.
boolean isStopped()
True if stopped.
Fired when a DreamStopwatch is paused.
Fired when a DreamStopwatch is resumed.
Fired when a DreamStopwatch starts.
Fired when a DreamStopwatch stops.
Fired each tick period while a DreamStopwatch is running.