diff options
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java | 15 | ||||
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | 45 |
2 files changed, 48 insertions, 12 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java b/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java index 503d3a64..aa10b29a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java +++ b/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java @@ -22,14 +22,22 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.minecraft.client.MinecraftClient; +/** + * Main class for Skyblocker which initializes features, registers events, and manages ticks. This class will be instantiated by Fabric. Do not instantiate this class. + */ public class SkyblockerMod implements ClientModInitializer { public static final String NAMESPACE = "skyblocker"; private static SkyblockerMod INSTANCE; + @SuppressWarnings("deprecation") public final Scheduler scheduler = new Scheduler(); public final ContainerSolverManager containerSolverManager = new ContainerSolverManager(); public final StatusBarTracker statusBarTracker = new StatusBarTracker(); + /** + * Do not instantiate this class. Use {@link #getInstance()} instead. + */ + @Deprecated public SkyblockerMod() { INSTANCE = this; } @@ -38,6 +46,9 @@ public class SkyblockerMod implements ClientModInitializer { return INSTANCE; } + /** + * Register {@link #tick(MinecraftClient)} to {@link ClientTickEvents#END_CLIENT_TICK}, initialize all features, and schedule tick events. + */ @Override public void onInitializeClient() { ClientTickEvents.END_CLIENT_TICK.register(this::tick); @@ -62,6 +73,10 @@ public class SkyblockerMod implements ClientModInitializer { scheduler.scheduleCyclic(DwarvenHud::update, 40); } + /** + * Ticks the scheduler. Called once at the end of every client tick through {@link ClientTickEvents#END_CLIENT_TICK}. + * @param client the Minecraft client. + */ public void tick(MinecraftClient client) { scheduler.tick(); } diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index 16e5b023..efba4995 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java @@ -1,27 +1,46 @@ 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; + /** + * 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) 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) LOGGER.error("Attempted to schedule a cyclic task with period lower than 1"); @@ -38,23 +57,25 @@ public class Scheduler { } } - 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; - } - + /** + * 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 + */ + private 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 + */ + private record ScheduledTask(Runnable inner, int schedule) implements Comparable<ScheduledTask>, Runnable { @Override public int compareTo(ScheduledTask o) { return schedule - o.schedule; |