aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-05-30 11:43:13 -0400
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-05-30 11:43:13 -0400
commit69e794ffcd06fe14f76f368b33d5af0e4bc60b3a (patch)
tree49548ab0e5ac81e474ec3831ac2324c18ba23770 /src/main/java
parentd5bdb1ef2bf50084fb59029184224b84f0b99806 (diff)
downloadSkyblocker-69e794ffcd06fe14f76f368b33d5af0e4bc60b3a.tar.gz
Skyblocker-69e794ffcd06fe14f76f368b33d5af0e4bc60b3a.tar.bz2
Skyblocker-69e794ffcd06fe14f76f368b33d5af0e4bc60b3a.zip
Refactored schedulers and added docs
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java22
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java32
2 files changed, 41 insertions, 13 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java
index a46bb9f3..ac6aa293 100644
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java
@@ -7,10 +7,22 @@ import net.minecraft.client.MinecraftClient;
*/
@SuppressWarnings("deprecation")
public class MessageScheduler extends Scheduler {
+ /**
+ * The minimum delay that the server will accept between chat messages.
+ */
+ private static final int MIN_DELAY = 200;
+ /**
+ * The timestamp of the last message send,
+ */
private long lastMessage = 0;
+ /**
+ * Sends a chat message or command after the minimum cooldown. Prefer this method to send messages or commands to the server.
+ *
+ * @param message the message to send
+ */
public void sendMessageAfterCooldown(String message) {
- if (lastMessage + 200 < System.currentTimeMillis()) {
+ if (lastMessage + MIN_DELAY < System.currentTimeMillis()) {
sendMessage(message);
lastMessage = System.currentTimeMillis();
} else {
@@ -29,13 +41,19 @@ public class MessageScheduler extends Scheduler {
}
}
+ /**
+ * Queues a chat message or command to send in {@code delay} ticks. Use this method to send messages or commands a set time in the future. The minimum cooldown is still respected.
+ *
+ * @param message the message to send
+ * @param delay the delay before sending the message in ticks
+ */
public void queueMessage(String message, int delay) {
schedule(() -> sendMessage(message), delay);
}
@Override
protected boolean runTask(Runnable task) {
- if (lastMessage + 200 < System.currentTimeMillis()) {
+ if (lastMessage + MIN_DELAY < System.currentTimeMillis()) {
task.run();
lastMessage = System.currentTimeMillis();
return true;
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
index 7313ec10..02162140 100644
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
@@ -11,8 +11,8 @@ import java.util.PriorityQueue;
*/
public class Scheduler {
private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);
- private int currentTick;
- private final PriorityQueue<ScheduledTask> tasks;
+ private int currentTick = 0;
+ private final PriorityQueue<ScheduledTask> tasks = new PriorityQueue<>();
/**
* Do not instantiate this class. Use {@link SkyblockerMod#scheduler} instead.
@@ -20,32 +20,34 @@ public class Scheduler {
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public Scheduler() {
- currentTick = 0;
- tasks = new PriorityQueue<>();
}
/**
* Schedules a task to run after a delay.
- * @param task the task to run
+ *
+ * @param task the task to run
* @param delay the delay in ticks
*/
public void schedule(Runnable task, int delay) {
- if (delay < 0)
+ if (delay < 0) {
LOGGER.warn("Scheduled a task with negative delay");
+ }
ScheduledTask tmp = new ScheduledTask(task, currentTick + delay);
tasks.add(tmp);
}
/**
* Schedules a task to run every period ticks.
- * @param task the task to run
+ *
+ * @param task the task to run
* @param period the period in ticks
*/
public void scheduleCyclic(Runnable task, int period) {
- if (period <= 0)
+ if (period <= 0) {
LOGGER.error("Attempted to schedule a cyclic task with period lower than 1");
- else
+ } else {
new CyclicTask(task, period).run();
+ }
}
public void tick() {
@@ -56,6 +58,12 @@ public class Scheduler {
}
}
+ /**
+ * Runs the task if able.
+ *
+ * @param task the task to run
+ * @return {@code true} if the task is run, and {@link false} if task is not run.
+ */
protected boolean runTask(Runnable task) {
task.run();
return true;
@@ -63,7 +71,8 @@ public class Scheduler {
/**
* A task that runs every period ticks. More specifically, this task reschedules itself to run again after period ticks every time it runs.
- * @param inner the task to run
+ *
+ * @param inner the task to run
* @param period the period in ticks
*/
protected record CyclicTask(Runnable inner, int period) implements Runnable {
@@ -76,7 +85,8 @@ public class Scheduler {
/**
* A task that runs at a specific tick, relative to {@link #currentTick}.
- * @param inner the task to run
+ *
+ * @param inner the task to run
* @param schedule the tick to run at
*/
protected record ScheduledTask(Runnable inner, int schedule) implements Comparable<ScheduledTask>, Runnable {