aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
diff options
context:
space:
mode:
authormsg-programs <msgdoesstuff@gmail.com>2023-06-04 13:44:22 +0200
committermsg-programs <msgdoesstuff@gmail.com>2023-06-04 13:44:22 +0200
commit9170e46f4166573fe837e2a7bdd2747854ab2921 (patch)
tree1b81b1cd1d669e4156d37b98a2841394d936d51f /src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
parent34d87f0e435963cb571568a783c9c4456e811012 (diff)
parent9d114a7be8ac358131441752db14d5356fc9fd3d (diff)
downloadSkyblocker-9170e46f4166573fe837e2a7bdd2747854ab2921.tar.gz
Skyblocker-9170e46f4166573fe837e2a7bdd2747854ab2921.tar.bz2
Skyblocker-9170e46f4166573fe837e2a7bdd2747854ab2921.zip
Merge branch 'master' of https://github.com/SkyblockerMod/Skyblocker into skyhytab
# Conflicts: # src/main/java/me/xmrvizzy/skyblocker/SkyblockerInitializer.java # src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java # src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java # src/main/resources/skyblocker.mixins.json Pull newest changes from upstream master
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java')
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java75
1 files changed, 55 insertions, 20 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
index 16e5b023..02162140 100644
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java
@@ -1,60 +1,95 @@
package me.xmrvizzy.skyblocker.utils;
+import me.xmrvizzy.skyblocker.SkyblockerMod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.PriorityQueue;
+/**
+ * A scheduler for running tasks at a later time. Tasks will be run synchronously on the main client thread. Use the instance stored in {@link SkyblockerMod#scheduler}. Do not instantiate this class.
+ */
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.
+ */
+ @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 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(currentTick + delay, task);
+ }
+ 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 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() {
currentTick += 1;
ScheduledTask task;
- while ((task = tasks.peek()) != null && task.schedule <= currentTick) {
+ while ((task = tasks.peek()) != null && task.schedule <= currentTick && runTask(task)) {
tasks.poll();
- task.run();
}
}
- private class CyclicTask implements Runnable {
- private final Runnable inner;
- private final int period;
-
- public CyclicTask(Runnable task, int period) {
- this.inner = task;
- this.period = period;
- }
+ /**
+ * 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;
+ }
+ /**
+ * 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 period the period in ticks
+ */
+ protected record CyclicTask(Runnable inner, int period) implements Runnable {
@Override
public void run() {
- schedule(this, period);
+ SkyblockerMod.getInstance().scheduler.schedule(this, period);
inner.run();
}
}
- private record ScheduledTask(int schedule, Runnable inner) implements Comparable<ScheduledTask>, Runnable {
+ /**
+ * A task that runs at a specific tick, relative to {@link #currentTick}.
+ *
+ * @param inner the task to run
+ * @param schedule the tick to run at
+ */
+ protected record ScheduledTask(Runnable inner, int schedule) implements Comparable<ScheduledTask>, Runnable {
@Override
public int compareTo(ScheduledTask o) {
return schedule - o.schedule;