diff options
author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2023-06-01 02:26:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-01 02:26:19 -0400 |
commit | d8167863622b082c43afc21b421f325429185aeb (patch) | |
tree | 481637be115406b7c9d1ef78dde4d4290f4bd63b /src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | |
parent | 66387b1fe7dbdf4a0c3e82e53e59a5c24cce6075 (diff) | |
parent | 69e794ffcd06fe14f76f368b33d5af0e4bc60b3a (diff) | |
download | Skyblocker-d8167863622b082c43afc21b421f325429185aeb.tar.gz Skyblocker-d8167863622b082c43afc21b421f325429185aeb.tar.bz2 Skyblocker-d8167863622b082c43afc21b421f325429185aeb.zip |
Merge pull request #162 from kevinthegreat1/livid-color
Livid color
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index efba4995..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,49 +20,62 @@ 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() { 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(); } } /** + * 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 inner the task to run * @param period the period in ticks */ - private record CyclicTask(Runnable inner, int period) implements Runnable { + protected record CyclicTask(Runnable inner, int period) implements Runnable { @Override public void run() { SkyblockerMod.getInstance().scheduler.schedule(this, period); @@ -72,10 +85,11 @@ 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 */ - private record ScheduledTask(Runnable inner, int schedule) implements Comparable<ScheduledTask>, Runnable { + protected record ScheduledTask(Runnable inner, int schedule) implements Comparable<ScheduledTask>, Runnable { @Override public int compareTo(ScheduledTask o) { return schedule - o.schedule; |