aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-09-10 20:53:07 -0400
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-09-10 21:41:34 -0400
commit659b73aefd8d9f5d7e60c4d222b7934121703df1 (patch)
tree2f4d6492ecfa86ee016b5019f4123f391d985b60 /src/main/java/me
parentda68e9a162645c90e0ae816f178f25819b81652f (diff)
downloadSkyblocker-659b73aefd8d9f5d7e60c4d222b7934121703df1.tar.gz
Skyblocker-659b73aefd8d9f5d7e60c4d222b7934121703df1.tar.bz2
Skyblocker-659b73aefd8d9f5d7e60c4d222b7934121703df1.zip
Remove ScheduledTask allocations
Diffstat (limited to 'src/main/java/me')
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
index cf285316..3f578899 100644
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
@@ -37,8 +37,7 @@ public class Scheduler {
if (delay < 0) {
LOGGER.warn("Scheduled a task with negative delay");
}
- ScheduledTask tmp = new ScheduledTask(task, currentTick + delay);
- tasks.add(tmp);
+ tasks.add(new ScheduledTask(task, currentTick + delay));
}
/**
@@ -51,7 +50,7 @@ public class Scheduler {
if (period <= 0) {
LOGGER.error("Attempted to schedule a cyclic task with period lower than 1");
} else {
- schedule(new CyclicTask(this, task, period), 0);
+ tasks.add(new CyclicTask(this, task, period));
}
}
@@ -75,6 +74,7 @@ public class Scheduler {
ScheduledTask task;
while ((task = tasks.peek()) != null && task.schedule <= currentTick && runTask(task)) {
tasks.poll();
+ if (task instanceof CyclicTask) tasks.add(task);
}
}
@@ -91,25 +91,36 @@ 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 period the period in ticks
*/
- protected record CyclicTask(Scheduler scheduler, Runnable inner, int period) implements Runnable {
+ protected static class CyclicTask extends ScheduledTask {
+ private final Scheduler scheduler;
+ private final int period;
+
+ CyclicTask(Scheduler scheduler, Runnable inner, int period) {
+ super(inner, scheduler.currentTick);
+ this.scheduler = scheduler;
+ this.period = period;
+ }
+
@Override
public void run() {
- scheduler.schedule(this, period);
- inner.run();
+ super.run();
+ schedule = scheduler.currentTick + period;
}
}
/**
* 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 {
+ protected static class ScheduledTask implements Comparable<ScheduledTask>, Runnable {
+ private final Runnable inner;
+ protected int schedule;
+
+ public ScheduledTask(Runnable inner, int schedule) {
+ this.inner = inner;
+ this.schedule = schedule;
+ }
+
@Override
public int compareTo(ScheduledTask o) {
return schedule - o.schedule;