From 5f05e7bf6c08880af5c3c00ae15c494ec5b87d9c Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Wed, 17 May 2023 23:11:15 -0400 Subject: Added docs to SkyblockerMod and Scheduler and refactored Scheduler --- .../me/xmrvizzy/skyblocker/utils/Scheduler.java | 45 ++++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') 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 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, 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, Runnable { @Override public int compareTo(ScheduledTask o) { return schedule - o.schedule; -- cgit From 049f988c904e7bee85e4d0a03e2dbbcf0047d27e Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Mon, 22 May 2023 20:30:12 -0400 Subject: Add Livid Color --- src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index efba4995..09db3f93 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java @@ -12,7 +12,7 @@ import java.util.PriorityQueue; public class Scheduler { private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class); private int currentTick; - private final PriorityQueue tasks; + protected final PriorityQueue tasks; /** * Do not instantiate this class. Use {@link SkyblockerMod#scheduler} instead. @@ -52,17 +52,20 @@ public class Scheduler { currentTick += 1; ScheduledTask task; while ((task = tasks.peek()) != null && task.schedule <= currentTick) { - tasks.poll(); - task.run(); + runTask(tasks.poll()); } } + protected void runTask(Runnable task){ + task.run(); + } + /** * 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 { + protected record CyclicTask(Runnable inner, int period) implements Runnable { @Override public void run() { SkyblockerMod.getInstance().scheduler.schedule(this, period); @@ -75,7 +78,7 @@ public class Scheduler { * @param inner the task to run * @param schedule the tick to run at */ - private record ScheduledTask(Runnable inner, int schedule) implements Comparable, Runnable { + protected record ScheduledTask(Runnable inner, int schedule) implements Comparable, Runnable { @Override public int compareTo(ScheduledTask o) { return schedule - o.schedule; -- cgit From 300fe03100274d0e4f75e1294d71542cd0cc8e0e Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Tue, 30 May 2023 11:23:46 -0400 Subject: Fix MessageScheduler removing and not sending message tasks during cooldown --- src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index 09db3f93..73c15efc 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java @@ -51,13 +51,14 @@ public class Scheduler { public void tick() { currentTick += 1; ScheduledTask task; - while ((task = tasks.peek()) != null && task.schedule <= currentTick) { - runTask(tasks.poll()); + while ((task = tasks.peek()) != null && task.schedule <= currentTick && runTask(task)) { + tasks.poll(); } } - protected void runTask(Runnable task){ + protected boolean runTask(Runnable task) { task.run(); + return true; } /** -- cgit From d5bdb1ef2bf50084fb59029184224b84f0b99806 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Tue, 30 May 2023 11:35:48 -0400 Subject: Fix MessageScheduler not scheduling relative to current tick --- src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index 73c15efc..7313ec10 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java @@ -12,7 +12,7 @@ import java.util.PriorityQueue; public class Scheduler { private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class); private int currentTick; - protected final PriorityQueue tasks; + private final PriorityQueue tasks; /** * Do not instantiate this class. Use {@link SkyblockerMod#scheduler} instead. -- cgit From 69e794ffcd06fe14f76f368b33d5af0e4bc60b3a Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Tue, 30 May 2023 11:43:13 -0400 Subject: Refactored schedulers and added docs --- .../me/xmrvizzy/skyblocker/utils/Scheduler.java | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') 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 tasks; + private int currentTick = 0; + private final PriorityQueue 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, Runnable { -- cgit From 796177d64aac6c64949973a5604aedc0d1f2651f Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 8 Jul 2023 17:20:55 +0800 Subject: Refactor Title Container --- .../me/xmrvizzy/skyblocker/utils/Scheduler.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java index 02162140..7b19e284 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java @@ -1,10 +1,13 @@ package me.xmrvizzy.skyblocker.utils; import me.xmrvizzy.skyblocker.SkyblockerMod; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.PriorityQueue; +import java.util.function.Supplier; /** * 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. @@ -50,6 +53,24 @@ public class Scheduler { } } + /** + * Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed. + * + * @param screenSupplier the supplier of the screen to open + */ + public void queueOpenScreen(Supplier screenSupplier) { + queueOpenScreen(screenSupplier.get()); + } + + /** + * Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed. + * + * @param screen the supplier of the screen to open + */ + public void queueOpenScreen(Screen screen) { + MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screen)); + } + public void tick() { currentTick += 1; ScheduledTask task; -- cgit