From 24656fa74f452a7ef9e7d458e5130153bc0ee4d3 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 12 Feb 2023 14:51:30 +0100 Subject: [PATCH] add OneTimeSyncTask and started to implement its test (not finished) Signed-off-by: Quentin Legot --- .../toolbox/task/sync/OneTimeSyncTask.java | 53 +++++++++++++++++++ .../toolbox/task/sync/PeriodicSyncTask.java | 4 +- .../toolbox/task/sync/ServerTickListener.java | 4 +- .../task/sync/OneTimeSyncTaskTest.java | 25 +++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java create mode 100644 Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java new file mode 100644 index 0000000..0beeda1 --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java @@ -0,0 +1,53 @@ +package fr.altarik.toolbox.task.sync; + +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.SchedulerTaskData; +import fr.altarik.toolbox.task.TaskI; + +import java.util.ArrayList; +import java.util.List; + +public class OneTimeSyncTask implements TaskI, Runnable { + + private final ServerTickListener listener; + private final List tasks; + + private OneTimeSyncTask() { + this.listener = new ServerTickListener(this); + this.tasks = new ArrayList<>(2); + } + + public static TaskI initialize() { + return new OneTimeSyncTask(); + } + + @Override + public void addTask(AltarikRunnable function) { + addTask(function, 0); + } + + public void addTask(AltarikRunnable function, int delay) { + tasks.add(new SchedulerTaskData(function, delay, 0)); + } + + @Override + public void run() { + List removeList = new ArrayList<>(tasks.size()); + for(SchedulerTaskData data : tasks) { + if(!data.getFunction().isCancelled()) { + long currentDelay = data.getCurrentDelay(); + if(currentDelay != 0) { + data.setCurrentDelay(currentDelay - 1); + } else { + data.getFunction().run(); + removeList.add(data); + } + } else { + removeList.add(data); + } + } + for(SchedulerTaskData toRemove : removeList) { + tasks.remove(toRemove); + } + } +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java index c4e38f9..25d2e10 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java @@ -9,8 +9,8 @@ import java.util.List; public class PeriodicSyncTask implements PeriodicTaskI, Runnable { - private ServerTickListener listener; - private List tasks; + private final ServerTickListener listener; + private final List tasks; private PeriodicSyncTask() { this.listener = new ServerTickListener(this); diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java index a1ae23c..d70562d 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java @@ -5,9 +5,9 @@ import net.minecraft.server.MinecraftServer; public class ServerTickListener { - private final PeriodicSyncTask task; + private final Runnable task; - public ServerTickListener(PeriodicSyncTask syncTask) { + public ServerTickListener(Runnable syncTask) { this.task = syncTask; ServerTickEvents.START_SERVER_TICK.register(this::onServerTick); } diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java new file mode 100644 index 0000000..e9e8a1f --- /dev/null +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java @@ -0,0 +1,25 @@ +package fr.altarik.toolbox.task.sync; + +import fr.altarik.toolbox.task.AltarikRunnable; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class OneTimeSyncTaskTest { + + @Test + void testOneTimeTask() { + OneTimeSyncTask worker = (OneTimeSyncTask) OneTimeSyncTask.initialize(); + List results = new ArrayList<>(); + AtomicInteger value1 = new AtomicInteger(1); + AtomicInteger value2 = new AtomicInteger(2); + worker.addTask(new AltarikRunnable() { + @Override + public void run() { + results.add(value1); + } + }); + } +}