DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamStopwatch.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamStopwatch;
25
26import org.bukkit.Bukkit;
27import org.bukkit.plugin.Plugin;
28
29import java.util.Objects;
30import java.util.function.BiConsumer;
31import java.util.function.Consumer;
32
52public final class DreamStopwatch {
53
54 private final Plugin plugin;
55 private final long periodTicks;
56 private final Consumer<Integer> onTick; // elapsed seconds
57 private final BiConsumer<Integer, Boolean> onPause; // (elapsed, paused=true/false)
58 private final Consumer<Integer> onStop; // final elapsed seconds
59
60 private int taskId = -1;
61 private int elapsedSeconds = 0;
62 private boolean paused = false;
63 private boolean stopped = false;
64
74 Plugin plugin,
75 long periodTicks,
76 Consumer<Integer> onTick,
77 BiConsumer<Integer, Boolean> onPause,
78 Consumer<Integer> onStop
79 ) {
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;
85 }
86
88 public void start() {
89 if (taskId != -1) return;
90 stopped = false;
91 paused = false;
92 taskId = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
93 if (stopped || paused) return;
94 elapsedSeconds++;
95 // callback
96 onTick.accept(elapsedSeconds);
97 // event
98 Bukkit.getPluginManager().callEvent(new StopwatchTickEvent(this, elapsedSeconds));
99 }, 0L, periodTicks).getTaskId();
100 Bukkit.getPluginManager().callEvent(new StopwatchStartedEvent(this));
101 }
102
104 public void pause() {
105 if (taskId == -1 || paused || stopped) return;
106 paused = true;
107 onPause.accept(elapsedSeconds, true);
108 Bukkit.getPluginManager().callEvent(new StopwatchPausedEvent(this, elapsedSeconds));
109 }
110
112 public void resume() {
113 if (taskId == -1 || !paused || stopped) return;
114 paused = false;
115 onPause.accept(elapsedSeconds, false);
116 Bukkit.getPluginManager().callEvent(new StopwatchResumedEvent(this, elapsedSeconds));
117 }
118
120 public void stop() {
121 if (taskId == -1 || stopped) return;
122 stopped = true;
123 cancelTask();
124 onStop.accept(elapsedSeconds);
125 Bukkit.getPluginManager().callEvent(new StopwatchStoppedEvent(this, elapsedSeconds));
126 }
127
129 public void reset() {
130 elapsedSeconds = 0;
131 paused = false;
132 stopped = false;
133 }
134
135 private void cancelTask() {
136 if (taskId != -1) {
137 Bukkit.getScheduler().cancelTask(taskId);
138 taskId = -1;
139 }
140 }
141
142 // --- Accessors ---
143
145 public int getElapsedSeconds() { return elapsedSeconds; }
147 public boolean isRunning() { return taskId != -1 && !paused && !stopped; }
149 public boolean isPaused() { return paused; }
151 public boolean isStopped() { return stopped; }
152
158 public static String formatHMS(int seconds) {
159 int m = seconds / 60;
160 int s = seconds % 60;
161 return String.format("%02d:%02d", m, s);
162 }
163}
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.
boolean isRunning()
True if running (started, not paused, not stopped).
void stop()
Stops the stopwatch and cancels the scheduled task.
static String formatHMS(int seconds)
Utility formatter: mm:ss (e.g., 03:07).
void start()
Starts the stopwatch. No-op if already started.
Fired each tick period while a DreamStopwatch is running.