DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamTimer.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.DreamTimer;
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
51public final class DreamTimer {
52
53 private final Plugin plugin;
54 private final long periodTicks;
55 private final Consumer<Integer> onTick; // remaining seconds
56 private final BiConsumer<Integer, Boolean> onPause; // (remaining, paused=true/false)
57 private final Consumer<Integer> onFinish; // final remaining (0 or below)
58
59 private int taskId = -1;
60 private int remainingSeconds;
61 private boolean paused = false;
62 private boolean finished = false;
63
73 public DreamTimer(
74 Plugin plugin,
75 int startingSeconds,
76 long periodTicks,
77 Consumer<Integer> onTick,
78 BiConsumer<Integer, Boolean> onPause,
79 Consumer<Integer> onFinish
80 ) {
81 this.plugin = Objects.requireNonNull(plugin, "plugin");
82 this.remainingSeconds = Math.max(0, startingSeconds);
83 this.periodTicks = Math.max(1L, periodTicks);
84 this.onTick = onTick == null ? s -> {} : onTick;
85 this.onPause = onPause == null ? (s, p) -> {} : onPause;
86 this.onFinish = onFinish == null ? s -> {} : onFinish;
87 }
88
96 public void start() {
97 if (taskId != -1) return;
98 finished = false;
99 paused = false;
100 taskId = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
101 if (finished || paused) return;
102
103 // fire tick first (with current remaining)
104 onTick.accept(remainingSeconds);
105 Bukkit.getPluginManager().callEvent(new TimerTickEvent(this, remainingSeconds));
106
107 // then decrement and test finish
108 remainingSeconds--;
109 if (remainingSeconds <= 0) {
110 finish();
111 }
112 }, 0L, periodTicks).getTaskId();
113 Bukkit.getPluginManager().callEvent(new TimerStartedEvent(this, remainingSeconds));
114 }
115
118 public void pause() {
119 if (taskId == -1 || paused || finished) return;
120 paused = true;
121 onPause.accept(remainingSeconds, true);
122 Bukkit.getPluginManager().callEvent(new TimerPausedEvent(this, remainingSeconds));
123 }
124
127 public void resume() {
128 if (taskId == -1 || !paused || finished) return;
129 paused = false;
130 onPause.accept(remainingSeconds, false);
131 Bukkit.getPluginManager().callEvent(new TimerResumedEvent(this, remainingSeconds));
132 }
133
137 public void cancel() {
138 if (taskId == -1 || finished) return;
139 finish();
140 }
141
142 private void finish() {
143 finished = true;
144 cancelTask();
145 onFinish.accept(Math.max(0, remainingSeconds));
146 Bukkit.getPluginManager().callEvent(new TimerFinishedEvent(this, Math.max(0, remainingSeconds)));
147 }
148
149 private void cancelTask() {
150 if (taskId != -1) {
151 Bukkit.getScheduler().cancelTask(taskId);
152 taskId = -1;
153 }
154 }
155
156 // --- Accessors ---
157
160 public int getRemainingSeconds() { return Math.max(0, remainingSeconds); }
161
163 public boolean isRunning() { return taskId != -1 && !paused && !finished; }
164
166 public boolean isPaused() { return paused; }
167
169 public boolean isFinished(){ return finished; }
170
176 public static String formatHMS(int seconds) {
177 int m = seconds / 60;
178 int s = seconds % 60;
179 return String.format("%02d:%02d", m, s);
180 }
181}
A countdown timer (counts down to zero) with pause/resume and Bukkit events.
boolean isRunning()
True if the timer is currently running (started, not paused, not finished).
static String formatHMS(int seconds)
Utility formatter: mm:ss (e.g., 03:07).
void cancel()
Cancels the timer early (fires finish events and callbacks).
boolean isFinished()
True if the timer has finished or was cancelled.
DreamTimer(Plugin plugin, int startingSeconds, long periodTicks, Consumer< Integer > onTick, BiConsumer< Integer, Boolean > onPause, Consumer< Integer > onFinish)
Constructs a countdown timer.
boolean isPaused()
True if the timer is paused.
int getRemainingSeconds()
Remaining seconds (never negative).
Fired when a DreamTimer finishes or is cancelled.
Fired each period while a running DreamTimer is active (before decrement).