aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-09-10 23:35:26 -0400
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-09-10 23:45:31 -0400
commit44904d03f992b46b96ecc7283d054e7b92b7fe50 (patch)
tree98b45ce8a82d8ac2b63598f6a144f06412cfb526
parentb81599c5f82e347b7709d538376fc895d77ba501 (diff)
downloadSkyblocker-44904d03f992b46b96ecc7283d054e7b92b7fe50.tar.gz
Skyblocker-44904d03f992b46b96ecc7283d054e7b92b7fe50.tar.bz2
Skyblocker-44904d03f992b46b96ecc7283d054e7b92b7fe50.zip
Optimize Scheduler
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java18
-rw-r--r--src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java18
2 files changed, 12 insertions, 24 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 1d61edd3..1bc8829b 100644
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
@@ -2,9 +2,7 @@ package me.xmrvizzy.skyblocker.utils.scheduler;
import com.mojang.brigadier.Command;
import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectSortedMap;
-import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
-import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
import me.xmrvizzy.skyblocker.SkyblockerMod;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
@@ -22,7 +20,7 @@ import java.util.function.Supplier;
public class Scheduler {
private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);
private int currentTick = 0;
- private final AbstractInt2ObjectSortedMap<List<ScheduledTask>> tasks = new Int2ObjectAVLTreeMap<>();
+ private final AbstractInt2ObjectSortedMap<List<ScheduledTask>> tasks = new Int2ObjectLinkedOpenHashMap<>();
/**
* Do not instantiate this class. Use {@link SkyblockerMod#scheduler} instead.
@@ -75,20 +73,9 @@ public class Scheduler {
}
public void tick() {
- currentTick += 1;
- if (tasks.containsKey(currentTick - 1)) {
- List<ScheduledTask> currentTickTasks = tasks.get(currentTick - 1);
- for (int i = 0; i < currentTickTasks.size(); i++) {
- ScheduledTask task = currentTickTasks.get(i);
- if (!runTask(task)) {
- tasks.computeIfAbsent(currentTick + 1, key -> new ArrayList<>()).add(task);
- }
- }
- tasks.remove(currentTick - 1);
- }
-
if (tasks.containsKey(currentTick)) {
List<ScheduledTask> currentTickTasks = tasks.get(currentTick);
+ //noinspection ForLoopReplaceableByForEach (or else we get a ConcurrentModificationException)
for (int i = 0; i < currentTickTasks.size(); i++) {
ScheduledTask task = currentTickTasks.get(i);
if (!runTask(task)) {
@@ -97,6 +84,7 @@ public class Scheduler {
}
tasks.remove(currentTick);
}
+ currentTick += 1;
}
/**
diff --git a/src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java b/src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java
index 93cd7720..3bc12337 100644
--- a/src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java
+++ b/src/test/java/me/xmrvizzy/skyblocker/utils/scheduler/SchedulerTest.java
@@ -8,7 +8,7 @@ public class SchedulerTest {
@SuppressWarnings("deprecation")
private final Scheduler scheduler = new Scheduler();
private final MutableInt currentTick = new MutableInt(0);
- private final MutableInt cycleCount1 = new MutableInt(1);
+ private final MutableInt cycleCount1 = new MutableInt(0);
private final MutableInt cycleCount2 = new MutableInt(0);
private final MutableInt cycleCount3 = new MutableInt(0);
private final MutableInt cycleCount4 = new MutableInt(0);
@@ -19,7 +19,7 @@ public class SchedulerTest {
@Test
public void testSchedule() {
- scheduler.schedule(() -> Assertions.assertEquals(1, currentTick.intValue()), 0);
+ scheduler.schedule(() -> Assertions.assertEquals(0, currentTick.intValue()), 0);
scheduler.schedule(() -> Assertions.assertEquals(1, currentTick.intValue()), 1);
scheduler.schedule(() -> Assertions.assertEquals(2, currentTick.intValue()), 2);
scheduler.schedule(() -> Assertions.assertEquals(10, currentTick.intValue()), 10);
@@ -36,12 +36,12 @@ public class SchedulerTest {
cycleCount1.increment();
}, 1);
scheduler.scheduleCyclic(() -> {
- Assertions.assertEquals(1, currentTick.intValue() % 10);
+ Assertions.assertEquals(0, currentTick.intValue() % 10);
Assertions.assertEquals(cycleCount2.intValue(), currentTick.intValue() / 10);
cycleCount2.increment();
}, 10);
scheduler.scheduleCyclic(() -> {
- Assertions.assertEquals(1, currentTick.intValue() % 55);
+ Assertions.assertEquals(0, currentTick.intValue() % 55);
Assertions.assertEquals(cycleCount3.intValue(), currentTick.intValue() / 55);
cycleCount3.increment();
}, 55);
@@ -51,7 +51,7 @@ public class SchedulerTest {
cycleCount4.increment();
}, 10), 7);
scheduler.schedule(() -> scheduler.scheduleCyclic(() -> {
- Assertions.assertEquals(1, currentTick.intValue() % 75);
+ Assertions.assertEquals(0, currentTick.intValue() % 75);
Assertions.assertEquals(cycleCount5.intValue(), currentTick.intValue() / 75);
cycleCount5.increment();
}, 75), 0);
@@ -61,19 +61,19 @@ public class SchedulerTest {
cycleCount6.increment();
}, 99), 1);
scheduler.scheduleCyclic(() -> scheduler.schedule(() -> {
- Assertions.assertEquals(6, currentTick.intValue() % 10);
+ Assertions.assertEquals(5, currentTick.intValue() % 10);
Assertions.assertEquals(cycleCount7.intValue(), currentTick.intValue() / 10);
cycleCount7.increment();
}, 5), 10);
scheduler.scheduleCyclic(() -> scheduler.schedule(() -> {
- Assertions.assertEquals(11, currentTick.intValue() % 55);
+ Assertions.assertEquals(10, currentTick.intValue() % 55);
Assertions.assertEquals(cycleCount8.intValue(), currentTick.intValue() / 55);
cycleCount8.increment();
}, 10), 55);
while (currentTick.intValue() < 10_000_000) {
tick();
}
- Assertions.assertEquals(10000001, cycleCount1.intValue());
+ Assertions.assertEquals(10000000, cycleCount1.intValue());
Assertions.assertEquals(1000000, cycleCount2.intValue());
Assertions.assertEquals(181819, cycleCount3.intValue());
Assertions.assertEquals(1000000, cycleCount4.intValue());
@@ -84,7 +84,7 @@ public class SchedulerTest {
}
private void tick() {
- currentTick.increment();
scheduler.tick();
+ currentTick.increment();
}
}