Fix restore runner being executed too frequently

This commit is contained in:
zontreck 2024-04-25 02:01:43 -07:00
parent 5af11674cd
commit f54faa6f9e
3 changed files with 35 additions and 11 deletions

View file

@ -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. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # 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. # 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. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -8,6 +8,8 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry; import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry;
import dev.zontreck.libzontreck.config.ServerConfig; import dev.zontreck.libzontreck.config.ServerConfig;
@ -59,6 +61,7 @@ public class LibZontreck {
public static final UUID NULL_ID; public static final UUID NULL_ID;
public static boolean LIBZONTRECK_SERVER_AVAILABLE=false; public static boolean LIBZONTRECK_SERVER_AVAILABLE=false;
public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
public static LogicalSide CURRENT_SIDE; public static LogicalSide CURRENT_SIDE;

View file

@ -1,9 +1,12 @@
package dev.zontreck.libzontreck.memory.world; package dev.zontreck.libzontreck.memory.world;
import dev.zontreck.libzontreck.LibZontreck;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtIo;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.io.*; import java.io.*;
@ -12,16 +15,21 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public abstract class BlockRestoreQueue public abstract class BlockRestoreQueue
{ {
private List<PrimitiveBlock> BLOCK_QUEUE = new ArrayList<>(); private List<PrimitiveBlock> BLOCK_QUEUE = new ArrayList<>();
private BlockRestoreRunner RUNNER; private final BlockRestoreRunner RUNNER;
private ScheduledFuture<?> RUNNING_TASK;
public BlockRestoreQueue() public BlockRestoreQueue()
{ {
RUNNER = new BlockRestoreRunner(this); 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); return RUNNER;
tx.start(); }
/**
* 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 @SubscribeEvent
public void onTick(TickEvent.LevelTickEvent event) public void onServerStopping(ServerStoppingEvent event)
{ {
if(event.phase == TickEvent.Phase.END) cancel();
{
tick();
}
} }
/** /**