diff --git a/gradle.properties b/gradle.properties index 5a3376d..5cdc71b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1201.13.042524.0038 +mod_version=1201.13.042524.0151 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java index d51f00b..9356f31 100644 --- a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java +++ b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java @@ -8,6 +8,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.UUID; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry; import dev.zontreck.libzontreck.config.ServerConfig; @@ -59,6 +61,7 @@ public class LibZontreck { public static final UUID NULL_ID; public static boolean LIBZONTRECK_SERVER_AVAILABLE=false; + public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); public static LogicalSide CURRENT_SIDE; diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java index 5cb654b..525da6c 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java @@ -1,9 +1,12 @@ package dev.zontreck.libzontreck.memory.world; +import dev.zontreck.libzontreck.LibZontreck; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtIo; import net.minecraft.server.level.ServerLevel; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import java.io.*; @@ -12,16 +15,21 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; public abstract class BlockRestoreQueue { private List BLOCK_QUEUE = new ArrayList<>(); - private BlockRestoreRunner RUNNER; + private final BlockRestoreRunner RUNNER; + private ScheduledFuture RUNNING_TASK; public BlockRestoreQueue() { RUNNER = new BlockRestoreRunner(this); + + MinecraftForge.EVENT_BUS.register(this); } /** @@ -207,21 +215,34 @@ public abstract class BlockRestoreQueue } /** - * Executes a tick. Spawns a thread which will modify 1 block from the queue + * Gets the restore runner instance initialized for this queue + * @return */ - private void tick() + public BlockRestoreRunner getRunner() { - Thread tx = new Thread(RUNNER); - tx.start(); + return RUNNER; + } + + /** + * Must be called manually to register a restore queue. This will have a 2 second fixed delay before initial execution + */ + public void schedule(long interval, TimeUnit unit) + { + RUNNING_TASK = LibZontreck.executor.scheduleAtFixedRate(RUNNER, 2000, interval, unit); + } + + /** + * Cancels the restore job + */ + public void cancel() + { + RUNNING_TASK.cancel(false); } @SubscribeEvent - public void onTick(TickEvent.LevelTickEvent event) + public void onServerStopping(ServerStoppingEvent event) { - if(event.phase == TickEvent.Phase.END) - { - tick(); - } + cancel(); } /**