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

@ -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;

View file

@ -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<PrimitiveBlock> 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();
}
/**